Some basic techniques for TextBox in WinForm in asp.net

Source: Internet
Author: User

1. Common attributes

The code is as follows: Copy code
This. textBox5.PasswordChar = '@'; // password style
This. textBox5.UseSystemPasswordChar = true; // if this attribute is true, the password is the same as the default password style, and the PasswordChar attribute does not work.
This. textBox5.Multiline = true; // display multiple rows
This. textBox5.WordWrap = true; // wrap automatically
This. textBox5.ScrollBars = ScrollBars. Vertical; // display the Vertical scroll bar. One of the enumerated values of ScrollBars.
This. textBox5.MaxLength = 100; // maximum number of characters entered in the text box.

2. Implement automatic completion to improve user experience

To achieve automatic completion, you must understand three attributes:

AutoCompleteSource attribute: Set the source for automatic completion. The value of this attribute is one of the AutoCompleteSource enumerated values.

AutoCompleteMode attribute: sets the automatic display mode. The value of this attribute is one of the AutoCompleteMode enumerated values.

AutoCompleteCustomSource attribute: custom completion source. When the value of AutoCompleteSource is CustomSource

Attribute. The property value is the AutoCompleteStringCollection collection object. You can use the AutoCompleteCustomSource attribute.

This set.

You can use the direct property value to achieve automatic completion or code implementation. The code is as follows:

The code is as follows: Copy code

AutoCompleteStringCollection myCutomSource = new AutoCompleteStringCollection ();
MyCutomSource. AddRange (new string [] {"Chengdu East Gate", "Chengdu North Gate", "Chengdu West Gate", "Chengdu South Gate "});
This. textBox5.AutoCompleteSource = AutoCompleteSource. CustomSource;
This. textBox5.AutoCompleteMode = AutoCompleteMode. SuggestAppend;
This. textBox5.AutoCompleteCustomSource = myCutomSource;

3. The characters in the TextBox control are converted to uppercase or lowercase letters at the same time.

Method 1: Use the CharacterCasing attribute. The default value of this attribute is Normal, meaning that the case of characters will not change. There are two property values: Upper and Lower.

Method 2: implemented through the KeyPress event of TextBox

The code is as follows: Copy code

Private void textBox3_KeyPress (object sender, KeyPressEventArgs e)
        {
If (char. IsLower (e. KeyChar ))
            {
TextBox3.SelectedText = char. ToUpper (e. KeyChar). ToString ();
E. Handled = true;
            }
        }

4. Verify user input to improve user experience

The basic idea is: first, use the Validating event of the control to verify user input. Second, when the input value does not meet the requirements, use the ErrorProvider control to notify the user or use MessageBox to notify the user in the pop-up dialog box. It is clear that the RrrorProvider control is used to notify users of more user experience.

Note: The Validating event is triggered only when the CauseValidation attribute of the control is set to True (this is the default value. At the same time, it must be known that the Validating event will be triggered before the control component loses focus. The Validating event provides the CancelEventArgs parameter. You can set the Cancel attribute to indicate whether the data in the control is valid, if you set the Cancel attribute to True (indicating that the data in the control is invalid), the focus is on the control with invalid data; if you keep the default value of the Cancel attribute False (indicating that the data in the control is valid), the Validated event will be triggered and the focus will be transferred to the new control.

Note: The setting value of the CauseValidation attribute of the control to which the focus goes also determines whether the Validating event of the control where the focus is originally located should be triggered. If the value of the CauseValidation attribute of the control to which the focus goes is False, the Validating event of the control where the focus is originally located

"# Ff0000"> not triggered. If the value of the CauseValidation attribute of the control to which the focus goes is True, the Validating event of the control where the focus is originally located is triggered. The advantage is that it increases flexibility and does not omit any verification operations. In addition, the form cannot be closed as long as the input value of any control in the form cannot pass the validation condition of the Validating event.

Example:

The code is as follows: Copy code

// You must drag the ErrorProvider control from the toolkit component to the form. No matter how many controls are verified by the eye, only one ErrorProvider control is required.
Private void txtUserName_Validating (object sender, CancelEventArgs e)
        {
If (this.txt UserName. Text = string. Empty)
            {
ErrorProvider1.SetError (txtUserName, "name cannot be blank! ");
E. Cancel = true;
            }
Else
            {
ErrorProvider1.SetError (txtUserName ,"");
            }

Another example:

The code is as follows: Copy code

Private void txtPhone_Validating (object sender, CancelEventArgs e)
        {
Regex re = new Regex (@ "^ (d {3}) d {4} d {4} $ ");
If (! Re. IsMatch (txtPhone. Text ))
            {
ErrorProvider1.SetError (txtPhone, "the phone number must conform to the format of (xxx) xxxx xxx. ");
E. Cancel = true; // when the input value does not meet the requirements, it cannot be removed from the control.
            }
Else
            {
ErrorProvider1.SetError (txtPhone ,"");
            }
        }

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.