calling reference of the current mdi child?

ethicalhacker

Well-known member
Joined
Apr 22, 2007
Messages
142
Location
Delhi,India
Programming Experience
5-10
how do i call the current active mdi form
i have this code but its not working, it says MDIChildform not defined
VB.NET:
Public Class MDIParent1

    Private Sub ShowNewForm(ByVal sender As Object, ByVal e As EventArgs) Handles NewToolStripMenuItem.Click, NewToolStripButton.Click, NewWindowToolStripMenuItem.Click
        ' Create a new instance of the child form.
        Dim MDIChildForm As New Form1()
        MDIChildForm.MdiParent = Me
        m_ChildFormNumber += 1
        MDIChildForm.Text = "Untitled " & m_ChildFormNumber
        MDIChildForm.WindowState = FormWindowState.Maximized
        MDIChildForm.Show()
    End Sub

    Private Sub OpenFile(ByVal sender As Object, ByVal e As EventArgs) Handles OpenToolStripMenuItem.Click, OpenToolStripButton.Click
        Dim OpenFileDialog As New OpenFileDialog

        OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        OpenFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) And (OpenFileDialog.FileName.Length > 0) Then
            Dim FileName As String = OpenFileDialog.FileName
            Dim child As MDIChildForm = TryCast(Me.ActiveMdiChild, ChildForm)
            child.RichTextBox1.LoadFile(OpenFileDialog.FileName, _
                                RichTextBoxStreamType.PlainText)
        End If
    End Sub
 
You declare a variable of type Form1 when you create the child. In your OpenFile method you do strange things; declaring a variable of type MDIChildForm, then assign it the ActiveMdiChild you first try to cast to ChildForm type. What type is it? Form1? MDIChildForm? ChildForm?
 
Back
Top