Results 1 to 8 of 8

Thread: [RESOLVED] [2005] Insert a character ever 4 characters.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    Resolved [RESOLVED] [2005] Insert a character ever 4 characters.

    I'm working on my applications serial/license form now and I want to make it easy for the customer to type the license in. The license is seperated by "-" every four characters.

    I want to thow some if statements in so they can enter it in with spaces or all one long string. The spaces was easy, but I'm not sure how to insert "-" every 4 characters, except for the last 4 characters.

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX

    Thanks guys.
    Last edited by tylerm; Apr 14th, 2007 at 04:08 PM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2005] Insert a character ever 4 characters.

    Unless you use a series of textboxes such that the user is guided to enter it as several different chunks, I would suggest that instead of inserting hyphens, you let the user enter whatever they want to enter, and look for hyphens in the result. If they are there, then are there enough of them. If they aren't there, then you can insert them if you need to, but it seems that you might be more interested in the numbers, rather than the hyphens.
    My usual boring signature: Nothing

  3. #3
    Addicted Member
    Join Date
    Jan 2002
    Location
    West Virginia
    Posts
    193

    Re: [2005] Insert a character ever 4 characters.

    Why not use a masked textbox. Just set the mask to:
    "####-####-####-####-####-####-####-####"

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    Re: [2005] Insert a character ever 4 characters.

    If I'm thinking right, you can only have numerical characters in the mask textbox, which my serial system uses both numerical and alphabetical characters.

  5. #5
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] Insert a character ever 4 characters.

    What I did for this exact circumstance was to use four textboxes separated by labels (with text of "-") in between.

    I see many programs that do it this way. If you limit each textbox to accept only four characters, it makes it easier for the user too.

    p.s. Make sure you set the tabstops correctly.

  6. #6
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: [2005] Insert a character ever 4 characters.

    Use a masked Textbox with the mask of

    "&&&&-&&&&-&&&&-&&&&-&&&&-&&&&-&&&&-&&&&"

    BTW, According to MSDN this was n ot available until .Net 2.0

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Insert a character ever 4 characters.

    This code will behave pretty much like the new serial input boxes on Vista and Office 2007:
    vb Code:
    1. Private deleting As Boolean = False
    2.  
    3. Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    4.     Me.deleting = (e.KeyCode = Keys.Delete)
    5. End Sub
    6.  
    7. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    8.     'Reject the input if the character is not a letter, digit or control character.
    9.     e.Handled = (Not Char.IsLetterOrDigit(e.KeyChar) AndAlso _
    10.                  Not Char.IsControl(e.KeyChar))
    11. End Sub
    12.  
    13. Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    14.     'Remember the current caret position.
    15.     Dim selectionStart As Integer = Me.TextBox1.SelectionStart
    16.     Dim currentText As String = Me.TextBox1.Text
    17.  
    18.     'Strip out the existing dashes.
    19.     currentText = currentText.Replace("-", String.Empty)
    20.  
    21.     Dim newTextBuilder As New System.Text.StringBuilder
    22.  
    23.     'Insert dashes at the appropriate positions.
    24.     For i As Integer = 0 To currentText.Length - 1 Step 1
    25.         If i > 0 AndAlso i Mod 4 = 0 Then
    26.             newTextBuilder.Append("-"c)
    27.         End If
    28.  
    29.         newTextBuilder.Append(currentText(i))
    30.     Next i
    31.  
    32.     Dim newText As String = newTextBuilder.ToString()
    33.  
    34.     'Display the new text.
    35.     Me.TextBox1.Text = newText
    36.  
    37.     If Not Me.deleting AndAlso _
    38.        selectionStart > 0 AndAlso _
    39.        selectionStart < newText.Length AndAlso _
    40.        newText(selectionStart - 1) = "-"c Then
    41.         'A dash has just been inserted at the caret position so move ahead one character.
    42.         selectionStart += 1
    43.     End If
    44.  
    45.     'Restore the correct caret position.
    46.     Me.TextBox1.SelectionStart = selectionStart
    47. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    Re: [2005] Insert a character ever 4 characters.

    Quote Originally Posted by jmcilhinney
    This code will behave pretty much like the new serial input boxes on Vista and Office 2007:
    vb Code:
    1. Private deleting As Boolean = False
    2.  
    3. Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    4.     Me.deleting = (e.KeyCode = Keys.Delete)
    5. End Sub
    6.  
    7. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    8.     'Reject the input if the character is not a letter, digit or control character.
    9.     e.Handled = (Not Char.IsLetterOrDigit(e.KeyChar) AndAlso _
    10.                  Not Char.IsControl(e.KeyChar))
    11. End Sub
    12.  
    13. Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    14.     'Remember the current caret position.
    15.     Dim selectionStart As Integer = Me.TextBox1.SelectionStart
    16.     Dim currentText As String = Me.TextBox1.Text
    17.  
    18.     'Strip out the existing dashes.
    19.     currentText = currentText.Replace("-", String.Empty)
    20.  
    21.     Dim newTextBuilder As New System.Text.StringBuilder
    22.  
    23.     'Insert dashes at the appropriate positions.
    24.     For i As Integer = 0 To currentText.Length - 1 Step 1
    25.         If i > 0 AndAlso i Mod 4 = 0 Then
    26.             newTextBuilder.Append("-"c)
    27.         End If
    28.  
    29.         newTextBuilder.Append(currentText(i))
    30.     Next i
    31.  
    32.     Dim newText As String = newTextBuilder.ToString()
    33.  
    34.     'Display the new text.
    35.     Me.TextBox1.Text = newText
    36.  
    37.     If Not Me.deleting AndAlso _
    38.        selectionStart > 0 AndAlso _
    39.        selectionStart < newText.Length AndAlso _
    40.        newText(selectionStart - 1) = "-"c Then
    41.         'A dash has just been inserted at the caret position so move ahead one character.
    42.         selectionStart += 1
    43.     End If
    44.  
    45.     'Restore the correct caret position.
    46.     Me.TextBox1.SelectionStart = selectionStart
    47. End Sub
    100% Exactly what I was looking for, thanks again as always!

Posting Permissions

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



Click Here to Expand Forum to Full Width