how to put focus on TextBox when the form load?

前端 未结 16 1163
清歌不尽
清歌不尽 2020-12-04 14:59

I have in my C# program textBox

I need that when the program start, the focus will be on the textBox

I try this on Form_Load:

MyTextBox.Focus         


        
16条回答
  •  再見小時候
    2020-12-04 15:29

    You cannot set focus to a control if it has not been rendered. Form.Load() occurs before the controls are rendered.

    Go to the form's events and double click the "Shown" event. In the form's shown event handler call the control.Focus() method.

        private void myForm_Shown(object sender, EventArgs e)
        {
            // Call textbox's focus method
            txtMyTextbox.Focus();
        }
    

提交回复
热议问题