2010-08-07 82 views
2

我一直無法弄清楚如何在處理第一級的點擊事件後保持上下文菜單打開。這裏有一個例子,我有一個帶有可選菜單菜單的上下文菜單。處理點擊事件後,我打開上下文菜單,但我必須手動返回到內部菜單。有沒有辦法以編程方式打開外部菜單或阻止內部菜單關閉?ContextMenu不會保持打開狀態C#.Net 4.0客戶端配置文件WinForms

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace NoCloseContextMenu 
{ 
    public partial class Form1 : Form 
    { 
     bool[] store_checks = new bool[8]; 

     public Form1() 
     { 
      InitializeComponent(); 
      richTextBox1.AppendText("http://"); 
      richTextBox1.LinkClicked += new LinkClickedEventHandler(richTextBox1_LinkClicked); 
     } 

     void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e) 
     { 
      MenuItemExtended[] inner_menuitems = new MenuItemExtended[8]; 
      for (int i = 0; i < store_checks.Length; i++) 
      { 
       MenuItemExtended inner_menuitem = new MenuItemExtended("Check #" + i.ToString()); 
       inner_menuitem.menuitem_index = i; 
       inner_menuitem.contextmenu_point = this.PointToClient(Cursor.Position); 
       inner_menuitem.Checked = store_checks[i]; 
       inner_menuitem.Shortcut = (Shortcut)(131120 + i); //Ctrl+i = 131120+i 
       inner_menuitem.ShowShortcut = true; 
       inner_menuitem.Click += new EventHandler(inner_menuitem_Click); 
       inner_menuitems[i] = inner_menuitem; 
      } 
      MenuItem outer_menu = new MenuItem("Outer Menu", inner_menuitems); 
      ContextMenu context_menu = new ContextMenu(new MenuItem[] { outer_menu }); 
      context_menu.Show(this, this.PointToClient(Cursor.Position)); 
     } 

     void inner_menuitem_Click(object sender, EventArgs e) 
     { 
      MenuItemExtended sender_menu = (MenuItemExtended)sender; 
      store_checks[sender_menu.menuitem_index] = !store_checks[sender_menu.menuitem_index]; 
      sender_menu.Checked = !sender_menu.Checked; 
      sender_menu.GetContextMenu().Show(this, sender_menu.contextmenu_point); 
     } 

     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.richTextBox1 = new System.Windows.Forms.RichTextBox(); 
      this.SuspendLayout(); 
      // 
      // richTextBox1 
      // 
      this.richTextBox1.Location = new System.Drawing.Point(13, 13); 
      this.richTextBox1.Name = "richTextBox1"; 
      this.richTextBox1.Size = new System.Drawing.Size(100, 96); 
      this.richTextBox1.TabIndex = 0; 
      this.richTextBox1.Text = ""; 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(284, 262); 
      this.Controls.Add(this.richTextBox1); 
      this.Name = "Form1"; 
      this.Text = "Form1"; 
      this.ResumeLayout(false); 

     } 

     #endregion 

     private System.Windows.Forms.RichTextBox richTextBox1; 
    } 

    public class MenuItemExtended : MenuItem 
    { 
     public int menuitem_index; 
     public Point contextmenu_point; 

     public MenuItemExtended(string text) 
     { 
      this.Text = text; 
     } 
    } 
} 

此外,有沒有什麼辦法讓「Control + number」快捷鍵工作並激活點擊事件?先謝謝您的幫助!

回答

0

我沒有找到任何方法來阻止上下文菜單關閉,所以我使用了ContextMenuStrip和ToolStripMenuItem。這也解決了我之前使用快捷方式時遇到的問題。我處理包含可檢查項目的菜單的關閉事件,如果點擊/檢查項目,則取消關閉。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace NoCloseContextMenu 
{ 
    public partial class Form1 : Form 
    { 
     bool[] store_checks = new bool[8]; 

     public Form1() 
     { 
      InitializeComponent(); 
      richTextBox1.AppendText("http://"); 
      richTextBox1.LinkClicked += new LinkClickedEventHandler(richTextBox1_LinkClicked); 
     } 

     void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e) 
     { 
      ToolStripMenuItem[] inner_menuitems = new ToolStripMenuItem[8]; 
      for (int i = 0; i < store_checks.Length; i++) 
      { 
       ToolStripMenuItem inner_menuitem = new ToolStripMenuItem("Check #" + i.ToString()); 
       inner_menuitem.Checked = store_checks[i]; 
       inner_menuitem.CheckOnClick = true; 
       inner_menuitem.ShortcutKeys = Keys.Control | (Keys)(48 + i); //Di = 48 + i 
       inner_menuitem.ShowShortcutKeys = true; 
       inner_menuitem.Click += new EventHandler(inner_menuitem_Click); 
       inner_menuitem.Tag = i.ToString(); 
       inner_menuitems[i] = inner_menuitem; 
      } 
      ToolStripMenuItem outer_menu = new ToolStripMenuItem("Outer Menu", null, inner_menuitems); 
      outer_menu.DropDown.Closing += new ToolStripDropDownClosingEventHandler(DropDown_Closing); 
      ContextMenuStrip context_menu = new ContextMenuStrip(); 
      context_menu.Items.Add(outer_menu); 
      context_menu.Show(this, this.PointToClient(Cursor.Position)); 
     } 

     void DropDown_Closing(object sender, ToolStripDropDownClosingEventArgs e) 
     { 
      if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked) 
      { 
       e.Cancel = true; 
       ((ToolStripDropDownMenu)sender).Invalidate(); 
      } 
     } 

     void inner_menuitem_Click(object sender, EventArgs e) 
     { 
      ToolStripMenuItem sender_menu = (ToolStripMenuItem)sender; 
      int index = int.Parse(sender_menu.Tag.ToString()); 
      store_checks[index] = !store_checks[index]; 
     } 

     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.richTextBox1 = new System.Windows.Forms.RichTextBox(); 
      this.SuspendLayout(); 
      // 
      // richTextBox1 
      // 
      this.richTextBox1.Location = new System.Drawing.Point(13, 13); 
      this.richTextBox1.Name = "richTextBox1"; 
      this.richTextBox1.Size = new System.Drawing.Size(100, 96); 
      this.richTextBox1.TabIndex = 0; 
      this.richTextBox1.Text = ""; 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(284, 262); 
      this.Controls.Add(this.richTextBox1); 
      this.Name = "Form1"; 
      this.Text = "Form1"; 
      this.ResumeLayout(false); 
     } 

     #endregion 

     private System.Windows.Forms.RichTextBox richTextBox1; 
    } 
} 

您還可以從您的unclosable子菜單中選擇某些按鈕,以使上下文菜單正常關閉。對於一個特定的ToolStripMenuItem正常關閉菜單,給它一個不同的事件方法調用:

inner_menuitem.Click += new EventHandler(inner_menuitem_Can_Close); 

而且在方法使用下面的代碼(作品無論菜單有多深):

void inner_menuitem_Can_Close(object sender, EventArgs e) 
{ 
    ToolStripMenuItem castSender = (ToolStripMenuItem)sender; 
    object owner = castSender.OwnerItem; 
    while (owner is ToolStripMenuItem) 
    { 
     if (((ToolStripMenuItem)owner).Owner is ContextMenuStrip) 
      ((ContextMenuStrip)((ToolStripMenuItem)owner).Owner).Close(); 
     owner = ((ToolStripMenuItem)owner).OwnerItem; 
    } 
} 
0

我會強烈建議不要處理'父'上下文菜單項上的點擊事件 - 讓操作系統爲您處理。

相關問題