C # Introduction to programming trilogy: add controls to the form in step 2

Source: Internet
Author: User

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 ?.}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.