Moh*_*oth 6 c# .net-3.5 visual-studio-2008 windows-forms-designer winforms
我正在创建一个自定义控件来设置单选按钮(不,它不一定是单选按钮.我只是想学习如何做到这一点,所以我可以制作一些更复杂的东西,可能包含多个通过Items属性添加的控件列表(类似于其他一些控件).
我可以构建项目,将其从组件面板拖到窗体上,并通过Items属性添加单选按钮.不幸的是,除非您要么:
起初我有一个逻辑,在Initialize之后将它们放在构造函数中包含的表单上但是没有用,所以我转到了Form_Load.
我错过了什么?以上选项只是短期解决方法,而不是解决方案.
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 List<RadioButton> _items;
private int leftSpacing = 100;
private int topSpacing = 25;
public RBLTest()
{
_items = new List<RadioButton>();
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 List<RadioButton> Items
{
get
{
return _items;
}
set
{
_items = value;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
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
}
}
Run Code Online (Sandbox Code Playgroud)
您不应该使用Load事件或构造函数,因为当您使用设计器工具添加控件时,UserControl将创建一个实例并Load触发该事件。在你的情况下,发生这种情况时_item仍然是空的。另一个问题是序列化列表时存在一些问题,因此我使用数组:
public partial class RBLTest : UserControl {
private RadioButton[] _items;
private int leftSpacing = 100;
private int topSpacing = 25;
public RBLTest( ) {
InitializeComponent( );
}
[DesignerSerializationVisibility( DesignerSerializationVisibility.Content )]
public RadioButton[] Items {
get {
return _items;
}
set {
_items = value;
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 );
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
表单设计器中的结果:

| 归档时间: |
|
| 查看次数: |
4247 次 |
| 最近记录: |