|
-
Dec 3rd, 2003, 02:34 PM
#1
Thread Starter
Hyperactive Member
sorting text in a text box
I am trying to sort letters that are entered into a text box by a user. I want it to sort as the user is leaving the textbox. I thought about using the split method to create an array. The user will be entering a string of letters like "cbdafqg". I want to make sure it sorts to "abcdfgq" upon exiting. Am I on the right track or is there an easier way to do this?
VB Code:
Private Sub txtChangeCode_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtChangeCode.Leave
txtChangeCode.Text = txtChangeCode.Text.ToUpper
Dim strCodes(txtChangeCode.Text.Length) As String
strCodes = txtChangeCode.Text.Split()
Array.Sort(strCodes)
Dim strSorted As String
Dim i As Integer
For i = 0 To strCodes.Length - 1
strSorted = strSorted & strCodes(i)
Next i
txtChangeCode.Text = strSorted
End Sub
Am I correct that you can't split without a delimiter? like a "." or "/", etc.?
Thanks in advance.
-
Dec 3rd, 2003, 03:13 PM
#2
A string is by nature just an array of characters or char() so you can combine that with the sort method of an array:
VB Code:
Dim str As String = "cdasfg"
Dim chrs() As Char = str.ToCharArray 'convert string to char array
Array.Sort(chrs) 'sort
Dim newstr As New String(chrs) 'convert back to string
MsgBox(newstr)
-
Dec 3rd, 2003, 04:39 PM
#3
Thread Starter
Hyperactive Member
Thanks, that works great.
Now for the bonus question. I want to prevent the user from typing the same character twice in a sequence. I thought the following would work, but it doesn't.
VB Code:
Private Sub txtChangeCode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtChangeCode.KeyPress
If Not (Char.IsLetter(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
If InStr(DirectCast(sender, TextBox).Text, e.KeyChar.ToString.ToUpper, CompareMethod.Text) > 0 Then
e.Handled = True
End If
End If
end sub
-
Dec 3rd, 2003, 04:45 PM
#4
This will work but they can still cut and paste something in but you'll need to handle that seperate if its an issue.
VB Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If TextBox1.Text.Length > 0 Then 'must have text
Dim c() As Char = TextBox1.Text.ToCharArray() 'get char array
'check last element of char against current keychar
If e.KeyChar = c(c.GetUpperBound(0)) Then e.Handled = True
End If
End Sub
-
Dec 4th, 2003, 02:08 PM
#5
Thread Starter
Hyperactive Member
sorting text [RESOLVED]
That works, but it only compares against the last letter typed. I went one step further and compared against the entire string.
VB Code:
If txtChangeCode.Text.Length > 0 Then
If InStr(txtChangeCode.Text.ToUpper, e.KeyChar.ToString.ToUpper, CompareMethod.Text) > 0 Then
e.Handled = True
End If
End If
It works! You're right about the cut and paste problem. I'll have to decide how important that would be to block before I tackle that one.
Thanks again.
-
Dec 4th, 2003, 02:36 PM
#6
My bad I thought thats what you meant by 'twice in a sequence', twice in a row is what I was thinking. Your is shorter too
-
Dec 4th, 2003, 03:14 PM
#7
Originally posted by Edneeis
A string is by nature just an array of characters
I thought that was the CString only
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
|