Learn WinForm Text class controls (Label, Button, TextBox) _c# tutorial

Source: Internet
Author: User

For an application, the control is the building block that builds the user interface. They have interactive features. The controls in VS 2012 are placed in the toolbox, add a control to the form, just select the control you want to add in the toolbox, and hold down the left mouse button and drag onto the form. Another way to add a control to a form is through code, which can be formatted as a form ID by using the Add method of the form's controls class. Controls.Add (Control identification). Adding controls through the code is cumbersome, and basically does not use this method.
Text class controls contain label controls (label), Button controls (buttons), Text box controls (textbox), and Rich text controls (RichTextBox).
one, Label control
The label control is the simplest control in each version of VS, and is the control provided by the System.Windows.Forms.Label class. The role of a label control is usually used to provide descriptive text for other controls. The commonly used properties of a label control are the Text property, which can be set either through the property panel or in code, in the form of a Label control id.text= "description to display."
Create a form, add two label controls to the form Label1 and Label2, right-click Label1 Select Property command, set the Text property value to "I was set at design time." Double-click the form, generate the form Add event in code, and add a piece of code:

<span style= "FONT-SIZE:18PX;" > 

private void Form1_Load (object sender, EventArgs e) 
 { 
  Label2. Text = "I set it in code"; 
 } </span>

Running the form will show the following illustration:

Second, Button control
The button control is a control provided by the System.Windows.Forms.Button class, which is most commonly used to write the Click event code that handles the button. The commonly used properties of a button control are the Text property, the Image property, and the ImageList property. The Text property is used to set the button's literal, the Image property and the ImageList property are the images that are set to the buttons control, and the ImageList property is to set the ImageList object that is placed on the form. The Acceptbuttons property of a form sets the default button when the form is opened, and in general, the default button for the form has a slightly thicker border.
Add three button controls to the form, set the Name property to Btndefault,btnnodefault and Btnimage, set the Btndefault Text property to default, and set the Btnnodefault Text property to No default, sets the Btnimage Text property to NULL, and sets its Image property to an image. and set the form default button to Btndefault.
Double-click Btndefault, and the Btndefault click event appears in your code, adding a message box to display the specified message within the event. Add the Click event for Btnnodefault, whose code is:

<span style= "FONT-SIZE:18PX;" >private void Btndefault_click (object sender, EventArgs e) 
{ 
 MessageBox.Show ("I am the default button"); 
} 
 
private void Btnnodefault_click (object sender, EventArgs e) 
{ 
 MessageBox.Show ("I am not the default button"); 
} </span> 

Diagram of the result displayed after running the form and pressing the ENTER key:

Three, TextBox controls and RichTextBox controls

The TextBox control has the control provided by the System.Windows.Forms.TextBox class, provides basic text input and editing capabilities, and the common properties of the TextBox control;

A common event for a TextBox control is the TextChanged event, which means the event that is responsive when the text value of a text property changes.
Add two TextBox controls to the form, set the Name property to Tbsingle and Tbmall, set the Tbmall multiline to Ture, set the Tbmall property scrollbars to both, and set the property lines to " I am Chinese ", set the Tbsingle PasswordChar as" * ". Double-click Tbsingle, the TextChanged event appears in the code, and a message box that displays the specified message is added to the event, prompting the user to change the text content to the code:

<span style= "FONT-SIZE:18PX;" >
private void Tbsingle_textchanged (object sender, EventArgs e) 
 
 MessageBox.Show ("My text content changed"); 
t;
</span> 

The results of the run form display are:

The RichTextBox control is an upgraded version of the TextBox control that allows you to apply the control more flexibly, setting the color of the font, the form, and adding pictures, but more often than not the textbox is used.

The above is the entire content of this article, I hope to learn control to help you.

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.