Strange Exception

  • Thread starter Thread starter Beebs
  • Start date Start date
B

Beebs

Why would this line of code throw a NullReferenceException if the
first item of my listview is clearly highlighted (selected)

If lstContainers.FocusedItem.Index = 0 Then

I know lists in VB are zero based, yet for some reason, an exception
is thrown. Is this some type of weird bug cause it sure doesn't make
sense to me? All I want to do is get the item's index in the listview
that is currently selected.

Thanks
 
Highlighted (selected) and focused are not the same. Multiple items can be
selected (blue background). Only 1 can be focused. If you, for example,
select and item, then tap a button, the selection remains, but the focus
will not.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate
 
I totally understand exactly what you're saying Chris, thanks. My
simple question then is how do I force the first item in my listview
to actually receive the focus through VB code?
 
Setting selection:
myListView.Items(idx).Selected = True

Setting focus:
myListView.Items(idx).Focused = True

Getting selected item index:
if myListView.SelectedIndices.Length > 0 then
selectedIndex = myListView.SelectedIndices(0)
end if
 
Thanks Alex, it was as simple as missing the .Focused = True call, now
it works as expected.
 
Back
Top