C# 0 The Complete Reference


PART II C h a p t e r   2 6



Download 4,07 Mb.
Pdf ko'rish
bet1069/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   1065   1066   1067   1068   1069   1070   1071   1072   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART II

C h a p t e r   2 6 :  

U s e   S y s t e m . W i n d o w s . F o r m s  

861


Finally, the 

MainMenu

 object must be assigned to the 



Menu

 property of the form, as 

shown here:

Menu = MyMenu;

After this assignment takes place, the menu will be displayed when the window is created, 

and selections will be sent to the proper handler.

The following program puts together all the pieces and demonstrates how to create a 

main menu and handle menu selections.

// Add a Main Menu.

using System;

using System.Windows.Forms;

class MenuForm : Form {

  MainMenu MyMenu;

  public MenuForm() {

    Text = "Adding a Main Menu";

    // Create a main menu object.

    MyMenu  = new MainMenu();

    // Add top-level menu items to the menu.

    MenuItem m1 = new MenuItem("File");

    MyMenu.MenuItems.Add(m1);

    MenuItem m2 = new MenuItem("Tools");

    MyMenu.MenuItems.Add(m2);

    // Create File submenu.

    MenuItem item1 = new MenuItem("Open");

    m1.MenuItems.Add(item1);

    MenuItem item2 = new MenuItem("Close");

    m1.MenuItems.Add(item2);

    MenuItem item3 = new MenuItem("Exit");

    m1.MenuItems.Add(item3);

    // Create Tools submenu.

    MenuItem item4 = new MenuItem("Coordinates");

    m2.MenuItems.Add(item4);

    MenuItem item5 = new MenuItem("Change Size");

    m2.MenuItems.Add(item5);

    MenuItem item6 = new MenuItem("Restore");

    m2.MenuItems.Add(item6);

    // Add event handlers for the menu items.

    item1.Click += MMOpenClick;

    item2.Click += MMCloseClick;

www.freepdf-books.com




862

 

P a r t   I I :  



E x p l o r i n g   t h e   C #   L i b r a r y

    item3.Click += MMExitClick;

    item4.Click += MMCoordClick;

    item5.Click += MMChangeClick;

    item6.Click += MMRestoreClick;

    // Assign the menu to the form.

    Menu = MyMenu;

  }


  [STAThread]

  static void Main() {

    MenuForm skel = new MenuForm();

    Application.EnableVisualStyles();

    Application.Run(skel);

  }


  // Handler for main menu Coordinates selection.

  void MMCoordClick(object who, EventArgs e) {

    // Create a string that contains the coordinates.

    string size =

      String.Format("{0}: {1}, {2}\n{3}: {4}, {5} ",

                    "Top, Left", Top, Left,

                    "Bottom, Right", Bottom, Right);

    // Display a message box.

    MessageBox.Show(size, "Window Coordinates",

                    MessageBoxButtons.OK);

  }

  // Handler for main menu Change selection.



  void MMChangeClick(object who, EventArgs e) {

    Width = Height = 200;

  }

  // Handler for main menu Restore selection.



  void MMRestoreClick(object who, EventArgs e) {

    Width = Height = 300;

  }

  // Handler for main menu Open selection.



  void MMOpenClick(object who, EventArgs e) {

    MessageBox.Show("Inactive", "Inactive",

                    MessageBoxButtons.OK);

  }


  // Handler for main menu Close selection.

  void MMCloseClick(object who, EventArgs e) {

    MessageBox.Show("Inactive", "Inactive",

                    MessageBoxButtons.OK);

  }

  // Handler for main menu Exit selection.



www.freepdf-books.com


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   1065   1066   1067   1068   1069   1070   1071   1072   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish