我想在TextBox
有焦点时添加一个偶数。我知道我可以用一个简单的textbox1.Focus
来做到这一点并检查布尔值......但我不想那样做。
以下是我想这样做的方式:
this.tGID.Focus += new System.EventHandler(this.tGID_Focus);
我不确定事件处理程序是否是执行此操作的正确方法,但我知道这不起作用。
您正在寻找 GotFocus 事件。还有一个LostFocus事件。
textBox1.GotFocus += textBox1_GotFocus;
this.tGID.GotFocus += OnFocus;
this.tGID.LostFocus += OnDefocus;
private void OnFocus(object sender, EventArgs e)
{
MessageBox.Show("Got focus.");
}
private void OnDefocus(object sender, EventArgs e)
{
MessageBox.Show("Lost focus.");
}
这应该执行您想要的操作,本文介绍了调用的不同事件以及顺序。您可能会看到更好的事件。
我对Hans Passant的评论投了赞成票,但这确实应该是一个答案。 我正在 3.5 .NET 环境中开发 Telerik UI,并且 RadTextBoxControl 上没有 GotFocus 事件。 我不得不使用 Enter 事件。
textBox1.Enter += textBox1_Enter;
以下是基于 Hans 的答案包装它并声明处理函数的方法。
namespace MyNameSpace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
txtSchedNum.Enter += new EventHandler(txtSchedNum_Enter);
}
protected void txtSchedNum_Enter(Object sender, EventArgs e)
{
txtSchedNum.Text = "";
}
}
}