1) 创建一个 WinForms 项目,我称之为“ReusingUserControlsSample”
2) 创建一个新的 UserControl,将其命名为 MyUserControlWithButtons 或您喜欢的任何其他名称
3) 只是出于习惯,在 UserControl 属性上设置“AutoSize=true”和 AutoSizeMode="GrowAndShrink"。稍后您可能会了解他们的工作
4) 在 UserControlDesigner 上,在控件上放置一些按钮,将它们命名为“btnLetterA”、“btnLetterB”、“btnLetterC”
5) 双击每个按钮,将生成点击处理程序
6) 在您的 UserControl 代码中,创建一个 public TextBox TheOutput 属性
7) 在您的 UserControl 代码中,在您在步骤 (5) 中生成的每个单击处理程序中,添加一行,将一些文本添加到 TheOutput 文本框的 TextBox 属性。请记住检查TheOutput 是否为NULL。
构建。
8) 返回 Form1
9) 将MyUserControlWithButtons 放在表单上,将其命名为“mykeyboard”
10) 在表单上放置一个 TextBox,将其命名为“mytextbox”
11) 转到 Form1 的代码
12)在te构造函数中,在“InitializeComponent”下方,将mytextbox分配给mykeyboard的TheOutput
就是这样。现在您可以构建并运行它,一切都应该没问题。请不要将“键盘”的整个代码都放在用户控件中。该表单仅将其设置为与 that 文本框一起使用。在第二种形式中,您可以以相同的方式执行此操作:放置键盘、放置文本框、设置键盘以写入该文本框,它的工作方式相同。
代码:
MyUserControlWithButtons.cs
using System;
using System.Windows.Forms;
namespace ReusingUserControlsSample
{
public partial class MyUserControlWithButtons : UserControl
{
public MyUserControlWithButtons()
{
InitializeComponent();
}
public TextBox TheOutput { get; set; }
private void btnLetterA_Click(object sender, EventArgs e)
{
TheOutput.Text += "A";
}
private void btnLetterB_Click(object sender, EventArgs e)
{
TheOutput.Text += "B";
}
private void btnLetterC_Click(object sender, EventArgs e)
{
TheOutput.Text += "C";
}
}
}
MyUserControlWithButtons.cs
namespace ReusingUserControlsSample
{
partial class MyUserControlWithButtons
{
/// <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.btnLetterA = new System.Windows.Forms.Button();
this.btnLetterB = new System.Windows.Forms.Button();
this.btnLetterC = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnLetterA
//
this.btnLetterA.Location = new System.Drawing.Point(3, 3);
this.btnLetterA.Name = "btnLetterA";
this.btnLetterA.Size = new System.Drawing.Size(66, 21);
this.btnLetterA.TabIndex = 0;
this.btnLetterA.Text = "The \"A\"";
this.btnLetterA.UseVisualStyleBackColor = true;
this.btnLetterA.Click += new System.EventHandler(this.btnLetterA_Click);
//
// btnLetterB
//
this.btnLetterB.Location = new System.Drawing.Point(66, 30);
this.btnLetterB.Name = "btnLetterB";
this.btnLetterB.Size = new System.Drawing.Size(66, 21);
this.btnLetterB.TabIndex = 0;
this.btnLetterB.Text = "The \"B\"";
this.btnLetterB.UseVisualStyleBackColor = true;
this.btnLetterB.Click += new System.EventHandler(this.btnLetterB_Click);
//
// btnLetterC
//
this.btnLetterC.Location = new System.Drawing.Point(3, 57);
this.btnLetterC.Name = "btnLetterC";
this.btnLetterC.Size = new System.Drawing.Size(66, 21);
this.btnLetterC.TabIndex = 0;
this.btnLetterC.Text = "The \"C\"";
this.btnLetterC.UseVisualStyleBackColor = true;
this.btnLetterC.Click += new System.EventHandler(this.btnLetterC_Click);
//
// MyUserControlWithButtons
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.Controls.Add(this.btnLetterC);
this.Controls.Add(this.btnLetterB);
this.Controls.Add(this.btnLetterA);
this.Name = "MyUserControlWithButtons";
this.Size = new System.Drawing.Size(135, 81);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button btnLetterA;
private System.Windows.Forms.Button btnLetterB;
private System.Windows.Forms.Button btnLetterC;
}
}
Form1.cs
using System.Windows.Forms;
namespace ReusingUserControlsSample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
mykeyboard.TheOutput = mytextbox;
}
}
}
Form1.Designer.cs
namespace ReusingUserControlsSample
{
partial class Form1
{
/// <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 Windows Form 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.mytextbox = new System.Windows.Forms.TextBox();
this.mykeyboard = new ReusingUserControlsSample.MyUserControlWithButtons();
this.SuspendLayout();
//
// mytextbox
//
this.mytextbox.Location = new System.Drawing.Point(84, 38);
this.mytextbox.Name = "mytextbox";
this.mytextbox.Size = new System.Drawing.Size(100, 20);
this.mytextbox.TabIndex = 0;
//
// mykeyboard
//
this.mykeyboard.AutoSize = true;
this.mykeyboard.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.mykeyboard.Location = new System.Drawing.Point(66, 122);
this.mykeyboard.Name = "mykeyboard";
this.mykeyboard.Size = new System.Drawing.Size(135, 81);
this.mykeyboard.TabIndex = 1;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 264);
this.Controls.Add(this.mykeyboard);
this.Controls.Add(this.mytextbox);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox mytextbox;
private MyUserControlWithButtons mykeyboard;
}
}
程序.cs
using System;
using System.Windows.Forms;
namespace ReusingUserControlsSample
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}