扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
本篇文章为大家展示了C# 中怎么实现一个TextBox事件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
公司主营业务:网站制作、成都网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出忠县免费做网站回馈大家。
C# TextBox事件具体的需求:
◆界面要求:定义5个TEXTBOX,分别是:姓名、地址、职业、年龄、输出内容。1个BUTTON
◆满足条件:
(1)用户名不能为空
(2)年龄必须是一个大于或等于0的数字
(3)职业必须是“程序员”或为空
(4)地址不能为空
C# TextBox事件实例实现:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Chapter14 ...{ public partial class Form1 : Form ...{ public Form1() ...{ InitializeComponent(); this.button1.Enabled = false; this.textBox1.Tag = false; this.textBox2.Tag = false; this.textBox3.Tag = false; this.textBox4.Tag = false; this.textBox1.Validating+= new System.ComponentModel.CancelEventHandler( this.textboxEmpty_Validating); this.textBox2.Validating += new System.ComponentModel.CancelEventHandler( this.textboxEmpty_Validating); this.textBox3.Validating+= new System.ComponentModel.CancelEventHandler( this.textboxOccupation_Validating); this.textBox4.Validating += new System.ComponentModel.CancelEventHandler( this.textboxEmpty_Validating); this.textBox1.TextChanged += new System.EventHandler(this.textbox_TextChanged); this.textBox4.TextChanged += new System.EventHandler(this.textbox_TextChanged); this.textBox3.TextChanged += new System.EventHandler(this.textbox_TextChanged); } //C# TextBox事件 private void textboxOccupation_Validating( object sender, System.ComponentModel.CancelEventArgs e) ...{ TextBox tb=(TextBox)sender; if (tb.Text.CompareTo("Programmer") == 0||tb.Text.Length==0) ...{ tb.Tag = true; tb.BackColor = System.Drawing.SystemColors.Window; } else ...{ tb.Tag = false; tb.BackColor = Color.Red; } ValidateOk(); } //C# TextBox事件 private void textboxEmpty_Validating( object sender, System.ComponentModel.CancelEventArgs e) ...{ TextBox tb = (TextBox)sender; if (tb.Text.Length == 0) ...{ tb.BackColor = Color.Red; tb.Tag = false; } else ...{ tb.BackColor = System.Drawing.SystemColors.Window; tb.Tag = true; } ValidateOk(); } private void textboxAge_KeyPress( object sender, KeyPressEventArgs e) ...{ if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8) e.Handled = true; } private void textbox_TextChanged( object sender, System.EventArgs e) ...{ TextBox tb = (TextBox)sender; if (tb.Text.Length == 0&& tb!=textBox3) ...{ tb.Tag = false; tb.BackColor = Color.Red; } else if (tb == this.textBox3 && (tb.Text.Length != 0 && tb.Text.CompareTo("Programmer") != 0)) ...{ tb.Tag = false; } //C# TextBox事件 else ...{ tb.Tag = true; tb.BackColor = SystemColors.Window; } ValidateOk(); } private void ValidateOk() ...{ this.button1.Enabled = ((bool)(this.textBox2.Tag) && (bool)(this.textBox4.Tag) && (bool)(this.textBox1.Tag) && (bool)(this.textBox3.Tag)); } private void button1_Click( object sender, EventArgs e) ...{ string output; //C# TextBox事件 output = "Name:" + this.textBox1.Text + " "; output += "Address:" + this.textBox2.Text + " "; output += "Occupation:" + this.textBox3.Text + " "; output += "Age:" + this.textBox4.Text + " "; this.textBox5.Text = output; } } }
上述内容就是C# 中怎么实现一个TextBox事件,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流