当前位置: 首页>>代码示例>>C#>>正文


C# MenuItem.Click事件代码示例

本文整理汇总了C#中System.Windows.Forms.MenuItem.Click事件的典型用法代码示例。如果您正苦于以下问题:C# MenuItem.Click事件的具体用法?C# MenuItem.Click怎么用?C# MenuItem.Click使用的例子?那么恭喜您, 这里精选的事件代码示例或许可以为您提供帮助。您也可以进一步了解该事件所在System.Windows.Forms.MenuItem的用法示例。


在下文中一共展示了MenuItem.Click事件的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateMyMenu

public void CreateMyMenu()
{
    // Create a main menu object.
    MainMenu mainMenu1 = new MainMenu();

    // Create empty menu item objects.
    MenuItem topMenuItem = new MenuItem();
    MenuItem menuItem1 = new MenuItem();
          
    // Set the caption of the menu items.
    topMenuItem.Text = "&File";
    menuItem1.Text = "&Open";

    // Add the menu items to the main menu.
        topMenuItem.MenuItems.Add(menuItem1);
    mainMenu1.MenuItems.Add(topMenuItem);
                
    // Add functionality to the menu items using the Click event. 
    menuItem1.Click += new System.EventHandler(this.menuItem1_Click);

    // Assign mainMenu1 to the form.
    this.Menu=mainMenu1;
}

private void menuItem1_Click(object sender, System.EventArgs e)
{	
           // Create a new OpenFileDialog and display it.
   OpenFileDialog fd = new OpenFileDialog();
       fd.DefaultExt = "*.*";
   fd.ShowDialog();
}
开发者ID:.NET开发者,项目名称:System.Windows.Forms,代码行数:31,代码来源:MenuItem.Click

示例2: Main

//引入命名空间
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

class ImageClip : Form {
    MenuItem miCut, miCopy, miPaste, miDel;
    Image image;
    public static void Main() {
        Application.Run(new ImageClip());
    }
    public ImageClip() {
        Text = "Image Clip";

        MenuItem mi = new MenuItem("&Edit");
        mi.Popup += new EventHandler(MenuEditOnPopup);
        Menu.MenuItems.Add(mi);
        int index = Menu.MenuItems.Count - 1;

        miCut = new MenuItem("Cu&t");
        miCut.Click += new EventHandler(MenuEditCutOnClick);
        miCut.Shortcut = Shortcut.CtrlX;
        Menu.MenuItems[index].MenuItems.Add(miCut);

        miCopy = new MenuItem("&Copy");
        miCopy.Click += new EventHandler(MenuEditCopyOnClick);
        miCopy.Shortcut = Shortcut.CtrlC;
        Menu.MenuItems[index].MenuItems.Add(miCopy);

        miPaste = new MenuItem("&Paste");
        miPaste.Click += new EventHandler(MenuEditPasteOnClick);
        miPaste.Shortcut = Shortcut.CtrlV;
        Menu.MenuItems[index].MenuItems.Add(miPaste);

        miDel = new MenuItem("De&lete");
        miDel.Click += new EventHandler(MenuEditDelOnClick);
        miDel.Shortcut = Shortcut.Del;
        Menu.MenuItems[index].MenuItems.Add(miDel);
    }
    void MenuEditOnPopup(object obj, EventArgs ea) {
        miCut.Enabled = miCopy.Enabled = miDel.Enabled = image != null;
        IDataObject data = Clipboard.GetDataObject();
        miPaste.Enabled = data.GetDataPresent(typeof(Bitmap)) || data.GetDataPresent(typeof(Metafile));
    }
    void MenuEditCutOnClick(object obj, EventArgs ea) {
        MenuEditCopyOnClick(obj, ea);
        MenuEditDelOnClick(obj, ea);
    }
    void MenuEditCopyOnClick(object obj, EventArgs ea) {
        Clipboard.SetDataObject(image, true);
    }
    void MenuEditPasteOnClick(object obj, EventArgs ea) {
        IDataObject data = Clipboard.GetDataObject();

        if (data.GetDataPresent(typeof(Metafile)))
            image = (Image)data.GetData(typeof(Metafile));

        else if (data.GetDataPresent(typeof(Bitmap)))
            image = (Image)data.GetData(typeof(Bitmap));

        Invalidate();
    }
    void MenuEditDelOnClick(object obj, EventArgs ea) {
        image = null;
        Invalidate();
    }
}
开发者ID:C#程序员,项目名称:System.Windows.Forms,代码行数:68,代码来源:MenuItem.Click


注:本文中的System.Windows.Forms.MenuItem.Click事件示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。