Farhan Mukadam
Farhan Mukadam

Reputation: 470

Disable controls of MDI form when child is open

I have an MDIParent Form named MainForm. It has a child form named SelectDB. In the SelectDB form I have a button which when clicked opens FileDialog to browse and select a .MDB file. The path of the file is displayed in a TextBox. After I get the .MDB file the Child form closes and the MainForm is visible. On the MainForm I have a Label which should display the path I got in Textbox. And moreover, when the Child Form is open. The Menu Strip should also be disable, or you can say all the controls in the MDI Form should be disabled.

enter image description here

My MainForm.vb (MDIParent) code is like this:

Imports System.Windows.Forms

Public Class MainForm

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        sDB.MdiParent = Me
        sDB.Show()
    End Sub

    Public Sub Formload()
        msMenu.Enabled = True
        lblPath.Text = OG.GetValue("DBPath")
        Me.Refresh()
    End Sub
End Class

My SelectDB.vb (Child form) code is like this:

Public Class SelectDB    

    Private Sub btnBrowse_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        Dim dlg As New OpenFileDialog
        dlg.DefaultExt = ".txt"
        dlg.Filter = "MDB Files (*.mdb)|*.mdb"
        Dim result As Nullable(Of Boolean) = dlg.ShowDialog()
        If result = True Then
            Dim filename As String = dlg.FileName
            txtPath.Text = filename
        End If
        OG.SetValue("DBPath", txtPath.Text)
        main.Formload()
        Me.Hide()
    End Sub

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Application.Exit()
    End Sub

    Private Sub SelectDB_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Application.Exit()
    End Sub

End Class

Please help me with this.

Upvotes: 2

Views: 4486

Answers (2)

Leon
Leon

Reputation: 919

There are several options for what you're trying to achieve and (what Hans Passant already mentions) is the most easy one: use ShowDialog() not Show.

Second option: first of all you could create a public method in the main form and call that method from the client on opening and closing. Like:

[main form]

public void ChildControls(bool IsEnabled)
{
  msMenu.Enabled = IsEnabled;
}

[child]

 private void child_Activated(object sender, EventArgs e)
    {
        if (this.MdiParent != null)
            ((mainForm)this.MdiParent).ChildControls(false);
    }
    private void child_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (this.MdiParent != null)
            ((mainForm)this.MdiParent).ChildControls(true);
    }

Third option:

Use the mainform' MdiChildActivate event. When the ActiveMdiChild == null you could enable the controls again. But when you're using MdiChildActivate and you have more (and different) Mdi (child-) forms you should perform a type-check before (de-) activating the controls.

Upvotes: 0

C Sharper
C Sharper

Reputation: 8646

Place me.dispose() in place of me.hide() . This will help you to do the intended work.

Upvotes: 2

Related Questions