specifying selected item in a listbox

G

Guest

Hello again :

How do I specify which item in a listbox I want selected
The listbox is already populated, and if the user types in a value into a textbox, I want that value to be highlighted in the listbox

Thanks
Ambe
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?YW1iZXI=?= said:
How do I specify which item in a listbox I want selected?
The listbox is already populated, and if the user types in a value into a textbox, I want that value to be highlighted in the listbox.

Set the listbox's 'SelectedIndex' or 'SelectedItem' property.
 
G

Guest

Yes, I figured that, but I'm looking for specific syntax to force a listbox.selecteditem to equal a texbox
Thanks
Ambe
 
H

Herfried K. Wagner [MVP]

Amber,

* "=?Utf-8?B?QW1iZXI=?= said:
Yes, I figured that, but I'm looking for specific syntax to force a listbox.selecteditem to equal a texbox.

\\\
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim i As Integer = Me.ListBox1.FindString(Me.TextBox1.Text)
If i = ListBox.NoMatches Then
Me.ListBox1.SelectedIndex = -1
Else
Me.ListBox1.SelectedIndex = i
End If
End Sub
///
 
C

Cor

Hi Herfried, (but also Amber)
\\\
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged
Dim i As Integer = Me.ListBox1.FindString(Me.TextBox1.Text)
If i = ListBox.NoMatches Then
Me.ListBox1.SelectedIndex = -1
Else
Me.ListBox1.SelectedIndex = i
End If
End Sub
///

This is kicking,

Some (maybe what longer) time ago I got every time a messages from
Herfried.

"Better is findstringexact"

But very much "better" is

\\\
me.listbox1.SelectedIndex = Me.ListBox1.FindStringexact(Me.TextBox1.Text)
///

I would not have written this if I had not seen that findstringexact

:)

Cor
 
H

Herfried K. Wagner [MVP]

Cor,

* "Cor said:
This is kicking,

Some (maybe what longer) time ago I got every time a messages from
Herfried.

"Better is findstringexact"

But very much "better" is

\\\
me.listbox1.SelectedIndex = Me.ListBox1.FindStringexact(Me.TextBox1.Text)
///

I would not have written this if I had not seen that findstringexact

I used 'FindString' because it allows to search for parts of the string.

If there are items...

Cor
Corina
Corinna
Corrina
Corrinna

.... typing "Corr" into the textbox will select "Corrina".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top