在表单上放置相同控件的多个实例

本文关键字:控件 实例 表单 | 更新日期: 2023-09-27 18:17:50

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

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

由于每张图纸不同,零件数量无法提前确定。是否有可能在运行时创建同一控件的多个实例?还是有变通的办法?

在表单上放置相同控件的多个实例

如果您为图片元素使用控件(您不从鼠标单击的坐标确定任何内容),并且每个图片元素仅与一个菜单控件相关联,那么我可以建议您使用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);
    }

注:如果你需要关联多个控件,那么你可以将Tag属性设置为一些集合:

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