Step 2 add controls to the form
The next step is to add controls to the form. We create an instance variable for each control, initialize these new instance variables, and put each control in the form. Here is the form and updated code after the control is added:
Public class TempConverter: System. WinForms. Form {
Label lTempFah = new Label ();
Label lTempCel = new Label ();
TextBox tTempFah = new TextBox ();
TextBox tTempCel = new TextBox ();
Button bnCtoF = new Button ();
Button bnFtoC = new Button ();
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;
TTempCel. TabIndex = 0;
TTempCel. SetSize (50, 25 );
TTempCel. SetLocation (13, 5 );
LTempCel. TabStop = false;
LTempCel. Text = "C ";
LTempCel. SetSize (25, 25 );
LTempCel. SetLocation (65,5 );
TTempFah. TabIndex = 1;
TTempFah. SetSize (50, 25 );
TTempFah. SetLocation (90,5 );
LTempFah. TabStop = false;
LTempFah. Text = "F ";
LTempFah. SetSize (25, 25 );
LTempFah. SetLocation (142,5 );
BnCtoF. TabIndex = 2;
BnCtoF. Text = "C to F ";
BnCtoF. SetSize (70, 25 );
BnCtoF. SetLocation (13, 35 );
BnFtoC. TabIndex = 3;
BnFtoC. Text = "F to C ";
BnFtoC. SetSize (70, 25 );
BnFtoC. SetLocation (90,35 );
This. Controls. Add (tTempCel );
This. Controls. Add (lTempCel );
This. Controls. Add (tTempFah );
This. Controls. Add (lTempFah );
This. Controls. Add (bnCtoF );
This. Controls. Add (bnFtoC );
}
The above code first creates two labels, two text boxes, and two buttons, then initializes each control and adds it to the form. The specific meanings are as follows:
-SetSize () initializes the widget size
-SetLocation () initializes the position of the control in the form.
-Set the TabStop attribute of the control to false, indicating that the control is never focused.
-Setting TabIndex to X indicates to focus on this control when the TAB key is hit x times.
-The text attribute of the control indicates the text information displayed on it.
-This. Controls. Add () indicates to place a control on the form. To Add each control quickly, you can write as follows: this. Controls = new
Control [] {tTempCel, lTempCel, tTempFar ?.}