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
MyUserControlWithButtonsor whatever else you like3) 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 TheOutputproperty7) 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
TheOutputtextbox’sTextBoxproperty. Remeber to check theTheOutputfor NULL.BUILD.
8) go back to Form1
9) Place
MyUserControlWithButtonson 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
TheOutputof mykeyboardAnd 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
MyUserControlWithButtons.cs
Form1.cs
Form1.Designer.cs
Program.cs