2013-06-01 46 views
-1

IM上下文菜单做可以右击找到像在实际的代码编辑器是这样的: enter image description here在1个控制添加2事件处理程序

,并已经与切做IM,复制和粘贴使用此代码:

private void rtb_MouseDown(object sender, MouseEventArgs e) 
     { 
if (e.Button == MouseButtons.Right) 
      { 

       MenuItem[] menuItems = new MenuItem[] { 
             new MenuItem("Cut", new System.EventHandler(this.CutMenuItemClick)), 
             new MenuItem("Copy", new System.EventHandler(this.CopyMenuItemClick)), 
             new MenuItem("Paste", new System.EventHandler(this.PasteMenuItemClick)), 


       ContextMenu rightcontext = new ContextMenu(menuItems); 

       int xOffset = Cursor.Position.X - DtexteditoR.ActiveForm.Location.X; 
       int yOffset = Cursor.Position.Y - DtexteditoR.ActiveForm.Location.Y; 

       rightcontext.Show(DtexteditoR.ActiveForm, new Point(xOffset, yOffset)); 

      } 
     } 
private void CutMenuItemClick(object sender, EventArgs e) 
     { 
      rtb.Cut(); 
     } 
     private void CopyMenuItemClick(object sender, EventArgs e) 
     { 
      rtb.Copy(); 
     } 
     private void PasteMenuItemClick(object sender, EventArgs e) 
     { 
      rtb.Paste(); 
     } 

使用的WinForms与动态控制(不使用设计师)和我的问题IM是如何使多个事件处理程序的控制(不同的处理程序)是这样的:

new MenuItem("Copy", new System.EventHandler(this.CopyMenuItemClick) || new System.Windows.Forms.MeasureItemEventHandler(this.MeasureCopy)), 

private void MeasureCopy(object obj, 
          MeasureItemEventArgs miea) 
     { 
      MenuItem mi = (MenuItem)obj; 

      // Get standard menu font so that the text in this 
      // menu rectangle doesn't look funny with a 
      // different font 
      Font menuFont = SystemInformation.MenuFont; 

      StringFormat strfmt = new StringFormat(); 
      SizeF sizef = 
       miea.Graphics.MeasureString(mi.Text, menuFont, 1000, strfmt); 

      // Get image so size can be computed 
      Bitmap bmMenuImage = new Bitmap(typeof(NewForm), "COPY.BMP"); 

      // Add image height and width to the text height and width when 
      // drawn with selected font (got that from measurestring method) 
      // to compute the total height and width needed for the rectangle 
      miea.ItemWidth = (int)Math.Ceiling(sizef.Width) + bmMenuImage.Width; 
      miea.ItemHeight = (int)Math.Ceiling(sizef.Height) + bmMenuImage.Height; 
     } 

让我可以在“复制”旁边添加图像。

如何做这件事情:

new MenuItem("Copy", new System.EventHandler(this.CopyMenuItemClick) || new System.Windows.Forms.MeasureItemEventHandler(this.MeasureCopy)), 

正道.thanks!

+0

我不知道如果我理解正确的,但如果你正在使用多个事件一个事件的处理程序(来自一个控件)然后哪个事件处理程序会被触发?第一个还是第二个?比方说,如果我一次单击复制CopyMenuItemClick被触发,但在另一次它是MeasureCopy。但是如果你在CopyMenuItemClick之后说那是MeasureCopy的使用时间,那么我认为这更有意义。请参阅此链接http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/70cc4de1-cdda-4f1b-92a5-407443b2f591。 – Edper

回答

1

我只想用

MenuItem item = new MenuItem("Copy"); 
item.Click += this.CopyMenuItemClick; 
item.Click += this.MeasureCopy; 
1

MeasureItem事件不能从构造函数中设置,请尝试:

MenuItem item = new MenuItem("Copy", new System.EventHandler(this.CopyMenuItemClick)); 
item.MeasureItem += this.MeasureCopy;