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

时间:2014-08-13 07:19:15

标签: c# winforms

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

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

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

1 个答案:

答案 0 :(得分:1)

如果你对图片元素使用控件(你没有从鼠标点击的坐标确定任何东西)并且每个图片元素只与一个菜单控件相关联,那么我可以建议你使用Tag属性来关联相应的菜单控件:

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

P.S。如果您需要关联多个控件,那么您只需将Tag属性设置为某个集合:

button.Tag = new Control[] {associated[1], associated[3]};