C#TextBox事件实现实例详解

C# TextBox事件是我们在开发中会碰到的具体的功能需求,那么如何使得想要的C# TextBox事件执行呢?那么这里就向你介绍具体的C# TextBox事件演示实例,包括需求的实现,希望对你有所帮助。

公司主营业务:成都网站建设、做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出湘阴免费做网站回馈大家。

C# TextBox事件具体的需求:

◆界面要求:定义5个TEXTBOX,分别是:姓名、地址、职业、年龄、输出内容。1个BUTTON

◆满足条件:

(1)用户名不能为空

(2)年龄必须是一个大于或等于0的数字

(3)职业必须是“程序员”或为空

(4)地址不能为空

C# TextBox事件实例实现:

 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. namespace Chapter14  
  9. ...{  
  10. public partial class Form1 : Form  
  11. ...{  
  12. public Form1()  
  13. ...{  
  14. InitializeComponent();  
  15. this.button1.Enabled = false;  
  16. this.textBox1.Tag = false;  
  17. this.textBox2.Tag = false;  
  18. this.textBox3.Tag = false;  
  19. this.textBox4.Tag = false;  
  20. this.textBox1.Validating+=   
  21. new System.ComponentModel.CancelEventHandler(  
  22. this.textboxEmpty_Validating);  
  23. this.textBox2.Validating +=   
  24. new System.ComponentModel.CancelEventHandler(  
  25. this.textboxEmpty_Validating);  
  26. this.textBox3.Validating+=   
  27. new System.ComponentModel.CancelEventHandler(  
  28. this.textboxOccupation_Validating);  
  29. this.textBox4.Validating +=   
  30. new System.ComponentModel.CancelEventHandler(  
  31. this.textboxEmpty_Validating);  
  32. this.textBox1.TextChanged +=   
  33. new System.EventHandler(this.textbox_TextChanged);  
  34. this.textBox4.TextChanged +=   
  35. new System.EventHandler(this.textbox_TextChanged);  
  36. this.textBox3.TextChanged +=   
  37. new System.EventHandler(this.textbox_TextChanged);  
  38. }  
  39. //C# TextBox事件  
  40. private void textboxOccupation_Validating(  
  41. object sender,   
  42. System.ComponentModel.CancelEventArgs e)  
  43. ...{  
  44. TextBox tb=(TextBox)sender;  
  45. if (tb.Text.CompareTo("Programmer") == 0||tb.Text.Length==0)  
  46. ...{  
  47. tb.Tag = true;  
  48. tb.BackColor = System.Drawing.SystemColors.Window;  
  49.  
  50.  
  51. }  
  52. else 
  53. ...{  
  54. tb.Tag = false;  
  55. tb.BackColor = Color.Red;  
  56.  
  57. }  
  58. ValidateOk();  
  59. }  
  60. //C# TextBox事件  
  61. private void textboxEmpty_Validating(  
  62. object sender,   
  63. System.ComponentModel.CancelEventArgs e)  
  64. ...{  
  65. TextBox tb = (TextBox)sender;  
  66. if (tb.Text.Length == 0)  
  67. ...{  
  68. tb.BackColor = Color.Red;  
  69. tb.Tag = false;  
  70. }  
  71. else 
  72. ...{  
  73. tb.BackColor = System.Drawing.SystemColors.Window;  
  74. tb.Tag = true;  
  75. }  
  76. ValidateOk();  
  77. }  
  78. private void textboxAge_KeyPress(  
  79. object sender, KeyPressEventArgs e)  
  80. ...{  
  81. if ((e.KeyChar < 48 || e.KeyChar > 57) &&   
  82. e.KeyChar != 8)  
  83. e.Handled = true;  
  84. }  
  85. private void textbox_TextChanged(  
  86. object sender, System.EventArgs e)  
  87. ...{  
  88. TextBox tb = (TextBox)sender;  
  89. if (tb.Text.Length == 0&& tb!=textBox3)  
  90. ...{  
  91. tb.Tag = false;  
  92. tb.BackColor = Color.Red;  
  93.  
  94. }  
  95. else if (tb == this.textBox3 &&   
  96. (tb.Text.Length != 0 &&   
  97. tb.Text.CompareTo("Programmer") != 0))  
  98. ...{  
  99. tb.Tag = false;  
  100. }  
  101. //C# TextBox事件  
  102. else 
  103. ...{  
  104. tb.Tag = true;  
  105. tb.BackColor = SystemColors.Window;  
  106. }  
  107. ValidateOk();  
  108. }  
  109. private void ValidateOk()  
  110. ...{  
  111. this.button1.Enabled =   
  112. ((bool)(this.textBox2.Tag) &&   
  113. (bool)(this.textBox4.Tag) &&   
  114. (bool)(this.textBox1.Tag) &&   
  115. (bool)(this.textBox3.Tag));  
  116. }  
  117.  
  118. private void button1_Click(  
  119. object sender, EventArgs e)  
  120. ...{  
  121. string output;  
  122.   //C# TextBox事件  
  123. output = "Name:" + this.textBox1.Text + " ";  
  124. output += "Address:" + this.textBox2.Text + " ";  
  125. output += "Occupation:" + this.textBox3.Text + " ";  
  126. output += "Age:" + this.textBox4.Text + " ";  
  127. this.textBox5.Text = output;  
  128.  
  129. }  
  130. }  
  131. }  

C# TextBox事件实现的具体实例就向你介绍到这里,希望对你了解和学习C# TextBox事件有所帮助。

【编辑推荐】

  1. 详解C# MessageBox用法
  2. 详解C# CheckBox选中的判断方法
  3. C# CheckBox控件概念以及用途浅析
  4. 学习C# MessageBox用法的一点体会
  5. 浅析C# TextBox事件实现体会

网页标题:C#TextBox事件实现实例详解
路径分享:http://www.shufengxianlan.com/qtweb/news38/277988.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联