Results 1 to 5 of 5

Thread: [resolved] Combobox Autofind

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Albany, NY
    Posts
    489

    Resolved [resolved] Combobox Autofind

    We have severe issues with the combobox control in .Net 2003 at work. Basically when you type in something in the combobox it will look like the combobox finds the matching item, but it doesn't. I'm trying to write some AutoFind code to fix this.

    VB Code:
    1. Private Sub cmb_KeyUp _
    2.    ( _
    3.    ByVal sender As Object, _
    4.    ByVal e As System.Windows.Forms.KeyEventArgs _
    5.    ) Handles cmb.KeyUp
    6.  
    7.    Call Me.SelectComboboxItem(sender)
    8.    e.Handled = True
    9.  
    10. End Sub
    11.  
    12. Private Sub SelectComboboxItem(ByVal p_cmb As ComboBox)
    13.  
    14.    Dim retVal As Integer
    15.    Dim txtLen As Integer
    16.  
    17.    txtLen = p_cmb.Text.Length
    18.    p_cmb.Tag = p_cmb.Text
    19.    retVal = p_cmb.FindString(p_cmb.Text)
    20.  
    21.    If retVal <> -1 Then
    22.       p_cmb.SelectedIndex = retVal
    23.       'code below is ignored, entire text is selected
    24.       p_cmb.SelectionStart = txtLen
    25.       p_cmb.SelectionLength = p_cmb.Text.Length - txtLen
    26.    End If
    27.  
    28. End Sub

    Everything works except the SelectionStart and SelectLength parts. The properties change but when the code actually runs the entire text is selected instead of just selecting the letters that were filled in for the user. Anyone have any ideas on how to make this functionality work?
    Last edited by jcfowl; May 11th, 2005 at 11:19 AM. Reason: resolved

  2. #2
    Junior Member
    Join Date
    Nov 2002
    Posts
    16

    Re: Combobox Autofind

    wasnt there something about selection in vb6, that you have to set the length first and after that you set the start?
    I dont realy remember, but it's worth a try.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Albany, NY
    Posts
    489

    Re: Combobox Autofind

    Thanks for the response spjansen, but that didn't work, it's strange because I see that the values for SelectionStart and SelectionLength are changing to values I need but It just doesn't do that on the screen... so confusing.

    I know that its the basic functionality for the combobox to select the entire text on the change of an item but there has to be a way to override that.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Albany, NY
    Posts
    489

    Re: Combobox Autofind

    got the basic functionality of it to work...

    VB Code:
    1. Private Sub cmb_KeyUp _
    2.    ( _
    3.    ByVal sender As Object, _
    4.    ByVal e As System.Windows.Forms.KeyEventArgs _
    5.    ) Handles cmb.KeyUp
    6.  
    7.    Call Me.SelectComboboxItem(sender)
    8.    e.Handled = True
    9.  
    10. End Sub
    11.  
    12. Private Sub SelectComboboxItem(ByVal p_cmb As ComboBox)
    13.  
    14.    Dim retVal As Integer
    15.    Dim txtLen As Integer
    16.  
    17.    txtLen = p_cmb.Text.Length
    18.    retVal = p_cmb.FindString(p_cmb.Text)
    19.  
    20.    If retVal <> -1 Then
    21.       'p_cmb.SelectedIndex = retVal
    22.      
    23.       'set SelectedItem, Not SelectedIndex (this doesn't highlight all text)
    24.       p_cmb.SelectedItem = p_cmp.Items.Item(retVal)
    25.       p_cmb.SelectionStart = txtLen
    26.       p_cmb.SelectionLength = p_cmb.Text.Length - txtLen
    27.    End If
    28.  
    29. End Sub

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Albany, NY
    Posts
    489

    Re: Combobox Autofind

    finished code... I'd love for some feedback on the functionality if anyone wants to test it out. Any suggestions for change are welcome.

    I'm trying to mimic the old vb6 comobox on the forms 2.0 library where you could set the AutoFind flag to select combobox values.

    VB Code:
    1. Private Sub cmb_KeyUp _
    2.    ( _
    3.    ByVal sender As Object, _
    4.    ByVal e As System.Windows.Forms.KeyEventArgs _
    5.    ) Handles cmb.KeyUp
    6.  
    7.    If e.KeyValue >= Keys.A And e.KeyValue <= Keys.Z _
    8.       Or e.KeyValue >= Keys.NumPad0 And e.KeyValue <= Keys.NumPad9 _
    9.       Or e.KeyValue >= Keys.D0 And e.KeyValue <= Keys.D9 Then
    10.  
    11.       Call SelectComboboxItem(sender)
    12.  
    13.    End If
    14.  
    15. End Sub
    16.  
    17. Private Sub SelectComboboxItem(ByVal p_cmb As ComboBox)
    18.  
    19.    Dim retVal As Integer
    20.    Dim txtLen As Integer
    21.  
    22.    If p_cmb.Text.Length = 0 Then Exit Sub
    23.    txtLen = p_cmb.Text.Length
    24.    retVal = p_cmb.FindString(p_cmb.Text)
    25.  
    26.    If retVal <> -1 Then
    27.       p_cmb.SelectedItem = p_cmp.Items.Item(retVal)
    28.       p_cmb.SelectionStart = txtLen
    29.       p_cmb.SelectionLength = p_cmb.Text.Length - txtLen
    30.    End If
    31.  
    32. End Sub

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