C# 在窗体上放置同一控件的多个实例

C# 在窗体上放置同一控件的多个实例,c#,winforms,C#,Winforms,我正在用winforms制作一个应用程序,它在一个picturebox中显示一个蓝图,我需要以编程的方式在上面放置零件。这些部件需要是可点击的(因此它们应该是用户控件),然后触发相应的点击事件(点击部件应该显示该部件特有的信息)。我可以说我想在我的图片上放置自定义按钮。当然,现在我只需要单击一次事件,并根据选择更改显示的信息,尽管我不知道如何将此事件“链接”到每个创建的按钮 我在picturebox旁边有一个部件列表,选择一个部件会使相关控件出现在表单上(取消选择它会将其删除,或者至少使其隐藏)

我正在用winforms制作一个应用程序,它在一个picturebox中显示一个蓝图,我需要以编程的方式在上面放置零件。这些部件需要是可点击的(因此它们应该是用户控件),然后触发相应的点击事件(点击部件应该显示该部件特有的信息)。我可以说我想在我的图片上放置自定义按钮。当然,现在我只需要单击一次事件,并根据选择更改显示的信息,尽管我不知道如何将此事件“链接”到每个创建的按钮

我在picturebox旁边有一个部件列表,选择一个部件会使相关控件出现在表单上(取消选择它会将其删除,或者至少使其隐藏)。首先,我想我会在设计过程中创建一个控件,使它出现/消失,并在每次选择时重新定位它。问题是,用户应该能够选择多个零件,并且程序应该在蓝图上显示所有选择的零件


由于每个蓝图不同,因此无法预先定义零件的数量。是否可以在运行时创建同一控件的多个实例?或者有解决方法吗?

如果您对图片元素使用控件(不通过鼠标单击的坐标确定任何内容),并且每个图片元素仅与一个菜单控件关联,则我可以建议您使用该属性关联相应的菜单控件:

    public Form1()
    {
        InitializeComponent();

        this.CreatePictureRelatedControls();
    }

    private void CreatePictureRelatedControls()
    {
        Int32 xPictureControls = 50,
            yPictureControls = 50,
            xAssociatedControls = 200,
            yAssociatedControls = 50,
            yMargin = 10;

        Int32 controlWidth = 125,
            controlHeight = 20;


        Int32 controlCount = 3;

        // ---------Associated controls-----------------

        var associatedControls = new Button[controlCount];

        // Loop - creating associated controls
        for (int i = 0; i < associatedControls.Length; i++)
        {
            var associatedButton = new Button()
            {
                Left = xAssociatedControls,
                Top = yAssociatedControls + (i * (controlWidth + yMargin)),
                Width = controlWidth,
                Height = controlHeight,
                Text = String.Format("associated control {0}", i),
                Visible = false
            };

            // Event handler for associated button
            associatedButton.Click += (sender, eventArgs) =>
                {
                    MessageBox.Show(((Control)sender).Text, "Associated control clicked");
                };

            associatedControls[i] = associatedButton;
        }

        // ----------------- Picture controls ---------------
        var pictureControls = new Button[controlCount];

        // Loop - creating picture controls
        for (int i = 0; i < pictureControls.Length; i++)
        {
            var pictureButton = new Button()
            {
                Left = xPictureControls,
                Top = yPictureControls + (i * (controlWidth + yMargin)),
                Width = controlWidth,
                Height = controlHeight,
                Text = String.Format("picture part button {0}", i),
                // Use of tag property to associate the controls
                Tag = associatedControls[i],
                Visible = true
            };

            // Event hadler for picture button
            pictureButton.Click += (sender, eventArgs) =>
                {
                    Control senderControl = (Control)sender;
                    Control associatedControl = (Control)senderControl.Tag;

                    associatedControl.Visible = !associatedControl.Visible;
                };

            pictureControls[i] = pictureButton;
        }


        this.Controls.AddRange(associatedControls);
        this.Controls.AddRange(pictureControls);
    }

接下来是常见的问题。你试过什么?代码已经写在哪里了?你卡在哪里?如果你想“链接”
点击事件到按钮,你只需双击它(不是在运行时间),你的焦点将导航到它的
点击
event@Steve当前位置通常的回答是:在这个问题上我什么也没试过,因为我不知道怎么做。提供代码的其他部分不会帮助您理解我的问题。如果你不能理解它,告诉我你丢失了什么信息,我会编辑它,但是代码不会对你我有任何帮助。至于这个问题,我被困在哪里,已经写下来了。下次请仔细阅读。@Eli我知道;)请阅读我的问题,它是完全不同的。作为一个小提示,winforms中的控件在窗体关闭之前是不会完全释放的
button.Tag = new Control[] {associated[1], associated[3]};