Search code examples
c#focusgroupbox

issue with setting focus not working


I have a screen where the user enters in to a text box a name of a location. then click on a button to open a group box that has three textboxes Firstname, lastname and PW. I am trying to set the focus to the firstname text box when the user clicks the button to make the group box visible (on the main form). but it will not set the focus (txtbxnameFirst.focus();)


Solution

  • Requirement

    I understood the following:

    • user writes text into locationTextBox and clicks the button locationSaveButton

    • after clicking the button a GroupBoxControl should be set to visible and the firstNameTextBox shall receive the focus

    The code

    The button that is clicked after entereing the location locationButton has a Click Event with this code

    private void locationButton_Click(object sender, EventArgs e)
    {
        // get value from textbox location
        string location = locationTextBox.Text;
        // make the groupBox visible
        groupBox1.Visible = true;
        // set the focus to the textbox for the firstname
        firstNameTextBox.Focus();           
    }
    

    Simple Demo with windows forms

    This how the application looks before clicking the button

    Windows Forms

    After clicking it the groubBox is set to visible and the other TextBoxes are visible and the focus is set to the firstNameTextBox

    enter image description here