I have two forms. On the first form I have a virtual numpad (I have a GroupBox
and inside I have number buttons and this is my virtual numpad). With this virtual numpad I enter numbers into a TextBox
. On the second form I have another TextBox
where I enter numbers.
I want to use my virtual numpad on this second form. How can I do that?
If someone explained to me what I should do, step by step, I will be pleased.
1) Create a WinForms project, I called it "ReusingUserControlsSample"
2) Create a new UserControl, name it MyUserControlWithButtons
or whatever else you like
3) Just out of habit, set "AutoSize=true" and AutoSizeMode="GrowAndShrink" on the UserControl properties. Later you may learn what they do
4) On the UserControlDesigner place some button on the control, name them "btnLetterA", "btnLetterB", "btnLetterC"
5) Double click on each of the buttons, so the click-handlers will be generated
6) In your UserControl's code, make a public TextBox TheOutput
property
7) In your UserControl's code, in each of the click-handlers you've generated in step (5), add a line that adds some text to the TheOutput
textbox's TextBox
property. Remeber to check the TheOutput
for NULL.
BUILD.
8) go back to Form1
9) Place MyUserControlWithButtons
on the form, name it "mykeyboard"
10) Place a TextBox on the form, name it "mytextbox"
11) Go to the Form1's code
12) in te constructor, BELOW the "InitializeComponent", asign the mytextbox to the TheOutput
of mykeyboard
And this is it. Now you can build it and run, and everything should be OK. Please not that whole code of the 'keyboard' is in the usercontrol. The form only has set it up to work with that textbox. On the second form you can do it in the same way: place keyboard, place textbox, setup the keyboard to write to that textbox and it will work the same.
The Code:
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());
}
}
}