Home > Software engineering >  How can I change selected textBox in windowsForms app
How can I change selected textBox in windowsForms app

Time:01-16

I want users to be able to travel between textboxes and to a button by clicking Keys.Enter, Keys.Up, Keys.Down. Is there a way like this.SelectedItem = TxtBoxName or something?

    private void TxtBoxName_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            if(TextIsEmpty(TxtBoxDebt.Text))
            {
                //switch to TxtBoxDebt
            }
            else
            {
                AssignNewPerson();
            }
        }
        if (e.KeyCode == Keys.Down)
        {
            //switch to TxtBoxDebt
        }
    }
    private void TxtBoxDebt_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            AssignNewPerson();
        }
        else if (e.KeyCode == Keys.Up)
        {
            //switch to TxtBoxName
        }
        else if(e.KeyCode == Keys.Down)
        {
            //switch to TxtBoxNote
        }
    }
    private void TxtBoxNote_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            AssignNewPerson();
        }
        else if (e.KeyCode == Keys.Up)
        {
            //switch to TxtBoxDebt
        }
        else if (e.KeyCode == Keys.Down)
        {
            //switch to BtnAssign
        }
    }

CodePudding user response:

I think you are looking for the Focus() method, for example:

TxtBoxDebt.Focus();
  • Related