c# - 在C#winform中加载用户控件时,将焦点放在文本框中

标签 c# winforms user-controls load

加载用户控件时,如何将焦点设置在文本框上?

在Winforms中,我在textbox1.focus()中写了usercontrol.load(),但是没有用。

最佳答案

请尝试.Select()方法。

textBox1.Select();


要么

private void Form1_Load(object sender, EventArgs e)
{
    this.ActiveControl = textBox1;
}


另外,您可以尝试:

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.
    }
}

关于c# - 在C#winform中加载用户控件时,将焦点放在文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13428636/

相关文章:

wpf - 为什么将无操作转换器放在 Binding 上会改变其行为?

c# - 尝试在 WPF 中构建查询生成器控件

c# - ASP.NET 核心 WebSocket

c# - 在 Windows 窗体上绘制单个像素

c# - C# Winforms 中的文本框验证 - 应仅允许 1-100 之间的数字

c# - 用户控件在表单内进行通信的最佳方式

wpf - ItemsControl 按钮单击命令

c# - VSTO MS Word 2003 加载项导致错误 "Word experienced a serious error the last time the add-in ' XYZ' 已打开”

c# - 使用精确格式的 Sql 转换日期

c# - .Net 4 : Easy way to dynamically create List<Tuple<. ..>> 结果