想要容器groupBox内所有的文本框可以实现以下两个事件。事件内该怎么写呢?
注册事件时的代码有问题吗?
注:文本框的命名没有规则。比如第一个文本框名为txtBox1,可能第二个文本框名为txt2。 public Form1()
{
InitializeComponent();
SetEvents();
} /// <summary>
/// 注册事件
/// </summary>
void SetEvents()
{
foreach (Control ctl in this.groupBox.Controls)
{
if (ctl is TextBox)
{
TextBox txtBox = ctl as TextBox;
txtBox.Click += new System.EventHandler(txtBox_Click);
txtBox.TextChanged += new EventHandler(txtBox_TextChanged);
}
}
} private void txtBox_Click(object sender, EventArgs e)
{
//选中当前文本框所有文本
//类似实现if(txtBox1.Focus())
// txtBox1.SelectAll();
} private void txtBox_TextChanged(object sender, EventArgs e)
{
//文本框内文本改变触发事件
}
注册事件时的代码有问题吗?
注:文本框的命名没有规则。比如第一个文本框名为txtBox1,可能第二个文本框名为txt2。 public Form1()
{
InitializeComponent();
SetEvents();
} /// <summary>
/// 注册事件
/// </summary>
void SetEvents()
{
foreach (Control ctl in this.groupBox.Controls)
{
if (ctl is TextBox)
{
TextBox txtBox = ctl as TextBox;
txtBox.Click += new System.EventHandler(txtBox_Click);
txtBox.TextChanged += new EventHandler(txtBox_TextChanged);
}
}
} private void txtBox_Click(object sender, EventArgs e)
{
//选中当前文本框所有文本
//类似实现if(txtBox1.Focus())
// txtBox1.SelectAll();
} private void txtBox_TextChanged(object sender, EventArgs e)
{
//文本框内文本改变触发事件
}
Textbox textbox1 = (Textbox)sender;
switch(textbox1.name){
case "textbox1":{}
}
TextBox txtBox = sender as TextBox;
if(txtBox.Focus())
txtBox.SelectAll();
原来sender是这么用的...也感谢前面的各位兄弟!