0
votes

I have a project in Visual Studio 2019 (VB) with an MDI Parent form and several MDI Child forms. The Child forms were all created at design time rather than "on the fly" and they all interact with the parent and vice versa without any issues except for the following.

A problem I don't have in a non-MDI application is that when an MDI Child form is loaded I cannot set the focus to a text box on the Child form. In fact the whole form is deactivated which I proved by checking the deactivation event during load. I even added the following code to the deactivation event (it was already in the load event) and the event was called during load:

    Me.TopMost = True
    Me.Dock = DockStyle.Fill
    Me.Show()
    Me.Activate()
    Me.Focus()
    txtName.Focus()

The child form still shows as unfocused after load has finished. When I click on the child form it gets focus and shows the cursor flashing in the txtName text box so somehow the child form is being deactivated as soon as it loads. I checked the Sender as Object value in the Deactivation event which it shows as being the child form itself! I can't find any posts on this issue and I've ran out of ideas.

Can you help please.

1
Having Me.TopMost = True, Me.Dock = DockStyle.Fill and all the rest in the Load event of a MDIClhild is not exactly a good thing. Remove everything. If you want txtName to get focus when the Form is shown, set its TabIndex = 0 (while other Controls have a higher value) or set Me.ActiveControl = txtName. Show the code that's creating the Form and the code that's run after. -- You can set a Breakpoint in those lines of code, then proceed step by step to see what code is run while the Form is loading.Jimi

1 Answers

-1
votes

Here's a possible solution:

You can refer to the following code in form2(my MDI child form):

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.BeginInvoke(Sub()
                       TextBox1.Focus()
                   End Sub)
End Sub

Result of my test:

enter image description here