I am trying to fetch the objects from a ListBox, but ListBox.Items only returns Strings, then I want to check if the item is being focused by a mouse cursor, how can I do this?
-
1The Items collection contains the elements passed to the ItemsSource property of the ListBox. Either add ListBoxItem explicitly, or use the ListBox's ItemContainerGenerator to get the ListBoxItem container for a string item. – Clemens Dec 05 '21 at 16:58
-
Uhh, can you show me an example of this in action? Whatever you just said went right over my head and I don't know how to put it in action, a snippet of code to see how it is written would be welcome :). – Avrigeanu Laurian Dec 05 '21 at 17:00
-
https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-3.5/ms750552(v=vs.90) – Clemens Dec 05 '21 at 17:02
-
https://stackoverflow.com/a/984862/1136211 – Clemens Dec 05 '21 at 17:03
-
I understand now, this line has been enough to solve my entire issue: item = CType((mySender.ItemContainerGenerator.ContainerFromIndex(myVar)), ListBoxItem), if you put your comment as an answer I can mark it as the correct answer. Thank you! – Avrigeanu Laurian Dec 05 '21 at 17:12
-
1Instead of editing a solution into the question, better write an answer. It's ok to answers your own question. – Clemens Dec 05 '21 at 17:48
-
Please do not post code as an image. Post as text and format as code. – Mary Dec 06 '21 at 00:12
-
1@Mary I'm still new to this platform so I'm learning how to best post my code here, I realize now that it would have been better to put it as text. The question has been answered by Clemens earlier, so going forward I am going to put in only text for my code. Thank you. – Avrigeanu Laurian Dec 06 '21 at 13:17
2 Answers
You have a handler, which iterates through ListBox
items:
Private Sub ListBox_PreviewMouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles listBox.PreviewMouseLeftButtonDown
Dim mySender As ListBox = sender
Dim item As ListBoxItem
For Each item In mySender.Items
If item.IsFocused Then
MsgBox(item.ToString)
End If
Next
End Sub
You can add items like they are (Strings
in example):
Private Sub OnWindowLoad(sender As Object, e As RoutedEventArgs)
listBox.Items.Add("Item1")
listBox.Items.Add("Item2")
listBox.Items.Add("Item3")
End Sub
or like ListBoxItems
:
Private Sub OnWindowLoad(sender As Object, e As RoutedEventArgs)
listBox.Items.Add(New ListBoxItem With { .Content = "Item1" })
listBox.Items.Add(New ListBoxItem With { .Content = "Item2" })
listBox.Items.Add(New ListBoxItem With { .Content = "Item3" })
End Sub
In first way (when adding strings as they are) - handler will throw exception about fail to cast String
to ListBoxItem
, because you explicitly declare Dim item As ListBoxItem
. In second way - there wouldn't be cast exception, because you add not Strings, but ListBoxItem
with .Content
of String
.
Obviously you can declare it Dim item As String
to make things work, but String wouldn't have IsFocused
property, which you use to show message box.
Even if you set array or list of strings as ItemsSource
of your ListBox
- it would cause exception too. But array or list of ListBoxItems
, setted as ItemsSource
would work in your handler well.
So answer is that mySender.Items
isn't collection of ListBoxItem
s, to which you trying to cast and iterate. You should redefine your ItemsSource to be a collection of ListBoxItems or change type of iterated items from Dim item As ListBoxItem
to Dim item As String
and lose IsFocused
property.
You can also use just SelectedItem
without iterating and checking for IsFocused
and use safe cast with TryCast
and check whether item is ListBoxItem or just use SelectedItem
itself and at end call ToString on it:
Dim mySender As ListBox = sender
Dim item
If mySender.SelectedItem IsNot Nothing Then
item = TryCast(mySender.SelectedItem, ListBoxItem)
If item Is Nothing Then
item = mySender.SelectedItem
End If
MsgBox(item.ToString)
End If

- 1,607
- 1
- 7
- 13
-
Or use the ListBox's ItemContainerGenerator to get the ListBoxItem container of an integer item. – Clemens Dec 05 '21 at 17:45
Clemens helped me reach the solution below:
Solution:
item = CType((mySender.ItemContainerGenerator.ContainerFromIndex(myVar)), ListBoxItem)
Where mySender is your ListBox and myVar is your Integer.

- 85
- 8