我有两个表格。在第一个表单中,我有一个虚拟的数字键盘(我有一个GroupBox,里面有数字按钮,这是我的虚拟数字键盘)。使用这个虚拟的数字键盘,我将数字输入到TextBox中。在第二个表单中,我有另一个TextBox,我可以在其中输入数字。
我想在第二个表单上使用我的虚拟数字键盘。我该怎么做呢?
如果有人一步一步地向我解释我应该做什么,我会很高兴。
发布于 2012-08-11 07:59:40
1)创建一个WinForms项目,我叫它"ReusingUserControlsSample“
2)创建一个新的UserControl,将其命名为MyUserControlWithButtons或其他您喜欢的名称
3)出于习惯,在UserControl属性上设置AutoSize=true和AutoSizeMode=GrowAndShrink。稍后,您可能会了解它们的用途
4)在控件上放置一些按钮,将它们命名为"btnLetterA","btnLetterB","btnLetterC“
5)双击每个按钮,这样就会生成点击处理程序
6)在用户控件的代码中,创建一个public TextBox TheOutput属性
7)在用户控件的代码中,在步骤(5)中生成的每个单击处理程序中,添加一行向TheOutput textbox的TextBox属性添加一些文本。记住检查TheOutput是否为空。
构建。
8)返回Form1
9)将键盘放在表单上,将其命名为“myMyUserControlWithButtons”
10)在表单上放置一个TextBox,将其命名为"mytextbox“
11)转到Form1的代码
12)在te构造函数中,在"InitializeComponent“下面,将mytextbox赋值给mykeyboard的TheOutput
这就是了。现在您可以构建并运行它了,一切都应该正常了。请不要说“键盘”的全部代码都在用户控件中。表单仅将其设置为与文本框一起使用。在第二种情况下,你可以用同样的方法:放置键盘,放置文本框,将键盘设置为写入该文本框,它将同样起作用。
代码:
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;
}
}Program.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());
}
}
}发布于 2012-08-11 06:43:47
创建一个UserControl,并将您的虚拟数字键盘分组框/按钮放在上面。然后在每个表单上替换现有的组框/按钮,添加新的用户控件。
发布于 2012-08-11 06:53:00
您有两个选择:
https://stackoverflow.com/questions/11910144
复制相似问题