How to detect which ListView item user clicks?

R

Robert

I can add items to the ListView but could find a way to know which row (in
detail view) that the user taps.

The .NETCF does not pass current clicked index in the event procedures any
more. So how do you know which row user taps?

Thanks in advance!
 
D

Darren Shaffer

you iterate the items and inspect their Checked state if using checkboxes
or inspect the SelectedIndices array if not.
--
Darren Shaffer
..NET Compact Framework MVP
Principal Architect
Connected Innovation
www.connectedinnovation.com
 
R

Robert

Could you be more specific?

I tried follwing code and it got an exception saying something like
System.argumentsOutRange

Private Sub lsvList_SelectedIndexChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lsvList.SelectedIndexChanged
Dim index As Integer
index = lsvList.SelectedIndices(0)
txtFMPNumber.Text = lsvList.Items(index).Text
updQt.Value = lsvList.Items(index).SubItems(1).Text
End Sub

Any ideas?
 
S

Sergey Bogdanov

I suppose you should validate that SelectedIndices collection has at
least one item:

Dim index As Integer
if SelectedIndices.Count = 0 then return
index = lsvList.SelectedIndices(0)
....

Also using Debug mode (press F5 in VS) try to identify where
ArgumentsOutRange was thrown.

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
D

Darren Shaffer

I bet the arg out of range exception is right here:
updQt.Value = lsvList.Items(index).SubItems(1).Text

There is probably no SubItems(1) and you should be using
SubItems(0).Text

--
Darren Shaffer
..NET Compact Framework MVP
Principal Architect
Connected Innovation
www.connectedinnovation.com
 
D

Daniel Moth

I bet the arg out of range exception is right here:
How much? :-D

I bet it is here:
index = lsvList.SelectedIndices(0)

Cheers
Daniel
 
D

Darren Shaffer

Daniel - the bet is two pints at MEDC ;-)

I've gotta believe he at least clicked an item in the ListView before
he got the exception while testing, which means there is at least
one selected index and he's 'off-by-one' on his subitems index.

Robert - tt is always a good idea to do this:

if ( lvwTasks.SelectedIndices.Count > 0 )
{
ListViewItem lvi = lvwTasks.Items[lvwTasks.SelectedIndices[0]];
//
}

before inspecting the SelectedIndices collection.

Ok Robert - there's a lot riding on your code man - what was your issue?
--
Darren Shaffer
..NET Compact Framework MVP
Principal Architect
Connected Innovation
www.connectedinnovation.com
 
D

Daniel Moth

Darren you are on!

He clicked on a listviewitem and as a result the SelectedIndexChanged event
fires twice. Once with SelectedIndices.Count=0 and once with an actual item
selected. Since he is not checking for that, he got the argumentexception on
the line I suggested.

Robert, don't change a line in your code, stick a breakpoint on the
following line and then step into once and watch the exception come up. Then
point Darren to the bar :)
index = lsvList.SelectedIndices(0)

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Darren Shaffer said:
Daniel - the bet is two pints at MEDC ;-)

I've gotta believe he at least clicked an item in the ListView before
he got the exception while testing, which means there is at least
one selected index and he's 'off-by-one' on his subitems index.

Robert - tt is always a good idea to do this:

if ( lvwTasks.SelectedIndices.Count > 0 )
{
ListViewItem lvi = lvwTasks.Items[lvwTasks.SelectedIndices[0]];
//
}

before inspecting the SelectedIndices collection.

Ok Robert - there's a lot riding on your code man - what was your issue?
--
Darren Shaffer
.NET Compact Framework MVP
Principal Architect
Connected Innovation
www.connectedinnovation.com
 
D

Darren Shaffer

crap - I forgot about that little detail, which I have run into
at least a half-dozen times on CF projects. will square up
with you in May (and look fwd to meeting you as well btw - I've
learned a great deal from your BLOG over the last couple
of years)

now, what are we going to do if he checks for selectedindices.count > 0
and he ALSO gets an arg exception on the subitem index? ;-)
--
Darren Shaffer
..NET Compact Framework MVP
Principal Architect
Connected Innovation
www.connectedinnovation.com

Daniel Moth said:
Darren you are on!

He clicked on a listviewitem and as a result the SelectedIndexChanged
event fires twice. Once with SelectedIndices.Count=0 and once with an
actual item selected. Since he is not checking for that, he got the
argumentexception on the line I suggested.

Robert, don't change a line in your code, stick a breakpoint on the
following line and then step into once and watch the exception come up.
Then point Darren to the bar :)
index = lsvList.SelectedIndices(0)

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Darren Shaffer said:
Daniel - the bet is two pints at MEDC ;-)

I've gotta believe he at least clicked an item in the ListView before
he got the exception while testing, which means there is at least
one selected index and he's 'off-by-one' on his subitems index.

Robert - tt is always a good idea to do this:

if ( lvwTasks.SelectedIndices.Count > 0 )
{
ListViewItem lvi = lvwTasks.Items[lvwTasks.SelectedIndices[0]];
//
}

before inspecting the SelectedIndices collection.

Ok Robert - there's a lot riding on your code man - what was your issue?
--
Darren Shaffer
.NET Compact Framework MVP
Principal Architect
Connected Innovation
www.connectedinnovation.com


Daniel Moth said:
I bet the arg out of range exception is right here:
How much? :-D

I bet it is here:
index = lsvList.SelectedIndices(0)

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


message I bet the arg out of range exception is right here:
updQt.Value = lsvList.Items(index).SubItems(1).Text

