none
How can I access MDI menu properties in MDI Child forms

    Question

  • How can I access MDI menu properties in MDI Child forms

    Please reply as soon as possible I have to give this application on monday and I dont know how to access one form control properties in another form.


    Thanking you
    Saturday, August 30, 2008 12:19 PM

Answers

  • This should be quite straighforward to setup.

     

    Firstly, when the user clicks the Log-In menu item, I think this should display a Log-In dialog as opposed to a MDI child form. Then, if the user is successfully authenticated, this dialog will close and you will then create an MDI child form.

     

    So concentrating on the Log-In dialog first of all, which should not have a menu, you just need to call ShowDialog instead of Show to display the dialog.

     

    This would normally have Ok and Cancel buttons and for the Click handler of the OK button you would do the authentication. To be on the safe side, instead of simply setting DialogResult to indicate whether authentication was successful, I would instread call an Authenticate method in the parent form which sets an internal flag indicating whether the user is authenticated.

     

    To code this, you just need to setup an extra new method in the Log-in form, which has your main form as an extra parameter:

     

    Code Snippet

    Public Sub New(ByVal mainForm As MainForm)


    _mainForm = mainForm


    End Sub 

     

    Private Sub okButton_Click(ByVal sender As Object, ByVal e As EventArgs)

     

    userNameTextBox.Text = userNameTextBox.Text.Trim()

    passwordTextBox.Text = passwordTextBox.Text.Trim()

     

    If _mainForm.Authenticate(userNameTextBox.Text, passwordTextBox.Text) Then

    Me.Close

    Else

    MessageBox.Show("Invalid user name and or password")

    End If

     

    End Sub 
        
    Private _mainForm As MainForm

     

     

    Then in your main form, you could have the following code:

     

    Code Snippet

    Private _authenticated As Boolean = False

     

    Friend Function Authenticate(ByVal userName As String, ByVal password As String) As Boolean


         ' Dummy authentication

    _authenticated = (userName = "Hello" AndAlso password = "There") 
        
    Return _authenticated


    End Function

     

    Private Sub logInMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)


        ' Authenticate user
        Dim form As New LogInForm(Me)
        form.ShowDialog()
       
        ' If authentication failed, abort
        If Not _authenticated Then
            Return
        End If
       
        ' Create child MDI form
       
    End Sub

     

     

    Does this sound good so far? I just want to check I haven't lost you.

     

     

    Sunday, August 31, 2008 8:24 PM
  • Usually, when a form opens at runtimw with no controls on it is because you didn't call InitializeComponent() from the constructor.

    hth,
    Lior.
    Monday, September 01, 2008 11:39 AM
  • Oops, Lior is right; the added new routine needs to call the original new routine :

     

    Code Snippet
    Public Sub New(ByVal mainForm As Form) 
         Me.New() 
        _mainForm = mainForm 
    End Sub 
        
    Public Sub New() 
        InitializeComponent() 
    End Sub

     

     

    How are you finding C# (my native language)? From what I have seen, C# seems, if anything an easier language to work with.

     

    So in C# you would have something like the following in your LogInForm:

     

    Code Snippet

    public LogInForm(Form mainForm)

    : this()

    {

    _mainForm = mainForm;

    }

     

    public LogInForm()

    {

    InitializeComponent();

    }

     

     

    Ok, so continuing the thread, you can get the menu you outlined, when the child MDI form is shown, using menu merging (as outlined in my previous post).

     

    Essentially, in the menu strip of the child you include the extra menu items you want to appear and the menu items you want to remove.

     

    So in your case you would setup your child's File menu items as follows:

     

    Menu Item MergeAction MergeIndex
    Log - In Remove -1
    Registration Insert 2
    (Seperator) Insert 3
    Course Insert 4
    Module Insert 5
    Subject Insert 6

     

    Ideally though, how should this work from the users point of view?

     

    Once they've logged in, do they just register once? If so, you'll want to check whether they've already registerd and if so, remove the Registration and Seperator menu items dynamically, which I should be able to help you with.

    Monday, September 01, 2008 3:50 PM

