我有一个用于Windows窗体的UserControl。如何将其转换为组件?我想要做的是,将它添加到VS工具箱,在设计时通过拖放将其添加到表单,并使用“属性”窗口更改其位置和Dock属性。我该怎么做呢?
我之前创建过类似的东西,但它是一个Component类。我可以将它添加到VS工具箱,使用拖放功能将其添加到表单中,并使用“属性”窗口更改其自定义属性,但由于它是在底部组件区域显示的组件,并且没有设计时支持。
答案 0 :(得分:1)
UserControl
是Component
。你没有必要做任何额外的事情来做到这一点。
如果在VS中创建新的UserControl
,它将自动拥有您正在寻找的行为:从工具箱拖到表单上,通过“属性”面板更改其属性。
此代码创建在工具箱中显示自身的功能控件,并允许更改其属性。
public class TestUserControl : UserControl
{
public TestUserControl() {
InitializeComponent();
}
/// <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.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoEllipsis = true;
this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(231, 51);
this.label1.TabIndex = 0;
this.label1.Text = "This is a test user control";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// TestUserControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.Controls.Add(this.label1);
this.Name = "TestUserControl";
this.Size = new System.Drawing.Size(231, 51);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label label1;
}
也许你可以将你的功能添加到这个类中,看看它是否有效。