|
-
Nov 11th, 2012, 06:48 AM
#2
Re: Help: Numeric Keypad with multi-textboxes
That code is so much more complex than is necessary. Firstly, you've written basically the same code over and over and that is never a good thing. One of the most important principles in programming is DRY: don't repeat yourself. You could simply assign the appropriate digit to the Tag property of each Button and then use a single method to handle the Click event for all of them. In the event handler, you access the Button that was clicked via the 'sender' parameter and get the digit to use from its Tag.
Secondly, you don't need If statements to determine which TextBox has focus. You simply get the ActiveControl of the form and, if it's a TextBox, append the appropriate text, which you get from the Button's Tag, e.g.
Code:
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
Dim target = TryCast(ActiveControl, TextBox)
If target IsNot Nothing Then
Dim source = DirectCast(sender, Button)
target.AppendText(CStr(source.Tag))
End If
End Sub
That will replace all your 10 Button.Click event handlers.
The other issue you have, though, is that clicking a Button will steal focus, so the ActiveControl will never be one of the TextBoxes. You need a way to click the Button without it taking focus. To learn how to do that, follow the CodeBank link in my signature and check out my On-screen Keyboard thread. The custom controls provided their are already designed for an on-screen keyboard, so there's a button control that will not take focus when clicked and also has a property specifically for the character that that "key" represents.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|