Strange Exception

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
 
C

Chris Tacke, eMVP

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
 
B

Beebs

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?
 
A

Alex Feinman [MVP]

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
 
B

Beebs

Thanks Alex, it was as simple as missing the .Focused = True call, now
it works as expected.
 

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