Help: Numeric Keypad with multi-textboxes-VBForums
Results 1 to 6 of 6

Thread: Help: Numeric Keypad with multi-textboxes

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    20

    Help: Numeric Keypad with multi-textboxes

    This is just one Form of my project, I was wondering if how can I create a form that has a numeric Keypad (Buttons) that has 3 text boxes

    TEXTBOX 1 : OLD PIN CODE
    TEXTBOX 2 : NEW PIN CODE
    TEXTBOX 3 : CONFIRM NEW PIN CODE

    I have 10 buttons 0,1,2...9

    Actually it's easy if I use 1 text box only, but now with 3 text boxes I found a little bit difficulty

    I am using a touch screen for my project that's why I need this built in numeric keypad to input the PIN CODE in the specific areas..

    I tried using an IF STATEMENT that when the focus is set in TEXTBOX1 Then it should write to TEXTBOX1 only then when the TEXTBOX2 is clicked then it should now Write to TEXTBOX2

    here is my CODE:

    Code:
    Public Class frmpin
    
        Private Sub cmd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click
            
            If txtpassword.Focused = True Then
                txtpassword.Text = txtpassword.Text + "1"
    
            ElseIf txtnew.Focused = True Then
                txtnew.Text = txtnew.Text + "1"
    
            ElseIf txtnew.Focused = True Then
                txtnew2.Text = txtnew2.Text + "1"
            End If
    
        End Sub
    
        Private Sub cmd2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd2.Click
           
            If txtpassword.Focused = True Then
                txtpassword.Text = txtpassword.Text + "2"
    
            ElseIf txtnew.Focused = True Then
                txtnew.Text = txtnew.Text + "2"
    
            ElseIf txtnew.Focused = True Then
                txtnew2.Text = txtnew2.Text + "2"
            End If
    
        End Sub
    
        Private Sub cmd3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd3.Click
           
            If txtpassword.Focused = True Then
                txtpassword.Text = txtpassword.Text + "3"
    
            ElseIf txtnew.Focused = True Then
                txtnew.Text = txtnew.Text + "3"
    
            ElseIf txtnew.Focused = True Then
                txtnew2.Text = txtnew2.Text + "3"
            End If
    
    
        End Sub
    
        Private Sub cmd4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd4.Click
           
            If txtpassword.Focused = True Then
                txtpassword.Text = txtpassword.Text + "4"
    
            ElseIf txtnew.Focused = True Then
                txtnew.Text = txtnew.Text + "4"
    
            ElseIf txtnew.Focused = True Then
                txtnew2.Text = txtnew2.Text + "4"
            End If
    
        End Sub
    
        Private Sub cmd5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd5.Click
            
            If txtpassword.Focused = True Then
                txtpassword.Text = txtpassword.Text + "5"
    
            ElseIf txtnew.Focused = True Then
                txtnew.Text = txtnew.Text + "5"
    
            ElseIf txtnew.Focused = True Then
                txtnew2.Text = txtnew2.Text + "5"
            End If
    
    
        End Sub
    
        Private Sub cmd6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd6.Click
           
            If txtpassword.Focused = True Then
                txtpassword.Text = txtpassword.Text + "6"
    
            ElseIf txtnew.Focused = True Then
                txtnew.Text = txtnew.Text + "6"
    
            ElseIf txtnew.Focused = True Then
                txtnew2.Text = txtnew2.Text + "6"
            End If
    
    
        End Sub
    
        Private Sub cmd7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd7.Click
           
            If txtpassword.Focused = True Then
                txtpassword.Text = txtpassword.Text + "7"
    
            ElseIf txtnew.Focused = True Then
                txtnew.Text = txtnew.Text + "7"
    
            ElseIf txtnew.Focused = True Then
                txtnew2.Text = txtnew2.Text + "7"
            End If
    
    
        End Sub
    
        Private Sub cmd8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd8.Click
    
            If txtpassword.Focused = True Then
                txtpassword.Text = txtpassword.Text + "8"
    
            ElseIf txtnew.Focused = True Then
                txtnew.Text = txtnew.Text + "8"
    
            ElseIf txtnew.Focused = True Then
                txtnew2.Text = txtnew2.Text + "8"
            End If
    
    
        End Sub
    
        Private Sub cmd9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd9.Click
           
            If txtpassword.Focused Then
                txtpassword.Text = txtpassword.Text + "9"
    
            ElseIf txtnew.Focused Then
                txtnew.Text = txtnew.Text + "9"
    
            ElseIf txtnew.Focused Then
                txtnew2.Text = txtnew2.Text + "9"
            End If
    
    
        End Sub
    
        Private Sub cmd0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd0.Click
            
            If txtpassword.Focus Then
                txtpassword.Text = txtpassword.Text + "0"
    
            ElseIf txtnew.Focused Then
                txtnew.Text = txtnew.Text + "0"
    
            ElseIf txtnew.Focused Then
                txtnew2.Text = txtnew2.Text + "0"
            End If
    
    
        End Sub
    
        Private Sub cmdcancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdcancel.Click
            Me.Hide()
        End Sub
    
        Private Sub txtpassword_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtpassword.MouseClick
            txtpassword.Text = ""
    
        End Sub
    
        Private Sub txtnew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtnew.Click
            txtnew.Text = ""
        End Sub
    
        Private Sub txtnew2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtnew2.Click
            txtnew2.Text = ""
            If txtnew2.Text <> txtnew.Text Then
                MsgBox("Please Verify Your Password again! Passwords Should Match!")
    
            End If
        End Sub
    
        Private Sub frmpin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    End Class
    Any help will be appreciated. Thank you

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,560

    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.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    20

    Re: Help: Numeric Keypad with multi-textboxes

    Thank you for the help.
    Sorry for that. i am just a starter.
    I cannot find your thread on creating the On-Screen Keyboard, I only found the On-screen Keyboard: Active Window Loses Focus for External Apps

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,560

    Re: Help: Numeric Keypad with multi-textboxes

    Hey, no one knows these principals to begin with. Unfortunately, simple principles like DRY don't get taught as early as they should either.

    Anyway, with regards to that CodeBank link, the search feature for this site still doesn't seem to work properly, or certainly not the way it did, so those links in my signature don't always show all the threads they should. You can still find the thread easily enough though. Just go to the VB.NET CodeBank forum, sort by Thread Starter and then page until you find my threads. It should about page 13 or 14.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Help: Numeric Keypad with multi-textboxes

    Hi abo0badr,

    I have an example for you that may prove useful (See my signature for the link.). Not to long ago I found myself requiring my own on-screen numpad for a project I was working on to. So I went ahead and built one. Unfortunately though it was not as simple as I had originally thought, on-screen numpads, when for the users end require some attention to detail. Some going through with a fine tooth pick and comb am I right?

    Hope it helps you out a bit, do take note in my example I had to strip away a lot because of how 'implemented' it was in my other project, design and functionality-wise.


    Feel free to message me for any question you may have. If they're useful to the thread, post it here so others can see.

    David
    Last edited by DavesChillaxin; Nov 12th, 2012 at 07:57 AM.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    20

    Re: Help: Numeric Keypad with multi-textboxes

    hello sir, I tried your example and it was pretty awesome, but I am in a little bit trouble that I cannot solve, when I run your program as it's project, it is perfectly running, but I integrated the example in my system (actually it's just a start, not yet fully functioning, I just created it) and the numpad comes up, but it does not code any numbers..

    maybe because I used it in a panel? Here attached my Program, I hope you can find what is wrong,

    http://www.mediafire.com/download.php?cce6f9eyd361jhs

    When the Program start just click Run System then choose the button (Change PIN).. thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.