PRoblem with MDI child form

  • Thread starter Thread starter Tomas Andersson
  • Start date Start date
T

Tomas Andersson

I have a project with Form1
this form has a menu that opens a new child form called RotorTest
THe form Rotortest is opened as a MDI childform
Private Sub RotorToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles RotorToolStripMenuItem.Click
Dim NewMDIChild2 As New RotorTest
NewMDIChild2.MdiParent = Me
NewMDIChild2.Show()
End Sub

In the form RotorTEst there is a groupbox with a listbox and a button
Pressingf the button opens a sub in another module and runs a mysql query
the query loops and is supposed to add the DB information to the listbox.

Dim DR As MySqlDataReader = cmd.ExecuteReader
If DR.HasRows Then
DR.Read()
While (DR.Read())
RotorTest.SearchWindow.Items.Add(CStr(DR.GetString("ID"))
End While
End If
DR.Close()

Nothing i s added to the listbox.
Im guessing that I need to reference the specific childform but how do I do
this???
 
I have a project with Form1
this form has a menu that opens a new child form called RotorTest
THe form Rotortest is opened as a MDI childform
Private Sub RotorToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles RotorToolStripMenuItem.Click
Dim NewMDIChild2 As New RotorTest
NewMDIChild2.MdiParent = Me
NewMDIChild2.Show()
End Sub

In the form RotorTEst there is a groupbox with a listbox and a button
Pressingf the button opens a sub in another module and runs a mysql query
the query loops and is supposed to add the DB information to the listbox.

Dim DR As MySqlDataReader = cmd.ExecuteReader
If DR.HasRows Then
DR.Read()
While (DR.Read())
RotorTest.SearchWindow.Items.Add(CStr(DR.GetString("ID"))
End While
End If
DR.Close()

Nothing i s added to the listbox.
Im guessing that I need to reference the specific childform but how do I do
this???

Since this code is in a separate module, it knows nothing of the main
form or any child form. You will need to pass a reference of the child
form to the module function.

And if you ever plan to move to C#.NET you need to quit relying on
modules and the fact the form controls in VB are declared as Friend.
 
Tomas said:
I have a project with Form1
this form has a menu that opens a new child form called RotorTest
THe form Rotortest is opened as a MDI childform
Private Sub RotorToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
RotorToolStripMenuItem.Click Dim NewMDIChild2 As New RotorTest
NewMDIChild2.MdiParent = Me
NewMDIChild2.Show()
End Sub

In the form RotorTEst there is a groupbox with a listbox and a button
Pressingf the button opens a sub in another module and runs a mysql
query the query loops and is supposed to add the DB information to
the listbox.
Dim DR As MySqlDataReader = cmd.ExecuteReader
If DR.HasRows Then
DR.Read()
While (DR.Read())
RotorTest.SearchWindow.Items.Add(CStr(DR.GetString("ID"))
End While
End If
DR.Close()

Nothing i s added to the listbox.
Im guessing that I need to reference the specific childform but how
do I do this???


The problem is that MSFT unfortunatelly reintroduced "Form default
instances". This causes this problem.

'RotorTest' in the line

RotorTest.SearchWindow.Items.Add(CStr(DR.GetString("ID"))

refers to the Form default instance that you've never shown. You've created
your own instance and have shown it - which is absolutely ok. In the module,
you don't have access to your own instance. The solution is to move the code
from the module to the Form. The code is related to the Form, therefore it
is the better, object oriented approach. (or you can derive your own
listbox... but's that a different story)


Armin
 
Armin Zingler said:
The problem is that MSFT unfortunatelly reintroduced "Form default
instances". This causes this problem.

'RotorTest' in the line

RotorTest.SearchWindow.Items.Add(CStr(DR.GetString("ID"))

refers to the Form default instance that you've never shown. You've
created your own instance and have shown it - which is absolutely ok. In
the module, you don't have access to your own instance. The solution is to
move the code from the module to the Form. The code is related to the
Form, therefore it is the better, object oriented approach. (or you can
derive your own listbox... but's that a different story)


Armin


OK, Thanks.
 
Back
Top