Csharp/CSharp Tutorial/GUI Windows Forms/Menu — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

Add Menu to Form

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FormWithMenu : System.Windows.Forms.Form
{
  private MainMenu mainMenu;
  private System.ruponentModel.Container components = null;
  public FormWithMenu()
  {
    InitializeComponent();
    mainMenu = new MainMenu();
    MenuItem miFile = mainMenu.MenuItems.Add("&File");          
    miFile.MenuItems.Add(new MenuItem("E&xit", new EventHandler(this.FileExit_Clicked), Shortcut.CtrlX));
    
    MenuItem miHelp = mainMenu.MenuItems.Add("Help");
    miHelp.MenuItems.Add(new MenuItem("&About",  new EventHandler(this.HelpAbout_Clicked),Shortcut.CtrlA));
    this.Menu = mainMenu;
    mainMenu.GetForm().BackColor = Color.Black;
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.ruponents = new System.ruponentModel.Container();
    this.Size = new System.Drawing.Size(300,300);
    this.Text = "Form1";
  }
  private void FileExit_Clicked(object sender, EventArgs e) 
  {
    this.Close();
  }
    
  private void HelpAbout_Clicked(object sender, EventArgs e) 
  {
    MessageBox.Show("Help");
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new FormWithMenu());
  }
}

Add Submenu

using System;
using System.Drawing;
using System.ruponentModel;
using System.Windows.Forms;
public class MenuSubMenu : System.Windows.Forms.Form
{
    public  MenuSubMenu()
    {
        MainMenu menu = new MainMenu();
        MenuItem filemenu = new MenuItem();
        filemenu.Text = "&File";
        menu.MenuItems.Add(filemenu);
            MenuItem open = new MenuItem();
            open.Text = "&Open";
            filemenu.MenuItems.Add(open);
            MenuItem print= new MenuItem();
            print.Text = "Print...";
            filemenu.MenuItems.Add(print);
                MenuItem temp= new MenuItem();
                temp.Text = "Pre&view";
                print.MenuItems.Add(temp);
                temp= new MenuItem();
                temp.Text = "To &File";
                print.MenuItems.Add(temp);
            MenuItem exit= new MenuItem();
            exit.Text = "E&xit";
            filemenu.MenuItems.Add(exit);
    
        this.Menu = menu;
        ContextMenu cmenu = new ContextMenu();
        cmenu.MenuItems.Add(new MenuItem("&First"));
        cmenu.MenuItems.Add(new MenuItem("&Second"));
        cmenu.MenuItems.Add(new MenuItem("-"));
        cmenu.MenuItems.Add(new MenuItem("&Third"));
        this.ContextMenu=cmenu;
    }
    static void Main()
    {
        Application.Run(new MenuSubMenu());
    }
}

Context Menu Demo

using System;
using System.Drawing;
using System.Windows.Forms;
   
class ContextMenuDemo: Form
{
     MenuItem miColor;
   
     public static void Main()
     {
          Application.Run(new ContextMenuDemo());
     }
     public ContextMenuDemo()
     {
          EventHandler eh = new EventHandler(MenuColorOnClick);
   
          MenuItem[] ami = { new MenuItem("Black",   eh),
                             new MenuItem("Blue",    eh),
                             new MenuItem("Green",   eh),
                             new MenuItem("White",   eh) };
   
          foreach (MenuItem mi in ami)
               mi.RadioCheck = true;
   
          miColor = ami[3];
          miColor.Checked = true;
          BackColor = Color.FromName(miColor.Text);
   
          ContextMenu = new ContextMenu(ami);
     }
     void MenuColorOnClick(object obj, EventArgs ea)
     {
          miColor.Checked = false;
          miColor = (MenuItem) obj;
          miColor.Checked = true;
   
          BackColor = Color.FromName(miColor.Text);
     }
}

Context Menu Using Add

using System;
using System.Drawing;
using System.Windows.Forms;
   
class ContextMenuAdd: Form
{
     MenuItem miColor;
   
