日期:2014-05-18 浏览次数:21243 次
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":{ } }
------解决方案--------------------
查看sender参数
------解决方案--------------------
没问题。事件里面用sender获取执行的控件,如
TextBox txtBox = sender as TextBox;
if(txtBox.Focus())
txtBox.SelectAll();