不了解,这个,代表对事件的本质不大清楚简单的说当发生了事件以后,我们会交由一个事件处理程序(textBoxEmpty_Validating)来处理这件事(Validating),但你总的告诉事件处理程序是谁(sender)引发了事件,还有事件的其他信息(System.ComponentModel.CancelEventArgs)这就正如你要让律师帮你打官司,你总的让律师知道是"你"出了"什么样的事情"吧而这里TextBox tb = (TextBox)sender则是进行了一个强制类型转换,sender其实就是事件源,那么把它转换成TextBox也就是合情合理的了顺便提一句private void 事件处理程序(object sender,XXXEventArgs e) 是标准的事件处理程序
解决方案 »
- 串口通信的问题
- 我的Reflection示例中,为什么没有创建实例?
- varbinary(max)对应的net类型是什么
- C#对AutoCad二次开发的问题
- 有谁做过网站退货业务的 怎么使用DataList进行换行(用表格的形式)每行3列 10行后换行啊
- winform界面绘制速度很慢
- 请问.Net Compact Framework不支持窗体继承吗?
- Socket问题,在线等.
- 往服务器上上传文件的路径问题
- 求.net2005中windows应用程序将datatable转成Excel文件的代码(急)
- 有关利用ASP.NET+SQL SERVER 开发医院体检管理系统
- c#里面如何使禁止Form的托拽?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;#endregionnamespace TextBoxTest
{
partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.buttonOK.Enabled = false;// Tag values for testing if the data is valid
this.textBoxAddress.Tag = false;
this.textBoxAge.Tag = false;
this.textBoxName.Tag = false;// Subscriptions to events
this.textBoxName.Validating += new System.ComponentModel.CancelEventHandler(this.textBoxEmpty_Validating);
this.textBoxAddress.Validating += new
System.ComponentModel.CancelEventHandler(this.textBoxEmpty_Validating);
this.textBoxAge.Validating += new
System.ComponentModel.CancelEventHandler(this.textBoxEmpty_Validating);
this.textBoxName.TextChanged += new System.EventHandler(this.textBox_TextChanged);
this.textBoxAddress.TextChanged += new
System.EventHandler(this.textBox_TextChanged);
this.textBoxAge.TextChanged += new System.EventHandler(this.textBox_TextChanged);
} private void buttonOK_Click(object sender, EventArgs e)
{
// No testing for invalid values are made, as that should
// not be necessary string output; // Concatenate the text values of the four TextBoxes
output = "Name: " + this.textBoxName.Text + "\r\n";
output += "Address: " + this.textBoxAddress.Text + "\r\n";
output += "Occupation: " + (string)(this.checkBoxProgrammer.Checked ?
"Programmer" : "Not a programmer") + "\r\n";
output += "Sex: " + (string)(this.radioButtonFemale.Checked ? "Female" :
"Male") + "\r\n";
output += "Age: " + this.textBoxAge.Text; // Insert the new text
this.textBoxOutput.Text = output;
} private void buttonHelp_Click(object sender, EventArgs e)
{
// Write a short description of each TextBox in the Output TextBox
string output; output = "Name = Your name\r\n";
output += "Address = Your address\r\n";
output += "Programmer = Check 'Programmer' if you are a programmer\r\n";
output += "Sex = Choose your sex\r\n";
output += "Age = Your age"; // Insert the new text
this.textBoxOutput.Text = output;
} private void textBoxEmpty_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
// We know the sender is a TextBox, so we cast the sender object to that
TextBox tb = (TextBox)sender; // If the text is empty we set the background color of the
// Textbox to red to indicate a problem. We use the tag value
// of the control to indicate if the control contains valid
// information.
if (tb.Text.Length == 0)
{
tb.BackColor = Color.Red;
tb.Tag = false; // In this case we do not want to cancel further processing,
// but if we had wanted to do this, we would have added this line:
// e.Cancel = true;
}
else
{
tb.BackColor = System.Drawing.SystemColors.Window;
tb.Tag = true;
} // Finally, we call ValidateOK which will set the value of
// the OK button.
ValidateOK();
} private void textBoxAge_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
e.Handled = true; // Remove the character
} private void textBox_TextChanged(object sender, System.EventArgs e)
{
// Cast the sender object to a Textbox
TextBox tb = (TextBox)sender; // Test if the data is valid and set the tag and background
// color accordingly.
if (tb.Text.Length == 0)
{
tb.Tag = false;
tb.BackColor = Color.Red;
}
else
{
tb.Tag = true;
tb.BackColor = SystemColors.Window;
} // Call ValidateOK to set the OK button
ValidateOK();
} private void ValidateOK()
{
// Set the OK button to enabled if all the Tags are true
this.buttonOK.Enabled = ((bool)(this.textBoxAddress.Tag) &&
(bool)(this.textBoxAge.Tag) &&
(bool)(this.textBoxName.Tag));
}
}
}
其最重要的就是一个Cancel属性,设为true表示取消操作。
TextBox tb = (TextBox)sender;
sender是指事件的发起人,也就是事件提供程序的调用者。sender是一个object类型,在这里把它转换成了TextBox,以便对其按TextBox操作。当然,这样转换必须确定sender是TextBox,否则引发异常。建议学习一下“C#事件”部分,加深理解。
object sender----->触发事件的对象
System.ComponentModel.CancelEventArgs e----->其他一些信息可以看出是对象textBoxEmpty 触发了Validating事件.
由于sender 代表了TextBox,但这里他为object.所以处理时,需要显式转换...建议查看有关事件和委托有关知识!
TextBox tb = (TextBox)sender;
将TextBox tb引用传递sender对象.将sender对象的属性进行需要的改变.
System.ComponentModel.CancelEventArgs e)楼主的这个方法是 TextBox 控件一个通用的有效性验证. 代码重用的一个小示例而已.
TextBox tb = (TextBox)sender; //将触发此方法的对象 sender 强制转换为 TextBox 后验证.System.ComponentModel.CancelEventArgs 很明显,MS 标准类,查一下 MSDN