按下按钮时如何创建任意数量的对象实例 c# winforms

在我的代码中,每次按下 button1 时,面板中都会生成一个名为 NOT 的图片框实例。当图像被单击并按住时,可以四处拖动。我的问题是每次按下按钮 1 时,我都希望创建另一个具有相同属性的图片框,这样理论上我可以整天按下按钮 1 并根据需要拖动尽可能多的非图片框对象。到目前为止,一旦按下按钮,只会创建一个 NOT 实例,而无法生成另一个实例。所以基本上如何在每次按下 button1 时创建 NOT 的新唯一实例。


public Form1()

    {

        InitializeComponent();

        Drag();

    }


    private void button1_Click(object sender, EventArgs e)

    {

        spawnGate("not");

    }


    PictureBox NOT = new PictureBox();


    private Point startPoint = new Point();

    public void Drag()

    {

        NOT.MouseDown += (ss, ee) =>

        {

            if (ee.Button == System.Windows.Forms.MouseButtons.Left)

            {

                startPoint = Control.MousePosition;

            }

        };


        NOT.MouseMove += (ss, ee) =>

        {

            if (ee.Button == System.Windows.Forms.MouseButtons.Left)

            {

                Point temp = Control.MousePosition;

                Point res = new Point(startPoint.X - temp.X, startPoint.Y - temp.Y);


                NOT.Location = new Point(NOT.Location.X - res.X, NOT.Location.Y - res.Y);


                startPoint = temp;

            }

        };

    }


    public void spawnGate(string type)

    {

        switch (type)

        {

            case "not":

                NOT.Width = 100;

                NOT.Height = 50;

                NOT.Image = Properties.Resources.Not_gate;

                NOT.SizeMode = PictureBoxSizeMode.Zoom;

                workspace.Controls.Add(NOT);

            break;

        }

    }

}


慕尼黑的夜晚无繁华
浏览 230回答 2
2回答

摇曳的蔷薇

更改NOT为List<PictureBox>.然后,在方法中添加一个new PictureBox实例。请注意,需要更改以获取参数。NOTspawnGate()Drag()PictureBox编辑:根据评论中的要求,为了访问此问题的其他人的利益,这里正是需要更改代码才能获得 OP 请求的行为的方法。请注意,此设计可以而且应该在几个方面进行重构。List<PictureBox> NOT = new List<PictureBox>();Point startPoint = new Point();public Form1(){&nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; Drag();}private void button1_Click(object sender, EventArgs e){&nbsp; &nbsp; spawnGate();}public void spawnGate(){&nbsp; &nbsp; var pictureBox = new PictureBox()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Width = 100,&nbsp; &nbsp; &nbsp; &nbsp; Height = 50,&nbsp; &nbsp; &nbsp; &nbsp; Image = Properties.Resources.Not_gate,&nbsp; &nbsp; &nbsp; &nbsp; SizeMode = PictureBoxSizeMode.Zoom&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; Drag(pictureBox);&nbsp; &nbsp; NOT.Add(pictureBox);&nbsp; &nbsp; workspace.Controls.Add(pictureBox);}public void Drag(PictureBox pictureBox){&nbsp; &nbsp; pictureBox.MouseDown += (ss, ee) =>&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (ee.Button == System.Windows.Forms.MouseButtons.Left)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startPoint = Control.MousePosition;&nbsp; &nbsp; };&nbsp; &nbsp; pictureBox.MouseMove += (ss, ee) =>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (ee.Button == System.Windows.Forms.MouseButtons.Left)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Point temp = Control.MousePosition;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Point res = new Point(startPoint.X - temp.X, startPoint.Y - temp.Y);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pictureBox.Location = new Point(pictureBox.Location.X - pictureBox.X, pictureBox.Location.Y - res.Y);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startPoint = temp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };}

杨__羊羊

您不必直接在表单上保存指向 NOT 的指针(或者,如果您以后需要调用它们,可以将它们保存到列表中)。public Form1(){&nbsp; &nbsp; InitializeComponent();}private void button1_Click(object sender, EventArgs e){&nbsp; &nbsp; spawnGate("not");}// This list is optional, if you easily want to find them laterList<PictureBox> allNOTs = new List<PictureBox>();public void spawnGate(string type){&nbsp; &nbsp; switch (type)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; case "not":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PictureBox NOT = new PictureBox();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NOT.Width = 100;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NOT.Height = 50;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NOT.Image = Properties.Resources.Not_gate;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NOT.SizeMode = PictureBoxSizeMode.Zoom;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NOT.MouseDown += (ss, ee) =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Mouse down event code here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NOT.MouseMove += (ss, ee) =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Mouse move event code here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; allNOTS.Add(NOT); // Optional if you easily want to find it later&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; workspace.Controls.Add(NOT);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}}
打开App,查看更多内容
随时随地看视频慕课网APP