StealthRT
StealthRT

Reputation: 10552

Determine if an item is selected in a listview box

Using VB.net 2010 i am trying to figure out if an item was selected or not. Reason being is that if the user clicks on an item and pushes a button then everything works just fine. However, if the user selects an item and then clicks on a blank spot on the bottom of the listview and then clicks the button then it crashes.

My code is this:

    If (lstMaster.SelectedItems(0).SubItems(1).Text) Is Nothing Then
        MsgBox("test")
    End If

Any help would be great! :o)

David

Upvotes: 5

Views: 36368

Answers (4)

Aleksey
Aleksey

Reputation: 1

Use this checking with "If/EndIf" construction: ListView1.Items(0).Selected = True

Upvotes: 0

p4th0log1st
p4th0log1st

Reputation: 1

Not sure if I've understood you correctly - Try using the ListView MouseMove event and check that lstMaster.SelectedItems.Count > 0 if you want to change the Enable property of a Button based on whether a row has been selected or not within your ListView control.

Upvotes: 0

Grammarian
Grammarian

Reputation: 6882

Ensure that something is selected first by checking that SelectedItems is not empty.

lstMaster.SelectedItems.Count > 0

Upvotes: 16

manji
manji

Reputation: 47978

check lstMaster.SelectedItems(0).Selected

Upvotes: 1

Related Questions