靈魂-放水

          為學(xué)日益,為道日損。

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            296 Posts :: 10 Stories :: 274 Comments :: 0 Trackbacks
          ?????? ?可以從工具欄上拖個ContextMenu控件下來,并編輯好,然后把你所需要添加ContextMenu的控件的ContextMenu屬性設(shè)為這個右鍵菜單;除了上貼所述的方法,您也可以通過手工添寫代碼來實現(xiàn)彈出式菜單。關(guān)鍵的類是ContextMenu類。該類有兩個構(gòu)造函數(shù),其中ContextMenu()生成一個不含任何菜單項的彈出式菜單;ContextMenu(MenuItem[] ? menus)生成一個包括參數(shù)中所指定的菜單項的彈出式菜單。如要給一個按鈕控件button1添加彈出式菜單,可以參考以下的代碼: ?
          ? ?
          ? ContextMenu ? Menu1=new ? ContextMenu(); ? ?
          ? Menu1.MenuItems.Add(new ? MenuItem(“彈出菜單一")); ? ?
          ? Menu1.MenuItems.Add(new ? MenuItem(“彈出菜單二")); ? ?
          ? button1.ContextMenu=Menu1; ?
          ? ?
          ? ContextMenu有幾個關(guān)鍵的屬性、方法和事件,可以幫助您定制彈出式菜單,屬性RightToLeft可以使菜單項從右到左對齊,屬性SourceControl返回一個Control值表示當(dāng)前所顯示彈出菜單對應(yīng)的控件。Show()方法可以使程序主動顯示彈出菜單。當(dāng)彈出菜單彈出時將引發(fā)一個Popup事件,你可以在該事件的響應(yīng)方法中進行一些處理使彈出菜單顯示前做一些操作。 ?
          ? ?
          ? 您還可以參考MSDN中給出的一個示例來定制彈出式菜單: ?
          ? http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWindowsFormsContextMenuClassTopic.asp?

          msdn示例代碼(C#)

          private void MyPopupEventHandler(System.Object sender, System.EventArgs e)
           {
              // Define the MenuItem objects to display for the TextBox.
              MenuItem menuItem1 = new MenuItem("&Copy");
              MenuItem menuItem2 = new MenuItem("&Find and Replace");
              // Define the MenuItem object to display for the PictureBox.
              MenuItem menuItem3 = new MenuItem("C&hange Picture");
          
              // Clear all previously added MenuItems.
              contextMenu1.MenuItems.Clear();
           
              if(contextMenu1.SourceControl == textBox1)
              {
                 // Add MenuItems to display for the TextBox.
                 contextMenu1.MenuItems.Add(menuItem1);
                 contextMenu1.MenuItems.Add(menuItem2);
              }
              else if(contextMenu1.SourceControl == pictureBox1)
              {
                 // Add the MenuItem to display for the PictureBox.
                 contextMenu1.MenuItems.Add(menuItem3);
              }
           }
          
          ?
          posted on 2007-03-09 14:03 放水老倌 閱讀(2562) 評論(0)  編輯  收藏 所屬分類: .NET
          主站蜘蛛池模板: 镇赉县| 华池县| 乌兰察布市| 拜泉县| 株洲市| 苍南县| 华安县| 哈尔滨市| 故城县| 天门市| 肇州县| 遂川县| 黎平县| 铜鼓县| 平果县| 石泉县| 敦煌市| 白朗县| 临夏县| 丁青县| 铁力市| 榆树市| 鄂伦春自治旗| 望城县| 大渡口区| 浦东新区| 东乌珠穆沁旗| 交城县| 河西区| 泽库县| 鹤壁市| 海林市| 泾阳县| 汉川市| 杂多县| 塔河县| 湘潭县| 五台县| 勃利县| 神农架林区| 裕民县|