【问题标题】:How do I create as many instances of an object as i want when a button is press c# winformsc# winforms 如何在按下按钮时创建尽可能多的对象实例
【发布时间】:2018-08-20 23:22:33
【问题描述】:

在我的代码中,每次按下 button1 时,都会在面板中生成一个名为 NOT 的图片框实例。单击并按住图像时,可以拖动图像。我的问题是每次按下 button1 时,我都希望创建另一个具有相同属性的图片框,这样理论上我可以整天按下 button1 并随意拖动任意数量的非图片框对象。到目前为止,一旦按下按钮,就只创建了一个 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;
        }
    }
}

【问题讨论】:

    标签: c# winforms instance picturebox


    【解决方案1】:

    NOT 更改为List<PictureBox>

    然后,在spawnGate() 方法中将new PictureBox 实例添加到NOT。请注意,Drag() 需要更改为采用 PictureBox 参数。

    编辑:根据 cmets 的要求,为了其他访问此问题的人的利益,这正是需要更改代码以获得 OP 请求的行为的方式。请注意,这种设计可以而且应该在几个方面进行重构。

    List<PictureBox> NOT = new List<PictureBox>();
    Point startPoint = new Point();
    
    public Form1()
    {
        InitializeComponent();
        Drag();
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        spawnGate();
    }
    
    public void spawnGate()
    {
        var pictureBox = new PictureBox()
        {
            Width = 100,
            Height = 50,
            Image = Properties.Resources.Not_gate,
            SizeMode = PictureBoxSizeMode.Zoom       
        }
    
        Drag(pictureBox);
    
        NOT.Add(pictureBox);
    
        workspace.Controls.Add(pictureBox);
    }
    
    public void Drag(PictureBox pictureBox)
    {
        pictureBox.MouseDown += (ss, ee) => 
        {
            if (ee.Button == System.Windows.Forms.MouseButtons.Left)
                startPoint = Control.MousePosition;
        };
    
        pictureBox.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);
    
                pictureBox.Location = new Point(pictureBox.Location.X - pictureBox.X, pictureBox.Location.Y - res.Y);
    
                startPoint = temp;
            }
        };
    }
    

    【讨论】:

    • Drag 不应在 ctor 中调用,而是在相应事件中调用。
    • @AnyMoose 我故意没有提供示例,因为我怀疑 OP 是复制粘贴代码。鉴于他们发布的代码的复杂性,他们应该很容易理解对象实例化的工作原理。如果他们不只是将 SO 用作代码编写服务,那么这个答案应该是他们解决问题所需要的全部。
    • @AnyMoose 我已经添加了您要求的示例。
    • 如何添加到图片框列表中?
    • NOT.Add(pictureBox); 其中pictureBox 是对您要添加到NOT 列表中的PictureBox 的引用。
    【解决方案2】:

    您不必直接将指向 NOT 的指针保存在表单上(或者,如果您需要稍后调用它们,您可以将它们保存到列表中)。

    public Form1()
    {
        InitializeComponent();
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        spawnGate("not");
    }
    
    // This list is optional, if you easily want to find them later
    List<PictureBox> allNOTs = new List<PictureBox>();
    
    public void spawnGate(string type)
    {
        switch (type)
        {
            case "not":
                PictureBox NOT = new PictureBox();
                NOT.Width = 100;
                NOT.Height = 50;
                NOT.Image = Properties.Resources.Not_gate;
                NOT.SizeMode = PictureBoxSizeMode.Zoom;
                NOT.MouseDown += (ss, ee) =>
                {
                    // Mouse down event code here
                };
                NOT.MouseMove += (ss, ee) =>
                {
                    // Mouse move event code here
                };
                allNOTS.Add(NOT); // Optional if you easily want to find it later
                workspace.Controls.Add(NOT);
            break;
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多