在此用户控件中,设计者将项目添加到表单上的此控件中,并动态地将它们放置在表单上。不幸的是,一旦这些控件显示在设计器中的表单上,我就可以抓住一个并将其拉到自定义控件之外。
我该如何防止这种情况?
编辑
请注意,我不是在寻找处理单选按钮的解决方案或替代方案,这只是这种情况下的选择控制。如果它有助于在此处插入您想要的任何类型的控件(按钮、标签、文本框、自定义控件 1),则此问题与处理具有集合/数组类型的属性有关。最重要的是,用户不应该能够在设计器中抓取任何这些控件,其中定义了该控件的实例并将其移动到其他地方。就像 DataGridView 或 ListView 一样,尽管对象类型不同,但用户必须使用属性来更改这些控件的内容。这就是我正在寻找的行为。
RBLTest.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WFCL_Library
{
public partial class RBLTest : UserControl
{
private RadioButton[] _items;
private int leftSpacing = 100;
private int topSpacing = 25;
public RBLTest()
{
InitializeComponent();
}
private void RadioButtonList_Load(object sender, EventArgs e)
{
int curLeftPos = 0;
int curTopPos = 0;
foreach (RadioButton rb in _items)
{
rb.Location = new Point(curLeftPos, curTopPos);
rb.Size = new Size(85, 17);
curLeftPos += leftSpacing;
if (curLeftPos > this.Width)
{
curLeftPos = 0;
curTopPos += topSpacing;
}
this.Controls.Add(rb);
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public RadioButton[] Items
{
get
{
return _items;
}
set
{
_items = value;
}
}
}
}
RBLTest.Designer.cs
namespace WFCL_Library
{
partial class RBLTest
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// RBLTest
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Name = "RBLTest";
this.Size = new System.Drawing.Size(407, 44);
this.Load += new System.EventHandler(this.RadioButtonList_Load);
this.ResumeLayout(false);
}
#endregion
}
}