none
How can I create a button within a sub form to return to my Main Menu? RRS feed

  • Question

  • I tried adding a button and the Visual C# code

    SubFormMainMenu.Show();

    But I get the error message...

    The name 'SubFormMainMenu' does not exist in the current context

    Is that because the FormMainMenu is the first program executed within my Application?

    How can I create a button to go to my Main Menu from a Sub-Menu?

    Any help is greatly appreciated and Thanks in advance for your review and am hopeful for a reply.

    PSULionRP

    • Moved by CoolDadTx Tuesday, April 2, 2013 3:02 PM Winforms related
    Monday, April 1, 2013 9:03 PM

Answers

  • Hi PSULionRP,

    Compiler Error CS0103(The name 'identifier' does not exist in the current context) means the code attempt to use a name that does not exist the class, namespace, or scope. Check the spelling of the name and check your using directives and assembly references to make sure that the name that you are trying to use is available.

    Try to declare and initial SubFormMainMenu before you use it.

    For example,

    Form SubFormMainMenu = new Form();
    SubFormMainMenu.Show();

    Best Regards,


    Bob Wu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 3, 2013 2:52 AM
    Moderator

All replies

  • Well, there are two ways:

    1. Create the MainForm from scratch again (the same as WinForms does for you automagically at applciation start):

    MainWindow mw = new MainWindow();
    mw.Show();

    2. Give the form where the button resides a Varriable of Type MainWindow. Give it a reference on the main window during creation. But this might introduce memory leaks (the "I forgot I still got a reference" kind). Somewhat like this:

    //variable to store the mainWindow reference
    MainWindow MWR;
    
    //constructor that takes a parameter
    public subWindow(MainWindow parameter){
      MWR = parameter;
    }
    
    //event that shows the mai nWindow again
    public void buttonClick (objectg sender, InputEventArgs e){
      MWR.Show();
    }


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    Monday, April 1, 2013 9:39 PM
  • Don't close the main form, just hide it. Then, hide the sub form.

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Proposed as answer by Noam B Tuesday, April 2, 2013 3:34 PM
    Tuesday, April 2, 2013 9:04 AM
  • Hi PSULionRP,

    Compiler Error CS0103(The name 'identifier' does not exist in the current context) means the code attempt to use a name that does not exist the class, namespace, or scope. Check the spelling of the name and check your using directives and assembly references to make sure that the name that you are trying to use is available.

    Try to declare and initial SubFormMainMenu before you use it.

    For example,

    Form SubFormMainMenu = new Form();
    SubFormMainMenu.Show();

    Best Regards,


    Bob Wu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 3, 2013 2:52 AM
    Moderator