Dar*_*ght 43 .net c# textbox focus winforms
我想知道为什么这段代码无法集中文本框......?
private void sendEmail_btn_Click(object sender, EventArgs e)
{
String sendTo = recipientEmail_tbx.Text.Trim();
if (!IsValidEmailAddress(sendTo))
{
MessageBox.Show("Please Enter valid Email address","Cognex" MessageBoxButtons.OK, MessageBoxIcon.Error);
recipientEmail_tbx.Focus();
}
}
Run Code Online (Sandbox Code Playgroud)
Dan*_*lba 104
Select()
改为使用:
recipientEmail_tbx.Select();
Run Code Online (Sandbox Code Playgroud)
Focus是一种低级方法,主要用于自定义控件作者.相反,应用程序员应该对子控件使用Select方法或ActiveControl属性,或者对表单使用Activate方法.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx
小智 6
添加一些延迟miliSec
。延迟然后打电话Focus()
,不要忘记放进去Dispatcher
。
Task.Delay(100).ContinueWith(_ =>
{
Application.Current.Dispatcher.Invoke(new Action(() =>
{
TextBoxNAme.Focus();
}));
});
Run Code Online (Sandbox Code Playgroud)