We want to create a usercontrol as container for maintain button status and arrange button location called ucSwitchButtonList.
This usercontrol added only one FlowLayoutControl.
After compiling, you can add ucSwitchButtonList on the form and successfully add buttons from CollectionEditor. ucSwitchButtonList on the form
We can modify button objects added in SwitchButtonList and set properties at design time.
But, when we drag any button at design time, the Visual Studio throw an NullReferenceException exception.
How to prevent user from dragging buttons in SwitchButtonList at design time? Or how to handle drag event at design time?
Thanks in advanced!
excetion message
enter image description here
[ucSwitchButtonList.Designer.cs]
partial class ucSwitchButtonList
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.flpBtnContainer = new System.Windows.Forms.FlowLayoutPanel();
this.SuspendLayout();
//
// flpBtnContainer
//
this.flpBtnContainer.AutoScroll = true;
this.flpBtnContainer.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.flpBtnContainer.Location = new System.Drawing.Point(1, 1);
this.flpBtnContainer.Margin = new System.Windows.Forms.Padding(0);
this.flpBtnContainer.Name = "flpBtnContainer";
this.flpBtnContainer.Size = new System.Drawing.Size(116, 91);
this.flpBtnContainer.TabIndex = 0;
//
// ucSwitchButtonList
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.Controls.Add(this.flpBtnContainer);
this.DoubleBuffered = true;
this.Font = new System.Drawing.Font("新細明體", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
this.Name = "ucSwitchButtonList";
this.Padding = new System.Windows.Forms.Padding(1);
this.Size = new System.Drawing.Size(209, 202);
this.Load += new System.EventHandler(this.ucSwitchButtonList_Load);
this.ResumeLayout(false);
}
private System.Windows.Forms.Panel flpBtnContainer;
}
[ucSwitchButtonList.cs]
public partial class ucSwitchButtonList : UserControl
{
ObservableCollection<Button> _ButtonList = new System.Collections.ObjectModel.ObservableCollection<Button>();
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Browsable(true), EditorBrowsable(EditorBrowsableState.Always),
Editor(typeof(CollectionEditor), typeof(UITypeEditor))
]
public ObservableCollection<Button> ButtonList
{
get => _ButtonList;
}
public ucSwitchButtonList()
{
InitializeComponent();
flpBtnContainer.Padding = new Padding(0);
flpBtnContainer.Size = this.Size;
flpBtnContainer.Dock = DockStyle.Fill;
_ButtonList.CollectionChanged += ButtonList_CollectionChanged;
Size = new Size(80, 160);
}
private void ucSwitchButtonList_Load(object sender, EventArgs e)
{
}
private void ButtonList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
flpBtnContainer.SuspendLayout();
flpBtnContainer.Controls.Clear();
flpBtnContainer.Controls.AddRange(_ButtonList.ToArray());
flpBtnContainer.ResumeLayout();
foreach (Control ctl in flpBtnContainer.Controls)
{
if (ctl is Button btn)
{
btn.Click += Btn_Click;
if (e.NewItems.Contains(btn))
{
btn.BackColor = Color.White;
}
}
}
Invalidate(true);
}
private void Btn_Click(object sender, EventArgs e)
{
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (DesignMode && BorderStyle == BorderStyle.None)
{
var rect = Rectangle.Inflate(ClientRectangle, 0, 0);
rect.Width--;
rect.Height--;
Color lc = ControlPaint.Dark(Parent.BackColor);
开发者_Go百科 if (lc == Parent.BackColor) lc = ControlPaint.LightLight(Parent.BackColor);
using (Pen lp = new Pen(lc))
{
lp.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
lp.Alignment = System.Drawing.Drawing2D.PenAlignment.Center;
e.Graphics.DrawRectangle(lp, rect);
}
}
}
}
How to prevent user from dragging buttons in SwitchButtonList at design time? Or how to handle drag event at design time?
Thanks in advanced!
精彩评论