[英]How do I use the UserControl?
Developer Environment 开发人员环境
Create UserControl 创建UserControl
// ------- testControl.Designer.cs -------------------------------------- //------- testControl.Designer.cs ------------------------------------ -
partial class testTextBox<T> { 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.textBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // textBox1 this.textBox1.Location = new System.Drawing.Point(3, 2); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(147, 21); this.textBox1.TabIndex = 1; this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.testTextBox_KeyPress); // testTextBox this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.textBox1); this.Margin = new System.Windows.Forms.Padding(0); this.Name = "testTextBox"; this.Size = new System.Drawing.Size(157, 26); this.Resize += new System.EventHandler(this.testTextBox_Resize); this.ResumeLayout(false); this.PerformLayout(); } private System.Windows.Forms.TextBox textBox1; }
// ------- testControl.cs ---------------------------------------------------- // ------- testControl.cs -------------------------------------- --------------
public partial class testTextBox<T> : UserControl { public delegate void DelegateKeyPress(object sender, KeyPressEventArgs e); public event DelegateKeyPress testKeyPress; public testTextBox() { InitializeComponent(); } public T _testNext; // Generic property public T testNext { get { return this._testNext; } set { this._testNext = value; } } private void testTextBox_KeyPress(object sender, KeyPressEventArgs e) { if (this._testNext != null) { MessageBox.Show(this._testNext.GetType().Name); } if (this.testKeyPress != null) { this.testKeyPress(sender, e); } } }
Using testTextBox 使用testTextBox
1.Make sure usercontrol and mainform have same namespace. 1.确保usercontrol和mainform具有相同的名称空间。
Build Solution 建立解决方案
Double Click testControl.cs for open Design window. 双击testControl.cs以打开“设计”窗口。
Go to toolbox, and find ** Comopnets ( ** is your project name ) 转到工具箱,然后找到** Comopnets(**是您的项目名称)
your usercontrol is in there. 您的用户控件位于其中。 just add and use. 只需添加并使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.