[英]C# set focus on textbox
I'm trying to set the "txtMiles" textbox to focus after:我正在尝试将“txtMiles”文本框设置为聚焦:
I have tried using txtMiles.Focus();
我试过使用txtMiles.Focus();
but it doesn't seem to work for me.但它似乎对我不起作用。
CODE BEING USED ON THIS FORM本表格中使用的代码
private void btnConvert_Click(object sender, EventArgs e)
{
//assigns variable in memory.
double txtMile = 0;
double Results;
try
{
// here is where the math happens.
txtMile = double.Parse(txtMiles.Text);
Results = txtMile * CONVERSION;
lblResults.Text = Results.ToString("n2");
txtMiles.Focus();
}
// if the user enters an incorrect value this test will alert them of such.
catch
{
//MessageBox.Show (ex.ToString());
MessageBox.Show("You entered an incorrect value");
txtMiles.Focus();
}
}
private void btnClear_Click(object sender, EventArgs e)
{
//This empties all the fields on the form.
txtMiles.Text = "";
txtMiles.Focus();
lblResults.Text = "";
}
private void btnExit_Click(object sender, EventArgs e)
{
// closes program
this.Close();
}
}
}
Thanks in advance for the help.在此先感谢您的帮助。
You should make sure your TabIndex is set and then instead of Focus()
, try use Select()
.您应该确保您的 TabIndex 已设置,然后尝试使用Select()
而不是Focus()
Select()
。 See this MSDN link .请参阅此MSDN 链接。
txtMiles.Select();
Also make sure there isn't a TabStop = true
attribute set in the view file.还要确保视图文件中没有设置TabStop = true
属性。
It's old, but someone could need this.它很旧,但有人可能需要这个。
Control.Focus()
is bugged. Control.Focus()
被窃听。 If it's not working, try workaround:如果它不起作用,请尝试解决方法:
this.SelectNextControl(_controlname, true, true, true, true);
Change function parameters so they will work with your control, and remember about TabStop = true property of your control.更改函数参数,以便它们与您的控件一起使用,并记住您的控件的 TabStop = true 属性。
You already have your txtMiles focused after the clear-button click.单击清除按钮后,您已经将 txtMiles 设为焦点。 As for the Startup, set txtMiles.Focus() in your load-method.至于启动,请在您的加载方法中设置 txtMiles.Focus()。
private void MilesToKilometers_Load(object sender, EventArgs e)
{
txtMiles.Focus();
}
using this solution worked perfectly...使用此解决方案效果很好......
txtMiles.Select(); txtMiles.Select();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.