Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 631
  • Last Modified:

MDI Child forms - reference wrong form!

Hello, im sure this is an easy answer, but I cant figure it out!  Help is much needed!

I have a MDI parent that creates child forms which have common events.  When an event triggers it references the wrong child form, for example:
I have 2 identical child forms, the listbox in one is empty, the other has data, if one form is in focus with the data in, and i click the button on the other form, which runs an event to check data in the listbox, it reads the data from the other forms listbox.  And from then on, if the other form is in focus, and i try and read the listbox, it still reads other form!
Sorry for all the info below, thought better to have in than not.  Im guessing I either need to create a class here, or have a way to refernce back to correct child form.  Heres the code from my MDI parent:

Imports System.Windows.Forms

Public Class MDIParent1

    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents CheckedListBox1 As System.Windows.Forms.CheckedListBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents ChildForm As New System.Windows.Forms.Form

    Private Sub ShowNewForm(ByVal sender As Object, ByVal e As EventArgs) Handles NewToolStripMenuItem.Click, NewToolStripButton.Click, NewWindowToolStripMenuItem.Click

        ChildForm = New System.Windows.Forms.Form
        ChildForm.MdiParent = Me

        m_ChildFormNumber += 1

        ChildForm.SuspendLayout()

        GroupBox1 = New System.Windows.Forms.GroupBox
        Button2 = New System.Windows.Forms.Button
        TextBox3 = New System.Windows.Forms.TextBox
        Label3 = New System.Windows.Forms.Label
        CheckedListBox1 = New System.Windows.Forms.CheckedListBox
        Label2 = New System.Windows.Forms.Label
        TextBox2 = New System.Windows.Forms.TextBox
        Button1 = New System.Windows.Forms.Button
        TextBox1 = New System.Windows.Forms.TextBox
        Label1 = New System.Windows.Forms.Label
        Button3 = New System.Windows.Forms.Button
        OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog

        GroupBox1.Controls.Add(Button3)
        GroupBox1.Controls.Add(Button2)
        GroupBox1.Controls.Add(TextBox3)
        GroupBox1.Controls.Add(Label3)
        GroupBox1.Controls.Add(CheckedListBox1)
        GroupBox1.Controls.Add(Label2)
        GroupBox1.Controls.Add(Label6)
        GroupBox1.Controls.Add(TextBox2)
        GroupBox1.Controls.Add(Button1)
        GroupBox1.Controls.Add(TextBox1)
        GroupBox1.Controls.Add(Label1)
        GroupBox1.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        GroupBox1.ForeColor = System.Drawing.Color.DarkBlue
        GroupBox1.Location = New System.Drawing.Point(8, 0)
        GroupBox1.Name = "GroupBox1"
        GroupBox1.Size = New System.Drawing.Size(368, 408)
        GroupBox1.TabIndex = 7
        GroupBox1.TabStop = False
        '
        'Button2
        '
        Button2.BackColor = System.Drawing.Color.Gainsboro
        Button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Button2.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Button2.ForeColor = System.Drawing.Color.DarkBlue
        Button2.Location = New System.Drawing.Point(328, 88)
        Button2.Name = "Button2"
        Button2.Size = New System.Drawing.Size(32, 21)
        Button2.TabIndex = 17
        Button2.Text = "..."
        Button2.UseVisualStyleBackColor = False

        'TextBox3
        '
        TextBox3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        TextBox3.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        TextBox3.ForeColor = System.Drawing.Color.DarkBlue
        TextBox3.Location = New System.Drawing.Point(8, 88)
        TextBox3.Name = "TextBox3"
        TextBox3.Size = New System.Drawing.Size(320, 21)
        TextBox3.TabIndex = 16
        '
        'Label3
        '
        Label3.AutoSize = True
        Label3.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Label3.ForeColor = System.Drawing.Color.DarkBlue
        Label3.Location = New System.Drawing.Point(8, 72)
        Label3.Name = "Label3"
        Label3.Size = New System.Drawing.Size(128, 13)
        Label3.TabIndex = 15
        Label3.Text = "Enter path to listbox items file:"
        '
        'CheckedListBox1
        '
        CheckedListBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        CheckedListBox1.ForeColor = System.Drawing.Color.DarkBlue
        CheckedListBox1.FormattingEnabled = True
        CheckedListBox1.Location = New System.Drawing.Point(8, 144)
        CheckedListBox1.Name = "CheckedListBox1"
        CheckedListBox1.Size = New System.Drawing.Size(216, 162)
        CheckedListBox1.TabIndex = 14
        '
        'Label2
        '
        Label2.AutoSize = True
        Label2.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Label2.ForeColor = System.Drawing.Color.DarkBlue
        Label2.Location = New System.Drawing.Point(8, 128)
        Label2.Name = "Label2"
        Label2.Size = New System.Drawing.Size(155, 13)
        Label2.TabIndex = 13
        Label2.Text = "Choose fields to report on:"
        '
        'TextBox2
        '
        TextBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        TextBox2.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        TextBox2.ForeColor = System.Drawing.Color.DarkBlue
        TextBox2.Location = New System.Drawing.Point(8, 344)
        TextBox2.Name = "TextBox2"
        TextBox2.Size = New System.Drawing.Size(32, 21)
        TextBox2.TabIndex = 10
        TextBox2.Text = "10"
        '
        'Button1
        '
        Button1.BackColor = System.Drawing.Color.Gainsboro
        Button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Button1.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Button1.ForeColor = System.Drawing.Color.DarkBlue
        Button1.Location = New System.Drawing.Point(328, 32)
        Button1.Name = "Button1"
        Button1.Size = New System.Drawing.Size(32, 21)
        Button1.TabIndex = 8
        Button1.Text = "..."
        Button1.UseVisualStyleBackColor = False
        '
        'TextBox1
        '
        TextBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        TextBox1.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        TextBox1.ForeColor = System.Drawing.Color.DarkBlue
        TextBox1.Location = New System.Drawing.Point(8, 32)
        TextBox1.Name = "TextBox1"
        TextBox1.Size = New System.Drawing.Size(320, 21)
        TextBox1.TabIndex = 7
        '
        'Label1
        '
        Label1.AutoSize = True
        Label1.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Label1.ForeColor = System.Drawing.Color.DarkBlue
        Label1.Location = New System.Drawing.Point(8, 16)
        Label1.Name = "Label1"
        Label1.Size = New System.Drawing.Size(199, 13)
        Label1.TabIndex = 6
        Label1.Text = "Enter path other file:"
        '
        'Button3
        '
        Button3.AutoEllipsis = True
        Button3.BackColor = System.Drawing.Color.Gainsboro
        Button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Button3.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Button3.Location = New System.Drawing.Point(8, 376)
        Button3.Name = "Button3"
        Button3.Size = New System.Drawing.Size(160, 21)
        Button3.TabIndex = 18
        Button3.Text = "Search"
        Button3.UseVisualStyleBackColor = False
        '
        'Form1
        '
        ChildForm.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        ChildForm.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        ChildForm.BackColor = System.Drawing.Color.White
        ChildForm.ClientSize = New System.Drawing.Size(382, 414)
        ChildForm.Controls.Add(GroupBox1)
        ChildForm.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        ChildForm.Text = "Configure Form " & m_ChildFormNumber
        ChildForm.MaximizeBox = False
        ChildForm.MinimizeBox = False
        ChildForm.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
        GroupBox1.ResumeLayout(False)
        GroupBox1.PerformLayout()
        ChildForm.ResumeLayout(False)




        AddHandler Button2.Click, AddressOf Button2_Click
        AddHandler Button1.Click, AddressOf Button1_Click
        AddHandler Button3.Click, AddressOf Button3_Click



        ChildForm.Show()


    End Sub


    Public m_ChildFormNumber As Integer = 0
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim objReader

        OpenFileDialog1.Title = "Open List"
        OpenFileDialog1.Filter = "text files (*.txt)|*.txt"

        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
            OpenFileDialog1.Dispose()
            Exit Sub
        End If


        If OpenFileDialog1.FileName = "" Then Exit Sub

        TextBox3.Text = OpenFileDialog1.FileName

        objReader = New IO.StreamReader(TextBox3.Text)

        Do While Not objReader.EndOfStream
            CheckedListBox1.Items.Add(objReader.ReadLine)
        Loop

        objReader.Dispose()


    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim objReader

        OpenFileDialog1.Title = "Open  file"
        OpenFileDialog1.Filter = "Text files (*.txt)|*.txt"

        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
            OpenFileDialog1.Dispose()
            Exit Sub
        End If

        If OpenFileDialog1.FileName = "" Then Exit Sub

        TextBox1.Text = OpenFileDialog1.FileName

        numlines = 0

        objReader = New IO.StreamReader(TextBox1.Text)

        Do While Not objReader.EndOfStream
            numlines = numlines + 1
            objReader.readLine()
        Loop

        objReader.Dispose()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim i, confirmit, carryon

        confirmit = ""

        For i = 0 To CheckedListBox1.CheckedItems.Count - 1
            confirmit = confirmit & CheckedListBox1.CheckedItems(i) & vbCrLf
        Next


        If confirmit = "" Then
            MsgBox("No fields selected!", MsgBoxStyle.Critical)
            Exit Sub
        End If

        carryon = MsgBox("You are about to search:" & vbCrLf & vbCrLf & confirmit & vbCrLf & vbCrLf & "Do you want to proceed?", MsgBoxStyle.YesNo, "Proceed?")

        If carryon = vbNo Then Exit Sub

    End Sub

End Class
0
marklye
Asked:
marklye
  • 2
1 Solution
 
marklyeAuthor Commented:
sorry, just to update...it appears that every event triggered, actions items on the last child form that was created...so it looks like th eevents overwrite each other and only one set of events is valid (for the last form)?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
That is correct.  When you declare something as "WithEvents", it only references the last control assigned to it:

    Friend WithEvents Button1 As System.Windows.Forms.Button

Why are you building the form manually like that?

Build the form in the IDE as a seperate form, put the code in it, then create new instances of it...


0
 
marklyeAuthor Commented:
Dammit.  Thought this was the way you create new forms on the fly.  Didnt know you could create new forms like that!

Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now