【发布时间】:2016-05-23 06:26:42
【问题描述】:
我正在尝试将“txtMiles”文本框设置为焦点:
- 表单打开
- 单击“清除”按钮时
我尝试过使用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.Focus();仅在属性集 Tabindex 1 中以编程方式提供所需的输出