[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.
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.
Re: [2005] Insert a character ever 4 characters.
Why not use a masked textbox. Just set the mask to:
"####-####-####-####-####-####-####-####"
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.
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.
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
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:
Private deleting As Boolean = False
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
Me.deleting = (e.KeyCode = Keys.Delete)
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
'Reject the input if the character is not a letter, digit or control character.
e.Handled = (Not Char.IsLetterOrDigit(e.KeyChar) AndAlso _
Not Char.IsControl(e.KeyChar))
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
'Remember the current caret position.
Dim selectionStart As Integer = Me.TextBox1.SelectionStart
Dim currentText As String = Me.TextBox1.Text
'Strip out the existing dashes.
currentText = currentText.Replace("-", String.Empty)
Dim newTextBuilder As New System.Text.StringBuilder
'Insert dashes at the appropriate positions.
For i As Integer = 0 To currentText.Length - 1 Step 1
If i > 0 AndAlso i Mod 4 = 0 Then
newTextBuilder.Append("-"c)
End If
newTextBuilder.Append(currentText(i))
Next i
Dim newText As String = newTextBuilder.ToString()
'Display the new text.
Me.TextBox1.Text = newText
If Not Me.deleting AndAlso _
selectionStart > 0 AndAlso _
selectionStart < newText.Length AndAlso _
newText(selectionStart - 1) = "-"c Then
'A dash has just been inserted at the caret position so move ahead one character.
selectionStart += 1
End If
'Restore the correct caret position.
Me.TextBox1.SelectionStart = selectionStart
End Sub
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:
Private deleting As Boolean = False
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
Me.deleting = (e.KeyCode = Keys.Delete)
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
'Reject the input if the character is not a letter, digit or control character.
e.Handled = (Not Char.IsLetterOrDigit(e.KeyChar) AndAlso _
Not Char.IsControl(e.KeyChar))
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
'Remember the current caret position.
Dim selectionStart As Integer = Me.TextBox1.SelectionStart
Dim currentText As String = Me.TextBox1.Text
'Strip out the existing dashes.
currentText = currentText.Replace("-", String.Empty)
Dim newTextBuilder As New System.Text.StringBuilder
'Insert dashes at the appropriate positions.
For i As Integer = 0 To currentText.Length - 1 Step 1
If i > 0 AndAlso i Mod 4 = 0 Then
newTextBuilder.Append("-"c)
End If
newTextBuilder.Append(currentText(i))
Next i
Dim newText As String = newTextBuilder.ToString()
'Display the new text.
Me.TextBox1.Text = newText
If Not Me.deleting AndAlso _
selectionStart > 0 AndAlso _
selectionStart < newText.Length AndAlso _
newText(selectionStart - 1) = "-"c Then
'A dash has just been inserted at the caret position so move ahead one character.
selectionStart += 1
End If
'Restore the correct caret position.
Me.TextBox1.SelectionStart = selectionStart
End Sub
100% Exactly what I was looking for, thanks again as always!