     public static void Main()
     {
          Application.Run(new ContextMenuAdd());
     }
     public ContextMenuAdd()
     {
          Text = "Context Menu Using Add";
   
          ContextMenu  cm = new ContextMenu();
          EventHandler eh = new EventHandler(MenuColorOnClick);
   
          cm.MenuItems.Add("Black",   eh);
          cm.MenuItems.Add("Blue",    eh);
          cm.MenuItems.Add("Green",   eh);
          cm.MenuItems.Add("White",   eh);
   
          foreach (MenuItem mi in cm.MenuItems)
               mi.RadioCheck = true;
   
          miColor = cm.MenuItems[3];
          miColor.Checked = true;
          BackColor = Color.FromName(miColor.Text);
   
          ContextMenu = cm;
     }
     void MenuColorOnClick(object obj, EventArgs ea)
     {
          miColor.Checked = false;
          miColor = (MenuItem) obj;
          miColor.Checked = true;
   
          BackColor = Color.FromName(miColor.Text);
     }
}

Create a Menu without using the IDE

using System;
using System.Drawing;
using System.ruponentModel;
using System.Windows.Forms;
public class MenuCreationManually : System.Windows.Forms.Form
{
    void ShowInfo(Object Sender,EventArgs e)
    {
        MenuItem item=(MenuItem)Sender;
        switch(item.Text)
        {
            case "&Open":
                Console.WriteLine("Open a file from disk");
            break;
            case "&Save":
                Console.WriteLine("Save a file onto disk");
            break;
            case "E&xit":
                Console.WriteLine("Exit MenuApp");
            break;
        }
    }
    public MenuCreationManually()
    {
        this.MaximizeBox = true;
        this.FormBorderStyle = FormBorderStyle.Sizable;
        MainMenu menu = new MainMenu();
        
        MenuItem filemenu = new MenuItem();
        filemenu.Text = "&File";
        menu.MenuItems.Add(filemenu);
            MenuItem open = new MenuItem();
            open.Text = "&Open";
            open.Select += new EventHandler(ShowInfo);
            filemenu.MenuItems.Add(open);
            MenuItem save= new MenuItem();
            save.Text = "&Save";
            save.Select += new EventHandler(ShowInfo);
            filemenu.MenuItems.Add(save);

            MenuItem exit= new MenuItem();
            exit.Text = "E&xit";
            exit.Select += new EventHandler(ShowInfo);
            filemenu.MenuItems.Add(exit);
    
        this.Menu = menu;
        ContextMenu cmenu = new ContextMenu();
        cmenu.MenuItems.Add(new MenuItem("&First"));
        cmenu.MenuItems.Add(new MenuItem("&Second"));
        cmenu.MenuItems.Add(new MenuItem("-"));
        cmenu.MenuItems.Add(new MenuItem("&Third"));
        this.ContextMenu=cmenu;
    }
    static void Main()
    {
        Application.Run(new MenuCreationManually());
    }
}

Menu RightToLeft

using System;
using System.Drawing;
using System.ruponentModel;
using System.Windows.Forms;
public class MenuCreationManually : System.Windows.Forms.Form
{
    void ShowInfo(Object Sender,EventArgs e)
    {
        MenuItem item=(MenuItem)Sender;
        switch(item.Text)
        {
            case "&Open":
                Console.WriteLine("Open a file from disk");
            break;
            case "&Save":
                Console.WriteLine("Save a file onto disk");
            break;
            case "E&xit":
                Console.WriteLine("Exit MenuApp");
            break;
        }
    }
    public MenuCreationManually()
    {
        this.MaximizeBox = true;
        this.FormBorderStyle = FormBorderStyle.Sizable;
        MainMenu menu = new MainMenu();
        menu.RightToLeft = RightToLeft.Yes;
        
        MenuItem filemenu = new MenuItem();
        filemenu.Text = "&File";
        menu.MenuItems.Add(filemenu);
            MenuItem open = new MenuItem();
            open.Text = "&Open";
            open.Select += new EventHandler(ShowInfo);
            filemenu.MenuItems.Add(open);
            MenuItem save= new MenuItem();
            save.Text = "&Save";
            save.Select += new EventHandler(ShowInfo);
            filemenu.MenuItems.Add(save);

            MenuItem exit= new MenuItem();
            exit.Text = "E&xit";
            exit.Select += new EventHandler(ShowInfo);
            filemenu.MenuItems.Add(exit);
    
        this.Menu = menu;
        ContextMenu cmenu = new ContextMenu();
        cmenu.MenuItems.Add(new MenuItem("&First"));
        cmenu.MenuItems.Add(new MenuItem("&Second"));
        cmenu.MenuItems.Add(new MenuItem("-"));
        cmenu.MenuItems.Add(new MenuItem("&Third"));
        this.ContextMenu=cmenu;
    }
    static void Main()
    {
        Application.Run(new MenuCreationManually());
    }
}

