0

In my code, every time button1 is pressed an instance of a picturebox called NOT is spawned in a panel. When the image is clicked and held on it can be dragged around. My question is every time button1 is pressed I want another pictureBox of the same properties to be created so that theoretically I could press button1 all day and drag around as many NOT picturebox objects around as I want. So far once the button is pressed only one instance of NOT is created and another cannot be spawned. So essentially how do make new unique instances of NOT every time button1 is pressed.

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;
        }
    }
}

2 Answers2

4

Change NOT to a List<PictureBox>.

Then, add a new PictureBox instance to NOT in the spawnGate() method. Note that Drag() will need to be changed to take a PictureBox argument.

Edit: As requested in the comments, for the benefit of others visiting this question, here is exactly how the code would need to be changed to get the behavior requested by OP. Note that this design could and should be refactored in a few areas.

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;
        }
    };
}
Lews Therin
  • 3,707
  • 2
  • 27
  • 53
  • 1
    And `Drag` schouldn't be called in the ctor, but on the respective event. – Fildor Aug 20 '18 at 14:57
  • 1
    @AnyMoose I intentionally didn't provide and example because I suspect OP is copy-pasting code. Given the complexity of the code they posted, they should easily understand how object instantiation works. This answer should be all they need to solve their problem if they aren't just using SO as a code-writing service. – Lews Therin Aug 20 '18 at 15:03
  • @AnyMoose I've added the example you requested. – Lews Therin Aug 20 '18 at 15:22
  • How would I add to the list of pictureboxes? –  Aug 20 '18 at 15:38
  • `NOT.Add(pictureBox);` where `pictureBox` is a reference to the `PictureBox` you want to add to the `NOT` list. – Lews Therin Aug 20 '18 at 16:39
0

You don't have to save the pointer to NOT on the form directly (or you could save them to a list if you need to call them at a later point).

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;
    }
}

}

einord
  • 2,278
  • 2
  • 20
  • 26