AutoComplete Entry in ComboBox?

  • Thread starter Thread starter Mr. B
  • Start date Start date
M

Mr. B

Here's the situation...

You've a combobox with Items already added. Say they look like this (or even
lines of text):

10-00-232
10-00-256
10-01-006
10-01-213
10-02-200
10-03-045

How does one code the Combobox so that when you manually start to enter the
Item in the Combobox line, it AutoCompletes the input??

Many thanks!

Bruce
 
Hi Bruce,
that shouldn't too much of a problem. First of all it's not very hard to
port C# to VB.net. At least if you have a basic understanding of the C/C#
syntax.
On the other just download the zip from C# help (1st link William provided).
If haven't verified but it states the compiled dll is there, too. This
should be useable without problems from any .NET language.

Matti
 
Matthias Klöpper said:
that shouldn't too much of a problem. First of all it's not very hard to
port C# to VB.net. At least if you have a basic understanding of the C/C#

Okay... don't know anything about C# though (:
If haven't verified but it states the compiled dll is there, too. This
should be useable without problems from any .NET language.

A DLL file? Hmmm... I'll see about that.

Thanks!

Bruce
 
this dous w you need (deleted some things but normally nothing essential)



Private Sub cboProduct_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles cboProduct.KeyPress

Dim Combo As ComboBox = CType(sender, ComboBox)

If Asc(e.KeyChar) = Keys.Escape Then

Combo.SelectedIndex = -1

Combo.Text = ""

Controlkey = True

ElseIf Char.IsControl(e.KeyChar) Then

Controlkey = True

Else

Controlkey = False

End If



End Sub

Private Sub cboProduct_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cboProduct.TextChanged

Dim Combo As ComboBox = CType(sender, ComboBox)

If Combo.Text <> "" And Not Controlkey Then

Dim MatchText As String = Combo.Text

Dim Match As Integer = Combo.FindString(MatchText, -1)

'als gevonden invoegen

If Match <> -1 Then



Combo.SelectedIndex = Match

'de toegevoegde tekst selecteren zodat de gebruiker verder kan typen

Combo.SelectionStart = MatchText.Length

Combo.SelectionLength = Combo.Text.Length - Combo.SelectionStart



End If

End Sub

Private Sub cboProduct_Leave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cboProduct.Leave

Dim Combo As ComboBox = CType(sender, ComboBox)

If Combo.Text <> "" Then

'zoeken naar zelfde

Dim MatchText As String = Combo.Text

Dim Match As Integer = Combo.FindStringExact(MatchText)

If Match <> -1 Then

Combo.SelectedIndex = Match

End If

Else

stbp2.Text = "Not in list"

Combo.Focus()

Combo.SelectAll()

End If

End If

End Sub
 
:\\\\derian said:
i'll do you 1 better... here is a link to a c# -- VB converter.
I grabbed this from someone on this news forum (I forget from whom exactly)

Interesting... I'll check it out.

Thanks!

Bruce
 
Hi Eric
This is a useful sub that I have also been looking for. Thanks!
Just in case some completely newbies look in this code I have corrected two
things:
You need to declare a controlKey boolean variable in the top of your form
code:
Public Class Form1

Inherits System.Windows.Forms.Form

Dim Controlkey As Boolean

' And I believe one of your If-Endif was misplaced

Private Sub cboProduct_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles cboProduct.KeyPress
Dim Combo As ComboBox = CType(sender, ComboBox)

If Asc(e.KeyChar) = Keys.Escape Then

Combo.SelectedIndex = -1

Combo.Text = ""

Controlkey = True

ElseIf Char.IsControl(e.KeyChar) Then

Controlkey = True

Else

Controlkey = False

End If

End Sub

Private Sub cboProduct_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cboProduct.TextChanged

Dim Combo As ComboBox = CType(sender, ComboBox)

If Combo.Text <> "" And Not Controlkey Then

Dim MatchText As String = Combo.Text

Dim Match As Integer = Combo.FindString(MatchText, -1)

If Match <> -1 Then

Combo.SelectedIndex = Match

Combo.SelectionStart = MatchText.Length

Combo.SelectionLength = Combo.Text.Length - Combo.SelectionStart

End If

End If

End Sub

Private Sub cboProduct_Leave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cboProduct.Leave

Dim Combo As ComboBox = CType(sender, ComboBox)

If Combo.Text <> "" Then

Dim MatchText As String = Combo.Text

