CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2009
    Posts
    3

    Question Access a MainMenuStrip subitem from a child form

    Hi Guys,

    I have the following problem.
    In my Form1 (IsMdiContainer = true) I have added a MenuStrip with the following structure:

    System
    System->Log In
    System->Register
    System->Log Out(Visible = false)
    Forms(Enabled=False)
    Forms->Table1
    Forms->Table2
    Statistics
    Statistics->Blablabl
    Statistics->...

    From my Log In button I am displaying a form that logs me in the system:
    Code:
    #include "LogInForm.h"
    ...
    ...
    private: System::Void logInToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
    	LogInForm^form=gcnew LogInForm();
       form->MdiParent = this;
       form->Show();
    			 }
    In this form when correctly logged in I want to make the System->Log In and System->Register menu items visible = false, the Statistics and Forms menu items enabled.
    Here is the code of the login button on successful logging:
    Code:
    this->MdiParent->MainMenuStrip->Items["formsToolStripMenuItem"]->Enabled = true;
    this->MdiParent->MainMenuStrip->Items["statisticsToolStripMenuItem"]->Enabled = true;
    this->Close();
    Forms and Statistics menu items are correctly being enabled. However with this I am not able to make the System->Log In menu item visible = false;
    Code:
    this->MdiParent->MainMenuStrip->Items["logInToolStripMenuItem"]->Visible = true;
    Can you please help me out. It is quite urgent.

    Additional information you might need - I am using MS Visual Studio 2005 and 2008 is not an option here.

    Thanks in advance!

    Best,
    Wisher

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Access a MainMenuStrip subitem from a child form

    Accessing form controls this way is not recommended - it's breaking encapsulation.

    Large UIs can get really complicated if you have child forms accessing their parents directly - and it becomes impossible to know which window is causing certain behaviour. Maintenance hence becomes a nightmare.

    All the software companies I've ever worked for would have you shot on sight if you did this as part of their development team because of its impact of how maintainable the application is.

    Presumably your child form is being created by your MDI parent. If not, it should be.

    Implement an event on your child form such as "LoggedIn" and hook into it when your MDI parent is created.

    Fire the event when your child form is logged in and then the MDI parent can handle the event and set the relevant control states.

    If you don't know how to write your own delegates & events I suggest you look here.

    Or just google for C++/CLI delegates events.

    Darwen.
    Last edited by darwen; January 26th, 2009 at 06:13 AM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    Jan 2009
    Posts
    3

    Re: Access a MainMenuStrip subitem from a child form

    Darwen,

    Thanks for your criticism, but I want to know to how to do this exactly the way I have described.
    It's a project for the university, it is not for my employer and since it is a very simple application I have no problem with "Maintenance hence becomes a nightmare".

    Thanks,
    Wisher

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Access a MainMenuStrip subitem from a child form

    (1) I find bad habits are very hard to break. If you develop good habits in the first place then I tend to find life a lot easier. You have to learn good practice sooner or later so why not sooner ?

    (2) Coding 'just to get it done' tends to cause you more headaches than doing things the tried and tested way.

    (3) It's useful for you to learn events & delegates regardless as they're one of the most important and useful features of .NET.

    (4) Doing it the way I suggest fixes your problem. It's the way that I would do it. And maybe you're having a problem because you're not using the correct method in the first place ?

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  5. #5
    Join Date
    Jan 2009
    Posts
    3

    Re: Access a MainMenuStrip subitem from a child form

    Okay, this one is dropped.
    Thanks darwen for the constructive criticism! I will learn more about delegates and events but had only 1 day to solve the problem -> found a workaround.
    It's now time I visited the c# section as the next exam is coming this Saturday.

    Thank you guys, you are great!

    Best,
    Wisher

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured