Results 1 to 7 of 7

Thread: sorting text in a text box

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    Connecticut
    Posts
    257

    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:
    1. Private Sub txtChangeCode_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtChangeCode.Leave
    2.         txtChangeCode.Text = txtChangeCode.Text.ToUpper
    3.         Dim strCodes(txtChangeCode.Text.Length) As String
    4.         strCodes = txtChangeCode.Text.Split()
    5.         Array.Sort(strCodes)
    6.         Dim strSorted As String
    7.         Dim i As Integer
    8.         For i = 0 To strCodes.Length - 1
    9.             strSorted = strSorted & strCodes(i)
    10.         Next i
    11.         txtChangeCode.Text = strSorted
    12.     End Sub

    Am I correct that you can't split without a delimiter? like a "." or "/", etc.?

    Thanks in advance.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Dim str As String = "cdasfg"
    2.         Dim chrs() As Char = str.ToCharArray 'convert string to char array
    3.         Array.Sort(chrs) 'sort
    4.         Dim newstr As New String(chrs) 'convert back to string
    5.         MsgBox(newstr)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    Connecticut
    Posts
    257
    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:
    1. Private Sub txtChangeCode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtChangeCode.KeyPress
    2.         If Not (Char.IsLetter(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
    3.             If InStr(DirectCast(sender, TextBox).Text, e.KeyChar.ToString.ToUpper, CompareMethod.Text) > 0 Then
    4.                 e.Handled = True
    5.             End If
    6.         End If
    7. end sub

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.         If TextBox1.Text.Length > 0 Then 'must have text
    3.             Dim c() As Char = TextBox1.Text.ToCharArray() 'get char array
    4.             'check last element of char against current keychar
    5.             If e.KeyChar = c(c.GetUpperBound(0)) Then e.Handled = True
    6.         End If
    7.     End Sub

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    Connecticut
    Posts
    257

    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:
    1. If txtChangeCode.Text.Length > 0 Then
    2.             If InStr(txtChangeCode.Text.ToUpper, e.KeyChar.ToString.ToUpper, CompareMethod.Text) > 0 Then
    3.                 e.Handled = True
    4.             End If
    5.                     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.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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

  7. #7
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    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
  •  



Click Here to Expand Forum to Full Width