There is probably no SubItems(1) and you should be using
SubItems(0).Text

--
Darren Shaffer
.NET Compact Framework MVP
Principal Architect
Connected Innovation
www.connectedinnovation.com


Could you be more specific?

I tried follwing code and it got an exception saying something like
System.argumentsOutRange

Private Sub lsvList_SelectedIndexChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles lsvList.SelectedIndexChanged
Dim index As Integer
index = lsvList.SelectedIndices(0)
txtFMPNumber.Text = lsvList.Items(index).Text
updQt.Value = lsvList.Items(index).SubItems(1).Text
End Sub

Any ideas?



message you iterate the items and inspect their Checked state if using
checkboxes
or inspect the SelectedIndices array if not.
--
Darren Shaffer
.NET Compact Framework MVP
Principal Architect
Connected Innovation
www.connectedinnovation.com

I can add items to the ListView but could find a way to know which
row (in detail view) that the user taps.

The .NETCF does not pass current clicked index in the event
procedures any more. So how do you know which row user taps?

Thanks in advance!
 
D

Daniel Moth

at least a half-dozen times on CF projects. will square up
with you in May (and look fwd to meeting you as well btw - I've
Looking forward to it!
now, what are we going to do if he checks for selectedindices.count > 0
and he ALSO gets an arg exception on the subitem index? ;-)
I dunno; let's talk about that over a few pints once you get them in ;-)

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Darren Shaffer said:
crap - I forgot about that little detail, which I have run into
at least a half-dozen times on CF projects. will square up
with you in May (and look fwd to meeting you as well btw - I've
learned a great deal from your BLOG over the last couple
of years)

now, what are we going to do if he checks for selectedindices.count > 0
and he ALSO gets an arg exception on the subitem index? ;-)
--
Darren Shaffer
.NET Compact Framework MVP
Principal Architect
Connected Innovation
www.connectedinnovation.com

Daniel Moth said:
Darren you are on!

He clicked on a listviewitem and as a result the SelectedIndexChanged
event fires twice. Once with SelectedIndices.Count=0 and once with an
actual item selected. Since he is not checking for that, he got the
argumentexception on the line I suggested.

Robert, don't change a line in your code, stick a breakpoint on the
following line and then step into once and watch the exception come up.
Then point Darren to the bar :)
index = lsvList.SelectedIndices(0)

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Darren Shaffer said:
Daniel - the bet is two pints at MEDC ;-)

I've gotta believe he at least clicked an item in the ListView before
he got the exception while testing, which means there is at least
one selected index and he's 'off-by-one' on his subitems index.

Robert - tt is always a good idea to do this:

if ( lvwTasks.SelectedIndices.Count > 0 )
{
ListViewItem lvi = lvwTasks.Items[lvwTasks.SelectedIndices[0]];
//
}

before inspecting the SelectedIndices collection.

Ok Robert - there's a lot riding on your code man - what was your issue?
--
Darren Shaffer
.NET Compact Framework MVP
Principal Architect
Connected Innovation
www.connectedinnovation.com


I bet the arg out of range exception is right here:
How much? :-D

I bet it is here:
index = lsvList.SelectedIndices(0)

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


message I bet the arg out of range exception is right here:
updQt.Value = lsvList.Items(index).SubItems(1).Text

There is probably no SubItems(1) and you should be using
SubItems(0).Text

--
Darren Shaffer
.NET Compact Framework MVP
Principal Architect
Connected Innovation
www.connectedinnovation.com


Could you be more specific?

I tried follwing code and it got an exception saying something like
System.argumentsOutRange

Private Sub lsvList_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles lsvList.SelectedIndexChanged
Dim index As Integer
index = lsvList.SelectedIndices(0)
txtFMPNumber.Text = lsvList.Items(index).Text
updQt.Value = lsvList.Items(index).SubItems(1).Text
End Sub

Any ideas?



message you iterate the items and inspect their Checked state if using
checkboxes
or inspect the SelectedIndices array if not.
--
Darren Shaffer
.NET Compact Framework MVP
Principal Architect
Connected Innovation
www.connectedinnovation.com

I can add items to the ListView but could find a way to know which
row (in detail view) that the user taps.

The .NETCF does not pass current clicked index in the event
procedures any more. So how do you know which row user taps?

Thanks in advance!
 
R

Robert

It is probably a little late. But I still want to thank you all for your
input. I finally figured it out where the OutRange was caused. The problem
was that the listView control's SelectedIndexChanged is fired twice when you
tap an item in it. The first time it is fired the SelectedIndices collection
is empty (the tapped item has not become selectedItem yet). So, if you write
code

txtBox.Text = lsvList.Items(lsvList.SelectedIndices(0)).Text

in the event, the return value of lsvList.SelectedIndices(0) could be
nothing, if not causing the error, because the index collection is empty. I
believe that was where caused the error. Also, the ListView.Items collection
can not take nothing as its index, which may also cause the error.

Flowing code solves the problem. Sergey Bogdanov's input was right at the
point. Thanks Sergey! I wish I had read your input earlier.

Private Sub lsvList_SelectedIndexChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lsvList.SelectedIndexChanged
If lsvList.SelectedIndices.Count > 0 Then

txtBox1.Text = lsvList.Items(lsvList.SelectedIndices(0)).Text

txtBox2.Text =
lsvList.Items(lsvList.SelectedIndices(0)).SubItems(1).Text

End If

End Sub

Now, I can pick any item in the listView and edit it in the textBoxes.

Again, thank you all

Robert Liu
 

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