Solved

Text Boxes For Activation Code

Posted on 2014-03-27
16
321 Views
Last Modified: 2014-03-31
Experts, I am creating a form where the user will active the software. The user will be presented with a registration key and email that to an administrator who will generate the activation code and send back to the user. What I am having trouble trying to figure out how to do is to make the text boxes for the activation code mimic what you normally see when presented with software activation. The activation code will be 30 characters. There are 5 text boxes and each text box will hold 6 characters. So what i want to do is when the user has entered 6 characters i want to cursor to jump to the next text box. And to do the reverse if the user back spaces.

Please help me to figure this one out in the most efficient manner.

Thanks...

Screen Shot of Form
0
Comment
Question by:Basicfarmer
  • 7
  • 6
  • 3
16 Comments
 
LVL 5

Expert Comment

by:Pasha Kravtsov
Comment Utility
This should work and just do it for every text box you have.
Private Sub textBox1()
        if len(text1.text)=6 then
                texBox2.setfocus
        endif
End Sub 

Open in new window

0
 

Author Comment

by:Basicfarmer
Comment Utility
How to i do it in reverse and put the cursor at the end of the text string?
0
 
LVL 5

Expert Comment

by:Pasha Kravtsov
Comment Utility
Private Sub textBox1()
        if len(text2.text)=0 then
                texBox1.setfocus
        endif
End Sub 

Open in new window

0
 
LVL 23

Expert Comment

by:Jens Fiederer
Comment Utility
I really think you'd want to do it on one of the key events, that way you can catch the backspace.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
Comment Utility
Same basic idea as Pasha, but control when the logic is triggered and keep other keys such as control keys and tab from bringing you to the previous field:

    Private Sub TextBox2_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyUp
        If TextBox2.Text.Length = 6 Then
            TextBox3.Focus()
        End If
        If TextBox2.Text.Length = 0 And e.KeyCode = 8 Then
            TextBox1.Focus()
        End If
    End Sub

Open in new window

0
 
LVL 5

Expert Comment

by:Pasha Kravtsov
Comment Utility
Yes jen's backspace to go to the previous text box is a better way to do it then I did.
0
 

Author Comment

by:Basicfarmer
Comment Utility
OK, here is what i have come up with. I had to use both the key up and key down events. This works like i want it to save for two things.

The text boxes shown in the form screen shot are for entering sections of the activation code. The text boxes are named txtAct0, txtAct1 and so on. There are five text boxes.

So for my first problem:
If the user enters 6 characters into the text box "txtAct0", on the key up event of the last character the cursor jumps over to "txtAct1" just like it should. Now if the user uses the back space key the cursor jumps back to "txtAct0" just like it should. Now if the user presses another key, in the key down event the cursor is moved to "txtAct1" and the key the user pressed is displayed in "txtAct1" and the cursor is placed after the character so it is in position for another key to be entered. But what happens with this sequence of events is that there is a chime on the key down event of "txtAct0" like there is some type of error. I cannot figure this one out. Is there a conflict somewhere that is causing this sound.

