简体   繁体   English

<Button>列为属性。</button> <Button>不向面板添加按钮</button>

[英]List<Button> as properties. Not adding buttons to panel

I am trying to create a custom panel where I can change the properties using Properties -> Buttons -> [(Collection) ...]:我正在尝试创建一个自定义面板,我可以在其中使用 Properties -> Buttons -> [(Collection) ...] 更改属性:

The following is what I have tried以下是我尝试过的

public class CustomPanel : Panel
{
    private List<Button> buttons = new List<Button>();

    public List<Button> Buttons
    {
        get { return buttons; }
        set 
        {
             buttons = value;
             this.Controls.Clear();
                
             foreach (var button in buttons)
             {
                button.Size = new Size(200, 30);
                this.Controls.Add(button);
             }
         }
    }
}

But when I add a new button using Properties -> Buttons -> [(Collection) ...]: it is not adding it to my panel.但是当我使用 Properties -> Buttons -> [(Collection) ...] 添加一个新按钮时:它没有将它添加到我的面板中。

Assuming a CustomPanel class has been added as a custom control, and its base class changed to Panel , you can try the following code.假设添加了一个CustomPanel类作为自定义控件,并将其基类更改为Panel ,您可以尝试以下代码。

using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Design;
using System.Linq;
using System.Windows.Forms;

namespace TEST
{
    public partial class CustomPanel : Panel
    {
        public CustomPanel()
        {
            InitializeComponent();
        }

        [Editor(typeof(ArrayEditor), typeof(UITypeEditor))]
        public Button[] Buttons
        {
            get { return this.Controls.OfType<Button>().ToArray(); }
            set
            {
                SuspendLayout();
                try
                {
                    this.Controls.Clear();

                    foreach (var button in value)
                    {
                        button.Size = new Size(200, 30);
                        this.Controls.Add(button);
                    }
                }
                finally { ResumeLayout(); }
            }
        }
    }
}

Make sure the project has a reference to System.Design.dll , for ArrayEditor to be recognized.确保项目具有对System.Design.dll的引用,以便识别ArrayEditor

Keep in mind that:请记住:

  • Every Button control added by the designer through the Buttons property will be serialized like it was added in the usual design way.设计者通过Buttons属性添加的每个Button控件都将被序列化,就像它以通常的设计方式添加一样。
  • Adding other (non- Button ) controls is still possibile in the usual design way, or programmatically.添加其他(非Button )控件仍然可以以通常的设计方式或以编程方式添加。
  • this.Controls.Clear will remove any control, even the ones added without using Buttons ; this.Controls.Clear将删除任何控件,即使是不使用Buttons添加的控件; you can delete this instruction if you want to preserve them, but you will need a way to recognize the previously existing buttons and replace or keep them as needed.如果您想保留它们,您可以删除此指令,但您需要一种方法来识别以前存在的按钮并根据需要替换或保留它们。
  • All properties of any Button object can be edited inside the ArrayEditor form.任何Button对象的所有属性都可以在ArrayEditor表单内进行编辑。
  • You need to handle positioning of the Button controls (both newly added and existing ones) in order to avoid overlapping.您需要处理Button控件(新添加的和现有的)的定位以避免重叠。

I suggest to use a child FlowLayoutPanel for automatic positioning of the buttons and to prevent anyone to mess directly with the collection of buttons.我建议使用子FlowLayoutPanel来自动定位按钮并防止任何人直接弄乱按钮集合。 With this approach, this.Controls will become flowLayoutPanel1.Controls (or similar).使用这种方法, this.Controls将变成flowLayoutPanel1.Controls (或类似的)。 Otherwise, you should override OnControlAdded and OnControlRemoved to be notified when any control is added or removed.否则,您应该覆盖OnControlAddedOnControlRemoved以在添加或删除任何控件时收到通知。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM