简体   繁体   English

按下按钮时如何创建所需数量的对象实例?c#winforms

[英]How do I create as many instances of an object as i want when a button is press c# winforms

In my code, every time button1 is pressed an instance of a picturebox called NOT is spawned in a panel. 在我的代码中,每次按下button1时,就会在面板中生成一个名为NOT的图片框实例。 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. 我的问题是每次按下button1时,我都希望创建另一个具有相同属性的pictureBox,以便从理论上讲,我可以整天按下button1并根据需要拖动任意数量的NOT picturebox对象。 So far once the button is pressed only one instance of NOT is created and another cannot be spawned. 到目前为止,一旦按下按钮,只会创建一个NOT实例,而不能生成另一个实例。 So essentially how do make new unique instances of NOT every time button1 is pressed. 因此,从本质上讲,每次按下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;
        }
    }
}

Change NOT to a List<PictureBox> . NOT更改为List<PictureBox>

Then, add a new PictureBox instance to NOT in the spawnGate() method. 然后,在spawnGate()方法中将一个new PictureBox实例添加到NOT Note that Drag() will need to be changed to take a PictureBox argument. 请注意,将需要更改Drag()以采用PictureBox参数。

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. 编辑 :按照注释中的要求,为了使其他访问此问题的人受益,这正是需要如何更改代码才能获得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;
        }
    };
}

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). 您不必直接在表单上保存指向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;
    }
}

} }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次我按下按钮时,我希望它指示 C# 中我的文本框或文本框何时为空 - Evertime I press a button I want it to indicate when my textbox or textboxes are empty in C# 当我在C#应用程序上执行语音转文本时,我希望能够尽可能多地执行该操作。 我怎样才能做到这一点? - When I perform a speech-to-text on C# app I want to be able to do that as many time as I want. How can I do that? 如何使用实体框架C#创建和插入一对多对象 - How do I create and insert one-to-many object with entity framework c# C#当我按下按钮时如何在2D数组中分配i和j - C# How to assign i and j in 2D array when i press on a button 使用“SAPFEWSELib”的 C# SAP 自动化。 如何按下下拉列表中的按钮? - C# SAP automation using “SAPFEWSELib”. How do I press a button from a drop down list? 如何使用winforms-geplugin API创建自定义地标图标? (使用C#而不是Javascript) - How do I create a custom placemark icon using winforms-geplugin API? (using C# not Javascript) 如何在C#WinForms应用程序中创建可编辑的配置设置? - How do I create editable configuration settings in a C# WinForms application? 我希望当我按Enter按钮然后确定按钮工作时 - I want that when I press Enter Button then ok button work 按下按钮时如何进行GUI弹出? - How do I make a GUI popup when I press a Button? 按下按钮时如何调用静态方法? - How do I call a static method when I press a button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM