首页 > 解决方案 > How do I focus on a form after entering a textbox

问题描述

I'm not sure if focus is the right word but I have an on key up event on my form that will open a new form and close the current form, however after i enter a textbox or other such object i can't re select the form to be able to activate the key up event

This is code a I am using currently when i click on my form, to try select my currently open form however it does not close the current form when i active the key up event, when i do it this way

private void frmLevel1_Click(object sender, EventArgs e)
{
   this.BackColor = GlobalClass.BG;
   frmLevel1 lvl1 = new frmLevel1();
   lvl1.Select();
}

标签: c#visual-studiowinforms

解决方案


我不是 100% 肯定,但你有几个不同的问题。我希望它有所帮助。

// Button click event
private void button1_Click(object sender, EventArgs e)
{
    // Focus on textbox
    this.ActiveControl = textBox1;
}

// Form load event
private void Form1_Load(object sender, EventArgs e)
{
    // Focus on textbox
    this.ActiveControl = textBox1;
}

// Close the current form and open another one. Use any event what you want
private void button1_Click(object sender, EventArgs e)
{
    using (Form2 frm = new Form2())
    {
        // Hide the current form. If you close it it will dispose of all further events
        this.Hide();

        // Open the new form
        frm.Show();

        // Close the current form
        this.Close();
    }
}

推荐阅读