Set Label text in menu action

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class LabelSetTextMenuAction : System.Windows.Forms.Form
{
    private System.Windows.Forms.MainMenu mainMenu1;
    private System.Windows.Forms.MenuItem menuItem1;
    private System.Windows.Forms.MenuItem menuItem2;
    private System.Windows.Forms.MenuItem menuItem3;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.ContextMenu contextMenu1;
    private System.Windows.Forms.MenuItem menuItem4;
    private System.Windows.Forms.MenuItem menuItem5;
    private System.ruponentModel.Container components = null;
    public LabelSetTextMenuAction()
    {
        InitializeComponent();
    }
    protected override void Dispose( bool disposing )
    {
        if( disposing )
        {
            if (components != null) 
            {
                components.Dispose();
            }
        }
        base.Dispose( disposing );
    }
    #region Windows Form Designer generated code
    private void InitializeComponent()
    {
        this.mainMenu1 = new System.Windows.Forms.MainMenu();
        this.menuItem1 = new System.Windows.Forms.MenuItem();
        this.menuItem2 = new System.Windows.Forms.MenuItem();
        this.menuItem3 = new System.Windows.Forms.MenuItem();
        this.label1 = new System.Windows.Forms.Label();
        this.contextMenu1 = new System.Windows.Forms.ContextMenu();
        this.menuItem4 = new System.Windows.Forms.MenuItem();
        this.menuItem5 = new System.Windows.Forms.MenuItem();
        this.SuspendLayout();
        this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.menuItem1});
        this.menuItem1.Index = 0;
        this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                  this.menuItem2,
                                                                                  this.menuItem3});
        this.menuItem1.Text = "&Say";
        this.menuItem2.Index = 0;
        this.menuItem2.Shortcut = System.Windows.Forms.Shortcut.CtrlH;
        this.menuItem2.Text = "&Hello";
        this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
        this.menuItem3.Index = 1;
        this.menuItem3.Shortcut = System.Windows.Forms.Shortcut.CtrlY;
        this.menuItem3.Text = "Goodb&ye";
        this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
        this.label1.ContextMenu = this.contextMenu1;
        this.label1.Location = new System.Drawing.Point(96, 122);
        this.label1.Name = "label1";
        this.label1.TabIndex = 0;
        this.label1.Text = "";
        this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                     this.menuItem4,
                                                                                     this.menuItem5});
        this.menuItem4.Index = 0;
        this.menuItem4.Text = "Say hello";
        this.menuItem5.Index = 1;
        this.menuItem5.Text = "Say goodbye";
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                      this.label1});
        this.Menu = this.mainMenu1;
        this.Name = "LabelSetTextMenuAction";
        this.Text = "Menu Example";
        this.ResumeLayout(false);
    }
    #endregion
    [STAThread]
    static void Main() 
    {
        Application.Run(new LabelSetTextMenuAction());
    }
    private void menuItem2_Click(object sender, System.EventArgs e)
    {
        label1.Text = "Hello!";
    }
    private void menuItem3_Click(object sender, System.EventArgs e)
    {
        label1.Text = "Goodbye!";
    }
}