c#中的ContextMenuStrip事件处理程序

时间:2016-01-05 14:42:58

标签: c# winforms

我想将ContextMenyStrip事件处理程序附加到每个创建的图片框中,如果它的名称是" merger"。这是代码。

public partial class Form1 : Form
{
    public NetworkArea n;
    Components sp1;
    Components sp2;
    int s = 0;
    int p = 0;
    PictureBox pb;
    Merger mg;
            public Form1()
    {
        InitializeComponent();
        pb = new PictureBox();
       // pb.Name = "Merger";
        mg = null;
        n = new NetworkArea();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        panel1.AllowDrop = true;

        pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);

        pictureBox2.MouseDown += new MouseEventHandler(pictureBox2_MouseDown);
        pictureBox3.MouseDown += new MouseEventHandler(pictureBox3_MouseDown);
        pictureBox4.MouseDown += new MouseEventHandler(pictureBox4_MouseDown);
        pictureBox5.MouseDown += new MouseEventHandler(pictureBox5_MouseDown);

        panel1.DragEnter += new DragEventHandler(panel1_DragEnter);
        this.panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
        panel1.GiveFeedback += new GiveFeedbackEventHandler(panel1_GiveFeedback);

    }


    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {

        if (e.Button == MouseButtons.Left)
        {
            var dragImage = (Bitmap)pictureBox1.Image;
            IntPtr icon = dragImage.GetHicon();
            Cursor.Current = new Cursor(icon);
            DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);

            pb.Name = "Pump";

        }
    }
    void contexMenuuu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        ToolStripItem item = e.ClickedItem;
        // your code here
    }
    private void panel1_DragDrop(object sender, DragEventArgs e)
    {


        pb.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap);

         pb.Size = new System.Drawing.Size(50,50);
         pb.SizeMode = PictureBoxSizeMode.StretchImage;
        pb.Location = panel1.PointToClient(new Point(e.X - pb.Width / 2, e.Y - pb.Height / 2));
        panel1.Controls.Add(pb);

        if (pb.Name == "Pump")
        {
            label3.Text = "Pump" + pb.Location.X + " " + pb.Location.Y;


        }

            if (pb.Name == "Splitter")
            {
                label3.Text = "Splitter" + pb.Location.X + " " + pb.Location.Y;
            }
            if (pb.Name == "AdSplitter")
            {
                label3.Text = "AdSplitter" + pb.Location.X + " " + pb.Location.Y;
            }
            if (pb.Name == "Merger")
            {
                label3.Text = "Merger" + pb.Location.X + " " + pb.Location.Y;
                mg = new Merger(pb.Location.X, pb.Location.Y);
                pb.ContextMenuStrip = new ContextMenuStrip();
                pb.ContextMenuStrip.ItemClicked += new ToolStripItemClickedEventHandler(contexMenuuu_ItemClicked);
                pb.ContextMenuStrip.Items.Add("Change");

                // 
                }
            if (pb.Name == "Sink")
            {
                label3.Text = "Sink" + pb.Location.X + " " + pb.Location.Y;
                pb.ContextMenuStrip.ItemClicked -= new ToolStripItemClickedEventHandler(contexMenuuu_ItemClicked);
            }

        pb = new PictureBox(); 

    }

    private void panel1_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.Bitmap))
        {
            e.Effect = DragDropEffects.Copy;

        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }


    private void panel1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
    {
        e.UseDefaultCursors = false;
    }

    private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            var dragImage = (Bitmap)pictureBox2.Image;
            IntPtr icon = dragImage.GetHicon();
            Cursor.Current = new Cursor(icon);
            DoDragDrop(pictureBox2.Image, DragDropEffects.Copy);

            pb.Name ="Splitter";

        }
    }

    private void pictureBox3_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            var dragImage = (Bitmap)pictureBox3.Image;
            IntPtr icon = dragImage.GetHicon();
            Cursor.Current = new Cursor(icon);
            DoDragDrop(pictureBox3.Image, DragDropEffects.Copy);

            pb.Name = "AdSplitter";

        }
    }

    private void pictureBox4_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            var dragImage = (Bitmap)pictureBox3.Image;
            IntPtr icon = dragImage.GetHicon();
            Cursor.Current = new Cursor(icon);
            DoDragDrop(pictureBox4.Image, DragDropEffects.Copy);

            pb.Name = "Merger";

        }

    }

    private void pictureBox5_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            var dragImage = (Bitmap)pictureBox3.Image;
            IntPtr icon = dragImage.GetHicon();
            Cursor.Current = new Cursor(icon);
            DoDragDrop(pictureBox5.Image, DragDropEffects.Copy);

            pb.Name = "Sink";

        }
    }





    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        //Graphics gr = e.Graphics;
        //ImageList il = new ImageList();

        //il.ImageSize = new Size(50, 50);
        //il.Images.Add(Image.FromFile("../../../images/1Merger.png"));
        //gr.DrawImage(il.Images[0], 30, 109);
        //mg = new Merger(30, 109);
    }


}

问题是我拖放并删除合并然后它没有上下文菜单,但之后每个合并将有contextmenu.Then拖放另一个图片框(不合并)如果我拖放合并picturebox再次没有显示任何上下文菜单,但在此之后其余的合并都有这个菜单。 谁能告诉我这些代码有什么问题? 提前致谢

0 个答案:

没有答案