[英]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:请记住:
Button
control added by the designer through the Buttons
property will be serialized like it was added in the usual design way.Buttons
属性添加的每个Button
控件都将被序列化,就像它以通常的设计方式添加一样。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.Button
object can be edited inside the ArrayEditor
form.Button
对象的所有属性都可以在ArrayEditor
表单内进行编辑。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.否则,您应该覆盖
OnControlAdded
和OnControlRemoved
以在添加或删除任何控件时收到通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.