specifying selected item in a listbox

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
* "=?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.
 
Yes, I figured that, but I'm looking for specific syntax to force a listbox.selecteditem to equal a texbox
Thanks
Ambe
 
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
///
 
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
 
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".
 
Back
Top