C# 在WinC窗体中加载用户控件时,将焦点放在文本框中
加载用户控件时如何设置文本框的焦点C# 在WinC窗体中加载用户控件时,将焦点放在文本框中,c#,winforms,user-controls,load,C#,Winforms,User Controls,Load,加载用户控件时如何设置文本框的焦点 在winforms中,我在usercontrol.load中编写了textbox1.focus,但它不起作用。试试。改为选择方法 textBox1.Select(); 或 或者,您可以尝试: private TextBox TextFocusedFirstLoop() { // Look through all the controls on this form. foreach (Control con in this.Controls)
在winforms中,我在usercontrol.load中编写了textbox1.focus,但它不起作用。试试。改为选择方法
textBox1.Select();
或
或者,您可以尝试:
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.
}
}
请尝试。改为选择方法
textBox1.Select();
或
或者,您可以尝试:
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.
}
}
小心你的账单!小心你的账单!