C# TextBox事件实现实例详解

开发 后端
C# TextBox事件的实现需要注意什么呢?C# TextBox事件具体的会用到什么方式呢?那么本文就向你详细介绍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事件实现体会
责任编辑:仲衡 来源: 百度空间
相关推荐

2009-09-09 11:29:32

C# TextBox事

2009-08-27 18:09:49

C#接口的实现

2009-09-01 13:59:01

C#操作Excel

2009-08-17 14:41:47

C#进度条实现

2009-09-01 16:59:25

C#画直线

2009-09-02 17:29:10

C# TextBox换

2009-09-09 13:18:10

C# TextBox滚C# TextBox

2009-09-03 17:06:17

C#回车切换焦点

2009-09-03 17:23:45

C#发送邮件

2009-09-02 16:14:21

C#动态创建数组

2009-09-03 10:52:41

C#递归树

2009-08-14 09:50:46

C#复制构造函数

2009-08-17 17:15:48

C# 进度条效果

2009-08-26 11:32:37

C#打印文档

2009-08-26 11:07:36

C#打印窗体

2009-08-21 10:13:02

C#异步初步

2009-08-26 09:22:44

C#实现打印功能

2009-09-08 23:35:12

c# textbox失

2009-08-31 12:31:45

C#创建文件夹

2009-09-03 12:52:50

C#打开记事本
点赞
收藏

51CTO技术栈公众号