All replies

  • I've done plently of MDI in MFC but haven't needed an MDI app yet in .Net yet, so I thought I would give this a go.

     

    I had a spot of trouble though because some of the MSDN documentation is out-of-date. See my comment:

    Creating MDI Applications - MSDN documentation.

     

    Your answer depends on what you are trying to do. Say you want to gray a child menu item;  where you have setup the child menu to merge with the parent menu, you can still use the child menu instances created by the Designer. For example I added a button to my child form and this is the code for it's click handler:

     

    private void button1_Click(object sender, EventArgs e)

    {

    // Toggle whether Clear menu item is enabled

    clearToolStripMenuItem.Enabled = !clearToolStripMenuItem.Enabled;

    }

     

    To do things with the parent menu items, you use the MainMenuStrip property through the child form's MdiParent property. This is not behaving quite how I would expect though...

     

    You may be able to achieve what you want by just setting your child form menu item's to merge with a parent form's menu item (File etc).

     

    Please provide more details if you are unsure on how to proceed.

     

    Saturday, August 30, 2008 5:25 PM
  • Thanks for replying


    Exactly I want to disable MDI Menu Items from the child form close button.
    Saturday, August 30, 2008 8:51 PM
  • Sounds like you want to do this because the menu item is only applicable when the child form has focus.

     

    This is possible, but you may want to consider the usual approach, which is to define the menu items you want in the child form's menu and use menu merging to merge this with the parent.

     

    For example, say the parent has the following File menu items:

     

    New

    Close

     

    And the child has the following File menu items (the lines are seperators):

     

    ------

    Print

    ------

     

    When the child form has focus, to get the Print menu item to appear in between New & Close, you would do the following:

    1. Set the Child's File menu item MergeAction property  to MatchOnly.

    2. Set the Child's first seperator menu item MergeAction to Insert and the MergeIndex to 1 (number from insertion point in Parent menu).

    3. Set the Child's Print menu item MergeAction to Insert and the MergeIndex to 2.

    4. Set the Child's last seperator menu item MergeAction to Insert and the MergeIndex to 3.
    When the child has focus, you should have the File meniu:
     
    New

    ------

    Print

    ------

    Close

     

    Of course, you don't have to use seperators.

     

    NOTE: You must use exactly the same name for the File menu in the Parent and the Child, e.g. if you had the Parent menu item as &File and the Child menu item as just File, they wouldn't merge.

     

    I've found the child meniu is not hidden after the merge. So, to hide the child menu, you just need to add a Load event handler (in Designer etc) for the child form and set the child menu's Visible property to false, e.g.

     

    // C#

    private void Form2_Load(object sender, EventArgs e)

    {

    menuStrip1.Visible = false;

    }

     

    Here's the same code in VB:

     

    Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs)

       
        menuStrip1.Visible = False
       
    End Sub

     

    Also, to hide the pesky System Menu that is inserted as the first menu item, add an event handler to the Parent menu for the ItemAdded event handler and code it as follows:

     

    // C#

    private void menuStrip1_ItemAdded(object sender, ToolStripItemEventArgs e)

    {

    if (e.Item.GetType().Name == "SystemMenuItem")

    {

    e.Item.Visible = false;

    }

    }

     

    Here's the same code in VB:

     

    Private Sub menuStrip1_ItemAdded(ByVal sender As Object, ByVal e As ToolStripItemEventArgs)
        If e.Item.[GetType]().Name = "SystemMenuItem" Then
            e.Item.Visible = False
        End If
    End Sub

    Sunday, August 31, 2008 2:50 AM


  • I have file menu items in my mdi form as

    File
    -------------
    Log - In
    --------------
    Exit
    ---------------



    after click on Log-In  , Log -in form will be appeared. If user is authenticated with database with correct user and password.


    I want want to visible this menu item on mdi form which is invisible before.


    File
    -----------------
    Log - In
    -----------------
    Registration
    ------------------
    Course
    Module
    Subject
    -------------------
    Exit
    -------------------


    on the basis of user authentication . I don't know how to do this I have never developed MDI application before using dotnet.


    Please help me out with this problem I am very confused . I was developing in vb6.0 its so easy there but here I am not getting how to go for this.

    Thanks
    Sunday, August 31, 2008 7:16 PM
  • This should be quite straighforward to setup.

     

    Firstly, when the user clicks the Log-In menu item, I think this should display a Log-In dialog as opposed to a MDI child form. Then, if the user is successfully authenticated, this dialog will close and you will then create an MDI child form.

     

    So concentrating on the Log-In dialog first of all, which should not have a menu, you just need to call ShowDialog instead of Show to display the dialog.

     

    This would normally have Ok and Cancel buttons and for the Click handler of the OK button you would do the authentication. To be on the safe side, instead of simply setting DialogResult to indicate whether authentication was successful, I would instread call an Authenticate method in the parent form which sets an internal flag indicating whether the user is authenticated.

     

    To code this, you just need to setup an extra new method in the Log-in form, which has your main form as an extra parameter:

     

    Code Snippet

    Public Sub New(ByVal mainForm As MainForm)


    _mainForm = mainForm


    End Sub 

     

    Private Sub okButton_Click(ByVal sender As Object, ByVal e As EventArgs)

     

    userNameTextBox.Text = userNameTextBox.Text.Trim()

    passwordTextBox.Text = passwordTextBox.Text.Trim()

     

    If _mainForm.Authenticate(userNameTextBox.Text, passwordTextBox.Text) Then

    Me.Close

    Else

    MessageBox.Show("Invalid user name and or password")

    End If

     

    End Sub 
        
    Private _mainForm As MainForm

     

     

    Then in your main form, you could have the following code:

     

    Code Snippet

    Private _authenticated As Boolean = False

     

    Friend Function Authenticate(ByVal userName As String, ByVal password As String) As Boolean


         ' Dummy authentication

    _authenticated = (userName = "Hello" AndAlso password = "There") 
        
    Return _authenticated


    End Function

     

    Private Sub logInMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)


        ' Authenticate user
        Dim form As New LogInForm(Me)
        form.ShowDialog()
       
        ' If authentication failed, abort
        If Not _authenticated Then
            Return
        End If
       
        ' Create child MDI form
       
    End Sub

     

     

    Does this sound good so far? I just want to check I haven't lost you.

     

     

    Sunday, August 31, 2008 8:24 PM
  • Thanks Friend

    I have tried in c#  I have converted whole this code in c# language. but when I run this it show me blank form with no controls.


    Monday, September 01, 2008 10:41 AM
  • Usually, when a form opens at runtimw with no controls on it is because you didn't call InitializeComponent() from the constructor.

    hth,
    Lior.
    Monday, September 01, 2008 11:39 AM
  • Oops, Lior is right; the added new routine needs to call the original new routine :

     

    Code Snippet
    Public Sub New(ByVal mainForm As Form) 
         Me.New() 
        _mainForm = mainForm 
    End Sub 
        
    Public Sub New() 
        InitializeComponent() 
    End Sub

     

     

    How are you finding C# (my native language)? From what I have seen, C# seems, if anything an easier language to work with.

     

    So in C# you would have something like the following in your LogInForm:

     

    Code Snippet

    public LogInForm(Form mainForm)

    : this()

    {

    _mainForm = mainForm;

    }

     

    public LogInForm()

    {

    InitializeComponent();

    }

     

     

    Ok, so continuing the thread, you can get the menu you outlined, when the child MDI form is shown, using menu merging (as outlined in my previous post).

     

    Essentially, in the menu strip of the child you include the extra menu items you want to appear and the menu items you want to remove.

     

    So in your case you would setup your child's File menu items as follows:

     

    Menu Item MergeAction MergeIndex
    Log - In Remove -1
    Registration Insert 2
    (Seperator) Insert 3
    Course Insert 4
    Module Insert 5
    Subject Insert 6

     

    Ideally though, how should this work from the users point of view?

     

    Once they've logged in, do they just register once? If so, you'll want to check whether they've already registerd and if so, remove the Registration and Seperator menu items dynamically, which I should be able to help you with.

    Monday, September 01, 2008 3:50 PM
  • Thanks Lior and denial

    I got it what I want .

    Thank you very much
    Wednesday, September 03, 2008 5:09 AM