|
-
May 11th, 2005, 07:43 AM
#1
Thread Starter
Hyperactive Member
[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:
Private Sub cmb_KeyUp _
( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs _
) Handles cmb.KeyUp
Call Me.SelectComboboxItem(sender)
e.Handled = True
End Sub
Private Sub SelectComboboxItem(ByVal p_cmb As ComboBox)
Dim retVal As Integer
Dim txtLen As Integer
txtLen = p_cmb.Text.Length
p_cmb.Tag = p_cmb.Text
retVal = p_cmb.FindString(p_cmb.Text)
If retVal <> -1 Then
p_cmb.SelectedIndex = retVal
'code below is ignored, entire text is selected
p_cmb.SelectionStart = txtLen
p_cmb.SelectionLength = p_cmb.Text.Length - txtLen
End If
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
-
May 11th, 2005, 07:47 AM
#2
Junior Member
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.
-
May 11th, 2005, 08:07 AM
#3
Thread Starter
Hyperactive Member
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.
-
May 11th, 2005, 09:19 AM
#4
Thread Starter
Hyperactive Member
Re: Combobox Autofind
got the basic functionality of it to work...
VB Code:
Private Sub cmb_KeyUp _
( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs _
) Handles cmb.KeyUp
Call Me.SelectComboboxItem(sender)
e.Handled = True
End Sub
Private Sub SelectComboboxItem(ByVal p_cmb As ComboBox)
Dim retVal As Integer
Dim txtLen As Integer
txtLen = p_cmb.Text.Length
retVal = p_cmb.FindString(p_cmb.Text)
If retVal <> -1 Then
'p_cmb.SelectedIndex = retVal
'set SelectedItem, Not SelectedIndex (this doesn't highlight all text)
p_cmb.SelectedItem = p_cmp.Items.Item(retVal)
p_cmb.SelectionStart = txtLen
p_cmb.SelectionLength = p_cmb.Text.Length - txtLen
End If
End Sub
-
May 11th, 2005, 10:42 AM
#5
Thread Starter
Hyperactive Member
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:
Private Sub cmb_KeyUp _
( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs _
) Handles cmb.KeyUp
If e.KeyValue >= Keys.A And e.KeyValue <= Keys.Z _
Or e.KeyValue >= Keys.NumPad0 And e.KeyValue <= Keys.NumPad9 _
Or e.KeyValue >= Keys.D0 And e.KeyValue <= Keys.D9 Then
Call SelectComboboxItem(sender)
End If
End Sub
Private Sub SelectComboboxItem(ByVal p_cmb As ComboBox)
Dim retVal As Integer
Dim txtLen As Integer
If p_cmb.Text.Length = 0 Then Exit Sub
txtLen = p_cmb.Text.Length
retVal = p_cmb.FindString(p_cmb.Text)
If retVal <> -1 Then
p_cmb.SelectedItem = p_cmp.Items.Item(retVal)
p_cmb.SelectionStart = txtLen
p_cmb.SelectionLength = p_cmb.Text.Length - txtLen
End If
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|