In this article, we will experience the C # GUI development process by editing a small program that converts temperature from Celsius to Fahrenheit. The basic requirement for reading this article is that you have basic knowledge of C # and object-oriented programming. The purpose of this article is to introduce C #, and if you want to compile or run the programs listed in this article, you need support for the. NET Framework SDK.
Creating a Windows application consists primarily of the following basic steps: Creating the appropriate form, adding control to the form, and finally adding code. The C # and. NET framework needed to complete the above process can be found in System.WinForms namespace.
The first step is to create the form.
We use Class System.WinForms as the origin, create a class, and then initialize the attribute. For example, the definition of class begins as follows
public class TempConverter : System.WinForms.Form {
.
.
.
}
Here are the main window styles we want
Size is 180*90 pixel
You cannot modify the window size arbitrarily.
Title shown as °c->°f/°f->°c
The form appears in the center of the screen
We don't need the Help button (our application is so simple that we don't need this kind of help)
We do not need to give users permission to enlarge the scope of the program's Windows (because everything is clearly visible in a given size).
Initializes the form by setting the property value of the Tempconverter object. There are two ways to set a property value:
First, use the method to set the property value
Second, through the attribute variable directly set.
The following code. If you want to know more about the properties and methods of the WinForms class, you can refer to the random files of the. NET Framework SDK.
this.SetSize(180,90);
this.BorderStyle = FormBorderStyle.FixedDialog;
this.Text = "°C->°F / °F->°C";
this.StartPosition = FormStartPosition.CenterScreen;
this.HelpButton = false;
this.MaximizeBox = false;
With the above steps, we can connect all the code together, so that we can easily edit, run the program to watch the appearance of the form. To do this, we use the class definition to create a constructor that contains the code mentioned above and initializes the main window's appearance, and then you need to create a main method. The following steps are established:
public class TempConverter : System.WinForms.Form {
public TempConverter() {
this.SetSize(180,90);
this.BorderStyle = FormBorderStyle.FixedDialog;
this.Text = "°C->°F / °F->°C";
this.StartPosition = FormStartPosition.CenterScreen;
this.HelpButton = false;
this.MaximizeBox = false;
}
public static void Main() {
Application.Run( new TempConverter() );
}
}
A new statement appears in Main ():
Application.Run (New Tempconverter ());