
关于C#双击清空文本框!
假设一个系统有多个窗体,每个窗体上都有一些textbox和combobox,现在想在窗体里双击任意一个textbox或者combobox的时候对其进行清空(.text="...
假设一个系统有多个窗体,每个窗体上都有一些textbox和combobox,现在想在窗体里双击任意一个textbox或者combobox的时候对其进行清空(.text="")!怎样写成一样通用的过程然后可以在各个窗体里面调用?谢谢大家先!
展开
2个回答
展开全部
每个窗体的构造函数里注册文本框的双击事件
public partial class FormTest : Form
{
public FormTest()
{
InitializeComponent();
TextBox t;
foreach (Control control in this.Controls)
{
t = control as TextBox;
if (t != null)
{
t.DoubleClick += new EventHandler(Helper.ClearText);
}
}
}
}
在一个单独的类里进行处理
public class Helper
{
public static void ClearText(object sender, EventArgs e)
{
TextBox t = sender as TextBox;
Form f = t.FindForm();
foreach (Control ctl in f.Controls)
{
t = ctl as TextBox;
if (t != null)
{
t.Text = string.Empty;
}
}
}
}
不过如果用户一步小心点了双击,直接就清空所有文本框,不是什么好的设计
public partial class FormTest : Form
{
public FormTest()
{
InitializeComponent();
TextBox t;
foreach (Control control in this.Controls)
{
t = control as TextBox;
if (t != null)
{
t.DoubleClick += new EventHandler(Helper.ClearText);
}
}
}
}
在一个单独的类里进行处理
public class Helper
{
public static void ClearText(object sender, EventArgs e)
{
TextBox t = sender as TextBox;
Form f = t.FindForm();
foreach (Control ctl in f.Controls)
{
t = ctl as TextBox;
if (t != null)
{
t.Text = string.Empty;
}
}
}
}
不过如果用户一步小心点了双击,直接就清空所有文本框,不是什么好的设计
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询