【问题标题】:put focus in textbox when user control is load in C# winform在 C# winform 中加载用户控件时将焦点放在文本框中
【发布时间】:2012-11-17 07:23:12
【问题描述】:

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

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

【问题讨论】:

  • 小心你的标签顺序!

标签: c# winforms user-controls load


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 2010-11-26
    相关资源
    最近更新 更多