C# 将焦点设置在文本框上
C# set focus on textbox
我正在尝试将 "txtMiles" 文本框设置为聚焦于:
- 表格打开
- 单击 "clear" 按钮时
我试过使用 txtMiles.Focus();
但它似乎对我不起作用。
此表单中使用的代码
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();
}
}
}
在此先感谢您的帮助。
点击清除按钮后,您的 txtMiles 已经集中。至于启动,请在加载方法中设置 txtMiles.Focus()。
private void MilesToKilometers_Load(object sender, EventArgs e)
{
txtMiles.Focus();
}
您应确保已设置 TabIndex,然后尝试使用 Select()
而不是 Focus()
。看到这个 MSDN link.
txtMiles.Select();
还要确保视图文件中没有设置 TabStop = true
属性。
它很旧,但有人可能需要它。
Control.Focus()
被窃听了。如果它不起作用,请尝试解决方法:
this.SelectNextControl(_controlname, true, true, true, true);
更改函数参数以便它们与您的控件一起工作,并记住您的控件的 TabStop = true 属性。
使用此解决方案非常有效...
txtMiles.Select();
我正在尝试将 "txtMiles" 文本框设置为聚焦于:
- 表格打开
- 单击 "clear" 按钮时
我试过使用 txtMiles.Focus();
但它似乎对我不起作用。
此表单中使用的代码
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();
}
}
}
在此先感谢您的帮助。
点击清除按钮后,您的 txtMiles 已经集中。至于启动,请在加载方法中设置 txtMiles.Focus()。
private void MilesToKilometers_Load(object sender, EventArgs e)
{
txtMiles.Focus();
}
您应确保已设置 TabIndex,然后尝试使用 Select()
而不是 Focus()
。看到这个 MSDN link.
txtMiles.Select();
还要确保视图文件中没有设置 TabStop = true
属性。
它很旧,但有人可能需要它。
Control.Focus()
被窃听了。如果它不起作用,请尝试解决方法:
this.SelectNextControl(_controlname, true, true, true, true);
更改函数参数以便它们与您的控件一起工作,并记住您的控件的 TabStop = true 属性。
使用此解决方案非常有效...
txtMiles.Select();