在1个控件中添加2个事件处理程序

时间:2013-06-01 04:52:50

标签: c#

正如在实际的代码编辑器中一样,可以在右键单击上找到上下文菜单中的内容,如下所示: enter image description here

我已经使用此代码完成剪切,复制和粘贴:

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 (不要使用设计器)我的问题是如何在控件(不同的处理程序)中创建多个事件处理程序,如下所示:

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)),

正确的方式。谢谢!

2 个答案:

答案 0 :(得分:1)

我会用

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

答案 1 :(得分:1)

无法从构造函数设置MeasureItem事件,请尝试:

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