1
如何在用戶控件加載時將焦點置於文本框?在C#winform中加載用戶控件時,將焦點放在文本框中
在winforms中,我寫了textbox1.focus()
,在usercontrol.load()
但它沒有工作。
如何在用戶控件加載時將焦點置於文本框?在C#winform中加載用戶控件時,將焦點放在文本框中
在winforms中,我寫了textbox1.focus()
,在usercontrol.load()
但它沒有工作。
改爲使用.Select()方法。
textBox1.Select();
或
private void Form1_Load(object sender, EventArgs e)
{
this.ActiveControl = textBox1;
}
Alternativly你可以嘗試:
private TextBox TextFocusedFirstLoop()
{
// Look through all the controls on this form.
foreach (Control con in this.Controls)
{
// Every control has a Focused property.
if (con.Focused == true)
{
// Try to cast the control to a TextBox.
TextBox textBox = con as TextBox;
if (textBox != null)
{
return textBox; // We have a TextBox that has focus.
}
}
}
return null; // No suitable TextBox was found.
}
private void SolutionExampleLoop()
{
TextBox textBox = TextFocusedFirstLoop();
if (textBox != null)
{
// We have the focused TextBox.
// ... We can modify or check parts of it.
}
}
小心你的Tab鍵順序! – Joh