Dim Match As Integer = Combo.FindStringExact(MatchText)

If Match <> -1 Then

Combo.SelectedIndex = Match

Else

Label1.Text = "Not in list"

Combo.Focus()

Combo.SelectAll()

End If

End If

End Sub


This code works in my projects anyway.

Best regards

Jan
 
* Mr. B said:
Hmmmm... is this a C# project? If so, I've only VB.net (standard)... and it
doesn't like opening it up (fails in fact). Oh well...

Sorry...

;-(
 
correct i forgot the Private Controlkey as boolean and i deleted an end if
to much :)

btw if you are interested in only auto filling when there is only 1 inctance
of the text you are typing i have that 2 (boss dousn't like the possibility
of fast inputters making mistakes)
 
Hi Eric
Actually I have a little problem with your code.
I have a sub on the SelectedIndexChanged for my combo that creates an email.
And your code calls this.

So if I have ie IPC and IPA in the combo item list, when I type I-P it
selects the first (IPA or IPC depending on my sort order) and trigger my
SelectedIndexChanged sub and creates an email for the first item matching
I-P. It does not give me a chance to decide whether I actually wanted
another item starting with IP.

Jan
 
that is becouse you change the selected index when the text changes, have a
look at the following
here it wil not select anithing until there is only 1 possibility left if
there are more possiblilities it wil convert the text to uppercase so the
user knows he's not typing anithing wrong

i didn't test this against the selected index changed but i think you can
work with this

i did wonder wouldn't it be better to generate your e-mail in the leave
event? you can check if the value has changed there.
Private Sub cboProduct_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cboProduct.TextChanged

Dim Combo As ComboBox = CType(sender, ComboBox)

If Combo.Text <> "" And Not Controlkey Then

'zoeken naar zelfde

Dim MatchText As String = Combo.Text

Dim Match As Integer = Combo.FindString(MatchText, -1)

'als gevonden invoegen

If Match <> -1 Then

Dim y As Integer = 1

Dim g As Integer

g = Combo.FindString(MatchText, Match + 1)

If g <> Match Then

y += 1

End If

If y = 1 Then

Combo.SelectedIndex = Match

'de toegevoegde tekst selecteren zodat de gebruiker verder kan typen

Combo.SelectionStart = MatchText.Length

Combo.SelectionLength = Combo.Text.Length - Combo.SelectionStart



Else

Combo.Text = MatchText.ToUpper

Combo.SelectionStart = MatchText.Length

'Combo.SelectionLength = Combo.Text.Length - Combo.SelectionStart

End If

End If

End If

End Sub
 
Hi Eric
Thanks for answering
I also send an email in the Leave event.
But I would _also_ like the user to be able to select a group from the list
and see the email generated without having to press Enter or move focus from
the combo.

I shall look into your example

Thanks

Jan
 
Hi Herfried
Would you be so nice as to copy the C# code and post it on this NG as plain
text?

Jan
 
I knew that :-))
But I obviously forgot.

Thanks

Jan

If any VB.Net newbies looks, here is the "translation".
I think there is an extra e.keychar.ToString() in the C# code. It had to be
removed for the code to work
It is only the Keypress that is from the C# code.

I use my combo to send emails.


Dim StringAktueltValgICombo As String

Private Sub cboSendEmails_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles cboSendEmails.KeyPress

If Asc(e.KeyChar) = Keys.Enter Then cboSendEmails_Leave(Nothing, Nothing) '
Send email

If Char.IsControl(e.KeyChar) Then Exit Sub

StringAktueltValgICombo = Me.cboSendEmails.Text.Substring(0,
Me.cboSendEmails.SelectionStart)

Dim index As Integer = Me.cboSendEmails.FindString(StringAktueltValgICombo)

If index = -1 Then Return

Me.cboSendEmails.SelectedIndex = index

Me.cboSendEmails.Select(StringAktueltValgICombo.Length,
Me.cboSendEmails.Text.Length - StringAktueltValgICombo.Length)

e.Handled = True

End Sub



Private Sub cboSendEmails_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cboSendEmails.SelectedIndexChanged

' Combo has to be sorted ascending

Dim i As Integer = cboSendEmails.SelectedIndex

i = Me.cboSendEmails.FindString(StringAktueltValgICombo, i)

If i <> cboSendEmails.SelectedIndex Then

Exit Sub

End If

SendEmail()

End Sub
 
Back
Top