简体   繁体   English

如何使用UserControl?

[英]How do I use the UserControl?

  1. Developer Environment 开发人员环境

    • Tools : Visual Studio 2017 工具:Visual Studio 2017
    • Language: C# - Windows Form 语言:C#-Windows窗体
  2. Create UserControl 创建UserControl

    • testNext Property is Generic Type testNext属性是通用类型

    // ------- 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); } } } 
  3. Using testTextBox 使用testTextBox

    • I want to use testTextBox in a form, but I can not use it in Design Mode How do I use the testTextBox? 我想在表单中使用testTextBox,但不能在设计模式下使用它。如何使用testTextBox?

1.Make sure usercontrol and mainform have same namespace. 1.确保usercontrol和mainform具有相同的名称空间。

  1. Build Solution 建立解决方案

  2. Double Click testControl.cs for open Design window. 双击testControl.cs以打开“设计”窗口。

  3. Go to toolbox, and find ** Comopnets ( ** is your project name ) 转到工具箱,然后找到** Comopnets(**是您的项目名称)

  4. your usercontrol is in there. 您的用户控件位于其中。 just add and use. 只需添加并使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM