Help with Listbox
- Can you please tell me how to code for handling the keypress event and capturing when someone
is typing a word in the list (using a listbox)
Thanks
Answers
- This is the same thing in VB, courtesy of Telerik. (http://converter.telerik.com/)
Private timer As System.Windows.Forms.Timer = New Timer()
Private currentSelection As New StringBuilder()
Public Sub New()
InitializeComponent()
timer.Interval = 1000
AddHandler timer.Tick, AddressOf timer_Tick
End Sub
Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
currentSelection.Remove(0, currentSelection.Length)
timer.[Stop]()
End Sub
Private Sub listBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
timer.[Stop]()
timer.Start()
currentSelection.Append(e.KeyChar)
Dim i As Integer = 0
While i < listBox1.Items.Count
If listBox1.Items(i).ToString().ToUpper().StartsWith(currentSelection.ToString().ToUpper()) Then
listBox1.SelectedIndex = i
Exit While
End If
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While
e.Handled = True
End Sub
David Morton - http://blog.davemorton.net/- Marked As Answer byReallyNewToDotNet Friday, October 03, 2008 10:14 PM
Set the ListBox's SelectionMode property to SelectionMode.MultiExtended. This will allow multiple selection of items when the user clicks the control key plus the selected items.
I'd encourage you to explain to your user that the standard usage of multi-select listboxes is to use the control key, not the shift key. You can tell them it'll select multiple items just like Windows Explorer. I don't think they'll have a problem with it. Using shift+clicking to select multiple items isn't standard UI behavior, using control+clicking is.
David Morton - http://blog.davemorton.net/- Marked As Answer byReallyNewToDotNet Wednesday, October 15, 2008 6:33 PM
All Replies
- Doing something like the following will allow you to select a particular ListBox item based on the key inputs from the user, supposing the user doesn't stop for 2 seconds before typing another letter. It uses StringBuilder and a Timer to manipulate the values the user types when the ListBox has focus. It checks the value of the StringBuilder against the items in the ListBox to intelligently select an item.
System.Windows.Forms.Timer timer = new Timer(); private StringBuilder currentSelection = new StringBuilder(); public Form1() { InitializeComponent(); timer.Interval = 1000; timer.Tick += new EventHandler(timer_Tick); } void timer_Tick(object sender, EventArgs e) { currentSelection.Remove(0, currentSelection.Length); timer.Stop(); } private void listBox1_KeyPress(object sender, KeyPressEventArgs e) { timer.Stop(); timer.Start(); currentSelection.Append(e.KeyChar); for (int i = 0; i < listBox1.Items.Count; i++) { if (listBox1.Items[i].ToString().ToUpper().StartsWith(currentSelection.ToString().ToUpper())) { listBox1.SelectedIndex = i; break; } } e.Handled = true; }
David Morton - http://blog.davemorton.net/ - I will try this code in VB 2005, need to first modify to VB 2005
Thanks - This is the same thing in VB, courtesy of Telerik. (http://converter.telerik.com/)
Private timer As System.Windows.Forms.Timer = New Timer()
Private currentSelection As New StringBuilder()
Public Sub New()
InitializeComponent()
timer.Interval = 1000
AddHandler timer.Tick, AddressOf timer_Tick
End Sub
Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
currentSelection.Remove(0, currentSelection.Length)
timer.[Stop]()
End Sub
Private Sub listBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
timer.[Stop]()
timer.Start()
currentSelection.Append(e.KeyChar)
Dim i As Integer = 0
While i < listBox1.Items.Count
If listBox1.Items(i).ToString().ToUpper().StartsWith(currentSelection.ToString().ToUpper()) Then
listBox1.SelectedIndex = i
Exit While
End If
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While
e.Handled = True
End Sub
David Morton - http://blog.davemorton.net/- Marked As Answer byReallyNewToDotNet Friday, October 03, 2008 10:14 PM
David;
Using the code you provided works great; but now the user wants to have the ability to type for a particular title and only that title get highlighted (which is what it is doing right now, because the property SelectionMode is set to 'One') and have the ability to use the shift Key and Click to select multiple titles.
When I set the listbox property SelectionMode -> MultiSimple, they can do the shift key and Click for multiple titles without a problem, but when they are typing a title several get highlighted.
Example:
When I click on the listbox and start to type for title 'TRW' this is what is highlighted:
Apples and Oranges (first in list)
The little Train
Trains
TRW
Is it possible to give them both?- How can I make this work?
Private Sub listBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If Not e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.ShiftKey) Then
'e.Handled = True
listBox1.SelectionMode = SelectionMode.One
timer.[Stop]()
timer.Start()
currentSelection.Append(e.KeyChar)
Dim i As Integer = 0
While i < listBox1.Items.Count
If listBox1.Items(i).ToString().ToUpper().StartsWith(currentSelection.ToString().ToUpper()) Then
listBox1.SelectedIndex = i
Exit While
End If
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While
e.Handled = TrueElse
listBox1.SelectionMode = SelectionMode.MultiSimple
End If
End Sub Set the ListBox's SelectionMode property to SelectionMode.MultiExtended. This will allow multiple selection of items when the user clicks the control key plus the selected items.
I'd encourage you to explain to your user that the standard usage of multi-select listboxes is to use the control key, not the shift key. You can tell them it'll select multiple items just like Windows Explorer. I don't think they'll have a problem with it. Using shift+clicking to select multiple items isn't standard UI behavior, using control+clicking is.
David Morton - http://blog.davemorton.net/- Marked As Answer byReallyNewToDotNet Wednesday, October 15, 2008 6:33 PM
- Thanks for the help. I did as you suggested and it works great!!!


