View Single Post
Old 07-01-2005, 07:36 PM   #9 (permalink)
houssam_ballout
Diamond Member
 
Join Date: May 2005
Location: here
Age: 22
Posts: 1,009
Default

To test the application
• Debug the application and press F5 to run it. For details about debugging, see Debugging Basics.
The form has a menu that contains File, New, Open, and Exit menu items. Clicking New or Open raises an event, which is handled by the MenuSelect event handler. This method shows a MessageBox and changes the application state. The application state change is indicated by the addition of two menu items, Close and Save.
From the Microsoft site.

To create the second set of menus
1. In a second method, create a second instance of a MainMenu component with MenuItems that correspond to a second application state.
For example, after the user has opened a file, you can expose commands to close it and save it. You can copy the original menu structure to use as a base for this menu structure, and to preserve the File, New, Open, and Exit menu items. For details, see Copying Items on Windows Forms Menus.
' Visual Basic
' Create the second MainMain object.
Private mmFileLoadedMenu As MainMenu
Public Sub FileLoadedMenu()
mmFileLoadedMenu = New MainMenu()

' Clone the first menu with the CloneMenu method.
mmFileLoadedMenu.MenuItems.Add(miFile.CloneMenu())

' Create two additional menu items related to the
' application state.
Dim mnuitemClose As New MenuItem("&Close")
Dim mnuitemSave As New MenuItem("&Save")

' Add the two new menu items to the MenuItems collection of the
' top-level menu item cloned above, using the Add method to
' specify their order within the collection by their index.
mmFileLoadedMenu.MenuItems(0).MenuItems.Add((2), mnuitemClose)
mmFileLoadedMenu.MenuItems(0).MenuItems.Add((3), mnuitemSave)

' Assign the newly-created MainMenu object to the form.
Me.Menu = mmFileLoadedMenu
End Sub

// C#
// Create the second MainMain object.
private MainMenu mmFileLoadedMenu;
private void FileLoadedMenu(){
mmFileLoadedMenu = new MainMenu();

// Clone the first menu with the CloneMenu method.
mmFileLoadedMenu.MenuItems.Add(miFile.CloneMenu()) ;

// Create two additional menu items related to the
// application state.
MenuItem mnuitemClose = new MenuItem("&Close");
MenuItem mnuitemSave = new MenuItem("&Save");

// Add the two new menu items to the MenuItems collection of the
// top-level menu item cloned above, using the Add method to
// specify their order within the collection by their index.
mmFileLoadedMenu.MenuItems[0].MenuItems.Add(2, mnuitemClose);
mmFileLoadedMenu.MenuItems[0].MenuItems.Add(3, mnuitemSave);

// Assign the newly-created MainMenu object to the form.
Menu = mmFileLoadedMenu;
}

// C++
// Create the second MainMenu object.
private: System::Windows::Forms::MainMenu * mmFileLoadedMenu;

private:
System::Void FileLoadedMenu()
{
mmFileLoadedMenu = new System::Windows::Forms::MainMenu();

// Clone the first menu with the CloneMenu method.
mmFileLoadedMenu->MenuItems->Add(miFile->CloneMenu());

// Create two additional menu items related to the
// application state.
MenuItem *mnuitemClose = new MenuItem(S"&Close");
MenuItem *mnuitemSave = new MenuItem(S"&Save");

// Add the two new menu items to the MenuItems collection of the
// top-level menu item cloned above, using the Add method to
// specify their order within the collection by their index.
mmFileLoadedMenu->MenuItems->Item[0]->MenuItems->Add(2,
mnuitemClose);
mmFileLoadedMenu->MenuItems->Item[0]->MenuItems->Add(3,
mnuitemSave);

// Assign the newly created MainMenu object to the form.
this->Menu = mmFileLoadedMenu;
}
2. Add a line of code to the Form1 constructor, after the InitializeComponent method call, to call the AppStartMenu method created previously:
3. ' Visual Basic
4. AppStartMenu()
5.
6. // C#
7. AppStartMenu();
8.
9. // C++
AppStartMenu();
10. Create an event handler within the class to switch the form's menu property to FileLoadedMenu.
Instead of loading a file, the following code opens a message box to indicate a change in application state. For details, see MessageBox Class. The code then calls the AppStartMenu method that was written above, which creates the second MainMenu object and sets it to be the form's menu.
' Visual Basic
Protected Sub MenuSelect(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show("A file has been opened.", "Instead of a new file, here's a message box.")
FileLoadedMenu()
End Sub

// C#
protected void MenuSelect(Object sender, System.EventArgs e){
MessageBox.Show("A file has been opened.",
"Instead of a new file, here's a message box.");
FileLoadedMenu();
}

// C++
protected:
System::Void MenuSelect(System::Object * sender,
System::EventArgs * e)
{
MessageBox::Show("A file has been opened.",
"Instead of a new file, here's a message box.");
FileLoadedMenu();
}
houssam_ballout is offline   Reply With Quote