Here is my next issue:
When back spacing it works with the key events just fine. If you fill out all of the text boxes and then start pressing the back space key when all of the characters of one text box have been deleted the cursor jumps right to the previous text box as it should. But I want it to jump to the next text box if the user holds down the back space key. So if they had filled out all of the text boxes and the cursor were in front of the last character in "txtAct4" and they just held down the back space key it would delete all the character in "txtAct4" and then jump right to "txtAct3" and so on until it had deleted all characters in "txtAct0".

 
    Private Sub txtAct0_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtAct0.KeyDown, _
        txtAct1.KeyDown, txtAct2.KeyDown, txtAct3.KeyDown, txtAct4.KeyDown

        'All of the key down events for the activation text boxes are handled here.

        'Make sure that only numeric keys, alphanumeric keys, the backspace key and delete key function.
        If (e.KeyValue < 48 Or e.KeyValue > 57) And _
           (e.KeyValue < 65 Or e.KeyValue > 90) And _
           (e.KeyValue < 97 Or e.KeyValue > 122) And _
            e.KeyCode <> Keys.Delete And _
            e.KeyCode <> Keys.Back Then e.Handled = True

        'The MaxLength property of each activation text box is set to 6 in the designer.

        'Filters:
        'Key pressed is not the back space key and not the delete key.
        'The number of characters in the text box is equal to the MaxLength property of the text box.
        'Shift the cursor to the next text box.
        'Display the character that was pressed in the text box.
        'Move the cursor to the right of the character to be ready to accept another character.
        If Not e.KeyCode = Keys.Back And Not e.KeyCode = Keys.Delete Then
            If ActiveControl.Name = "txtAct0" Then
                If txtAct0.TextLength = txtAct0.MaxLength Then
                    txtAct1.Focus()
                    txtAct1.Text = e.KeyCode.ToString
                    txtAct1.SelectionStart = txtAct1.TextLength
                End If
            End If
            If ActiveControl.Name = "txtAct1" Then
                If txtAct1.TextLength = txtAct1.MaxLength Then
                    txtAct2.Focus()
                    txtAct2.Text = e.KeyCode.ToString
                    txtAct2.SelectionStart = txtAct2.TextLength
                End If
            End If
            If ActiveControl.Name = "txtAct2" Then
                If txtAct2.TextLength = txtAct2.MaxLength Then
                    txtAct3.Focus()
                    txtAct3.Text = e.KeyCode.ToString
                    txtAct3.SelectionStart = txtAct3.TextLength
                End If
            End If
            If ActiveControl.Name = "txtAct3" Then
                If txtAct3.TextLength = txtAct3.MaxLength Then
                    txtAct4.Focus()
                    txtAct4.Text = e.KeyCode.ToString
                    txtAct4.SelectionStart = txtAct4.TextLength
                End If
            End If
        End If
    End Sub

    Private Sub txtAct0_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtAct0.KeyUp, _
        txtAct1.KeyUp, txtAct2.KeyUp, txtAct3.KeyUp, txtAct4.KeyUp

        'All of the key up events for the activation text boxes are handled here.

        'Make sure that only numeric keys, alphanumeric keys, the backspace key and delete key function.
        If (e.KeyValue < 48 Or e.KeyValue > 57) And _
           (e.KeyValue < 65 Or e.KeyValue > 90) And _
           (e.KeyValue < 97 Or e.KeyValue > 122) And _
            e.KeyValue <> 8 And _
            e.KeyValue <> 127 Then e.Handled = True

        'Filters:
        'Key pressed is not the back space key and not the delete key.
        'The number of characters in the text box is equal to the MaxLength property of the text box.
        'Shift the cursor to the next text box.
        If Not e.KeyCode = Keys.Back And Not e.KeyCode = Keys.Delete Then
            If txtAct0.TextLength = txtAct0.MaxLength Then
                txtAct1.Focus()
            End If
            If txtAct1.TextLength = txtAct1.MaxLength Then
                txtAct2.Focus()
            End If
            If txtAct2.TextLength = txtAct2.MaxLength Then
                txtAct3.Focus()
            End If
            If txtAct3.TextLength = txtAct3.MaxLength Then
                txtAct4.Focus()
            End If
        End If

        'Filters:
        'The back space key has been pressed.
        'The number of characters in the text box equals zero.
        'Shift the cursor to the previous text box.
        If e.KeyCode = Keys.Back Then
            If Me.ActiveControl.Name = "txtAct1" Then
                If txtAct1.TextLength = 0 Then
                    txtAct0.Focus()
                    txtAct0.SelectionStart = txtAct0.MaxLength
                End If
            End If
            If Me.ActiveControl.Name = "txtAct2" Then
                If txtAct2.TextLength = 0 Then
                    txtAct1.Focus()
                    txtAct1.SelectionStart = txtAct1.MaxLength
                End If
            End If
            If Me.ActiveControl.Name = "txtAct3" Then
                If txtAct3.TextLength = 0 Then
                    txtAct2.Focus()
                    txtAct2.SelectionStart = txtAct2.MaxLength
                End If
            End If
            If Me.ActiveControl.Name = "txtAct4" Then
                If txtAct4.TextLength = 0 Then
                    txtAct3.Focus()
                    txtAct3.SelectionStart = txtAct3.MaxLength
                End If
            End If
        End If

    End Sub

Open in new window

0
 
LVL 23

Expert Comment

by:Jens Fiederer
Comment Utility
The beeping is probably because you are typing too fast...as long as you type slowly you won't get a beep.  You can type faster than the code switches boxes, so if you are sometimes typing while you are still in a box that is full...and the beep warns you that your keystroke was not accepted in a textbox that is already full.

If you just keep the backspace key pressed, the system will generate keyDOWN events for you, but it's not going to generate key UP events unless you actually release the key.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:Basicfarmer
Comment Utility
What you are saying makes sense, it is not the speed because if the sequence of events in the first paragraph are followed then one simple key down event switches the focus to the text box "txtAct1" then it shifts the keycode of the keypress to the text box "txtAct1", then it moves the cursor in front of the character. So since the displaying or moving of the character is being done programmatically i think i can see how what your saying would be the cause. Is there anything i can do differently to avoid this?
0
 
LVL 23

Expert Comment

by:Jens Fiederer
Comment Utility
The sound that I heard is DEFINITELY related to speed of typing - I entered characters fast (no backspaces, just "1") and heard beeps.  I entered the same character slowly...1...1...1...1...etc and heard no beeps.  THAT beep you might be able to avoid by not setting a max-length as a property and enforcing it using the key handler.

You might be able to trap the key PRESS event as well...perhaps that would substitute for the key  UP when no key UP occurs?
0
 

Author Comment

by:Basicfarmer
Comment Utility
The beep happens for me only on the key down event when the cursor has been backspaced from "txtAct1" to "txtAct0" with 6 characters in the text box "txtact0". What i am seeing has nothing to do with how fast I type. It is a single key down.
Type 6 characters  in the text box "txtAct0". The cursor will jump to the text box "txtAct1". Press the backspace key. The cursor will jump back to "txtAct0". Now at this point one key press will cause the chime. That is what i cannot seem to catch.
0
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 500 total points
Comment Utility
Have you tried taking off the max-length limit on the boxes and enforcing that in code?
0
 

Author Comment

by:Basicfarmer
Comment Utility
Your absolutely correct. The chime is that there are a already a maximum number of characters in the text box. So i have removed the max length and just used number 6 in its place. But now where the max length was keeping the key down event from placing the character in the text box the seventh character is put into the text box just before it jumps to the next text box. How can i cancel the key down event and still capture the key that was pressed to it does not enter this seventh character in the text box?
0
 
LVL 23

Expert Comment

by:Jens Fiederer
Comment Utility
My pleasure, and nice work there!
0
 

Author Comment

by:Basicfarmer
Comment Utility
Got it...
e.SuppressKeyPress
0
 

Author Closing Comment

by:Basicfarmer
Comment Utility
Thanks for your help and sticking with me...
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now