Combined Code of asp.net WinForm TextBox event

Source: Internet
Author: User

Asp tutorial. net winform textbox event combination code

Functional requirements:

The user name cannot be blank.

The user's age must be a number greater than or equal to 0;

User address cannot be blank

The user's role must be "software" or be blank

The textchanged, keypress, and validating events are used in this function. According to the functional requirements, the user name text box and the user address text box use the same event emptyvalidating to determine whether the text box is empty;

This function uses the tag attribute of the space to set the data of the control. In the constructor, we set the user name text box, user address text box, and user age text box, the value of the tag in the user title text box is false.

Use keypress events -- delete unwanted characters before they are displayed in the text box.

 

Code
   
1 public partial class form1: form
2 {
3 public form1 ()
4 {
5 initializecomponent ();
6 this. button1.enabled = false;
7}
8
9 private void emptyvalidating (object sender, canceleventargs e)
10 {
11 textbox tb = (textbox) sender;
12 if (tb. text. length = 0)
13 {
14 tb. backcolor = color. red;
15 tb. tag = false;
16}
17 else
18 {
19 tb. backcolor = systemcolors. window;
20 tb. tag = true;
21}
22 validateok ();
23}
24 public void validateok ()
25 {
26 this. button1.enabled = (bool1_this.txt name. tag & (bool1_this.txt address. tag & (bool1_this.txt age. tag & (bool1_this.txt occuption. tag;
27}
28
29 private void txtoccuption_validating (object sender, canceleventargs e)
30 {
31 textbox tb = (textbox) sender;
32 if (tb. text. length = 0 | tb. text. compareto ("software") = 0)
33 {
34 tb. tag = true;
35 tb. backcolor = systemcolors. window;
36
37}
38 else
39 {
40 tb. tag = color. red;
41 tb. backcolor = color. red;
42}
43 validateok ();
44}
45
46 private void txtage_keypress (object sender, keypresseventargs e)
47 {
48 if (e. keychar <48 | e. keychar> 57) & e. keychar! = 8)
49 {
50 e. handled = true;
51
52}
53
54
55}
56
57 private void txtage_validating (object sender, canceleventargs e)
58 {
59 textbox tb = (textbox) sender;
60 if (tb. text. length = 0)
61 {
62 tb. tag = false;
63 tb. backcolor = color. red;
64}
65 else
66 {
67 tb. tag = true;
68 tb. backcolor = systemcolors. window;
69}
70 validateok ();
71}
72
73 private void messagechanged (object sender, eventargs e)
74 {
75 textbox tb = (textbox) sender;
76 if (tb. text. length = 0 & tb. name! = "Txtoccuption ")
77 {
78 tb. tag = false;
79 tb. backcolor = color. red;
80}
81 else if (tb. name = "txtoccuption" & (tb. text. length! = 0 & tb. text. compareto ("software ")! = 0 ))
82 {
83 tb. tag = false;
84 // tb. backcolor = color. red;
85}
86 else
87 {
88 tb. tag = true;
89 tb. backcolor = systemcolors. window;
90}
91 validateok ();
92}
93
94 private void button#click (object sender, eventargs e)
95 {
96 this.txt output. text = "";
97 this.txt output. text + = "name:" + this.txt name. text + "rn" + "address:" + this.txt address. text + "rn" + "title:" + this.txt occuption. text + "rn" + "age:" + this.txt age. text;
98}
99}

 

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.