[英]C# set focus on textbox
我正在嘗試將“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();
}
}
}
在此先感謝您的幫助。
您應該確保您的 TabIndex 已設置,然后嘗試使用Select()
而不是Focus()
Select()
。 請參閱此MSDN 鏈接。
txtMiles.Select();
還要確保視圖文件中沒有設置TabStop = true
屬性。
它很舊,但有人可能需要這個。
Control.Focus()
被竊聽。 如果它不起作用,請嘗試解決方法:
this.SelectNextControl(_controlname, true, true, true, true);
更改函數參數,以便它們與您的控件一起使用,並記住您的控件的 TabStop = true 屬性。
單擊清除按鈕后,您已經將 txtMiles 設為焦點。 至於啟動,請在您的加載方法中設置 txtMiles.Focus()。
private void MilesToKilometers_Load(object sender, EventArgs e)
{
txtMiles.Focus();
}
使用此解決方案效果很好......
txtMiles.Select();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.