Home All Groups Group Topic Archive Search About

Getting the selected item from a listview

Author
3 Apr 2006 12:50 PM
George
Hi all,

How can I get the value stored from the selected item and subitems of a
listview?

Thanks in advance,

George

Author
3 Apr 2006 12:58 PM
Kerry Moorman
George,

Here is one way to get the first selected item:

        Dim item As ListViewItem
        If lvwInvoice.SelectedItems.Count > 0 Then
            item = lvwInvoice.SelectedItems(0)
            MsgBox(item.Text)
            MsgBox(item.SubItems(1).Text)
        End If



Show quoteHide quote
"George" wrote:

> Hi all,
>
> How can I get the value stored from the selected item and subitems of a
> listview?
>
> Thanks in advance,
>
> George
Author
3 Apr 2006 1:12 PM
George
Thanks for that - it is straight to the point!

I used the example, and it worked - sort of.  The first time I clicked on an
item, it returned the values fine.  But when I clicked on another item I got
the following error:

InvalidArgument=Value of '0' is not valid for 'index'.


This is the code:

        Dim item = lvCollection.SelectedItems(0)
        cmbCond.Text = item.Text
        txtQty.Text = item.SubItems(1).Text

It works the first time, but not the second?

Thanks in advance,

George

Show quoteHide quote
"Kerry Moorman" wrote:

> George,
>
> Here is one way to get the first selected item:
>
>         Dim item As ListViewItem
>         If lvwInvoice.SelectedItems.Count > 0 Then
>             item = lvwInvoice.SelectedItems(0)
>             MsgBox(item.Text)
>             MsgBox(item.SubItems(1).Text)
>         End If
>
>
>
> "George" wrote:
>
> > Hi all,
> >
> > How can I get the value stored from the selected item and subitems of a
> > listview?
> >
> > Thanks in advance,
> >
> > George
Author
3 Apr 2006 2:06 PM
Kerry Moorman
George,

Are you using an IF statement to make sure that an item is selected, like I
did in my original example?

If you are writing your code in the listview's SelectedIndexChanged event,
when you select a second item, the first item is de-selected and the
SelectedIndexChanged event fires. At that point there is no SelectedItems(0).

Kerry Moorman


Show quoteHide quote
"George" wrote:

>
> Thanks for that - it is straight to the point!
>
> I used the example, and it worked - sort of.  The first time I clicked on an
> item, it returned the values fine.  But when I clicked on another item I got
> the following error:
>
> InvalidArgument=Value of '0' is not valid for 'index'.
>
>
> This is the code:
>
>         Dim item = lvCollection.SelectedItems(0)
>         cmbCond.Text = item.Text
>         txtQty.Text = item.SubItems(1).Text
>
> It works the first time, but not the second?
>
> Thanks in advance,
>
> George
>
> "Kerry Moorman" wrote:
>
> > George,
> >
> > Here is one way to get the first selected item:
> >
> >         Dim item As ListViewItem
> >         If lvwInvoice.SelectedItems.Count > 0 Then
> >             item = lvwInvoice.SelectedItems(0)
> >             MsgBox(item.Text)
> >             MsgBox(item.SubItems(1).Text)
> >         End If
> >
> >
> >
> > "George" wrote:
> >
> > > Hi all,
> > >
> > > How can I get the value stored from the selected item and subitems of a
> > > listview?
> > >
> > > Thanks in advance,
> > >
> > > George
Author
3 Apr 2006 2:17 PM
George
I guess that is what I am having a hard time understanding....how can I get
the selected item?

Currently, I am using the SelectedIndexChanged event.  Is the
SelectedIndexChanged event the correct way to go and if so how do I get the
item and any other item clicked?



Show quoteHide quote
"Kerry Moorman" wrote:

> George,
>
> Are you using an IF statement to make sure that an item is selected, like I
> did in my original example?
>
> If you are writing your code in the listview's SelectedIndexChanged event,
> when you select a second item, the first item is de-selected and the
> SelectedIndexChanged event fires. At that point there is no SelectedItems(0).
>
> Kerry Moorman
>
>
> "George" wrote:
>
> >
> > Thanks for that - it is straight to the point!
> >
> > I used the example, and it worked - sort of.  The first time I clicked on an
> > item, it returned the values fine.  But when I clicked on another item I got
> > the following error:
> >
> > InvalidArgument=Value of '0' is not valid for 'index'.
> >
> >
> > This is the code:
> >
> >         Dim item = lvCollection.SelectedItems(0)
> >         cmbCond.Text = item.Text
> >         txtQty.Text = item.SubItems(1).Text
> >
> > It works the first time, but not the second?
> >
> > Thanks in advance,
> >
> > George
> >
> > "Kerry Moorman" wrote:
> >
> > > George,
> > >
> > > Here is one way to get the first selected item:
> > >
> > >         Dim item As ListViewItem
> > >         If lvwInvoice.SelectedItems.Count > 0 Then
> > >             item = lvwInvoice.SelectedItems(0)
> > >             MsgBox(item.Text)
> > >             MsgBox(item.SubItems(1).Text)
> > >         End If
> > >
> > >
> > >
> > > "George" wrote:
> > >
> > > > Hi all,
> > > >
> > > > How can I get the value stored from the selected item and subitems of a
> > > > listview?
> > > >
> > > > Thanks in advance,
> > > >
> > > > George
Author
3 Apr 2006 2:31 PM
George
I was able to get to the following:

        Dim itemall As ListView.SelectedListViewItemCollection =
lvCollection.SelectedItems
        Dim item As ListViewItem
        For Each item In itemall
            cmbCond.Text = item.Text
            txtQty.Text = item.SubItems(1).Text
            cmbLocation.Text = item.SubItems(2).Text
            txtColComment.Text = item.SubItems(3).Text
        Next


But it seems a bit overdone, since my listview is not multi select.  Is
there a more straightforward way to get what was selected?

George
Show quoteHide quote
"Kerry Moorman" wrote:

> George,
>
> Are you using an IF statement to make sure that an item is selected, like I
> did in my original example?
>
> If you are writing your code in the listview's SelectedIndexChanged event,
> when you select a second item, the first item is de-selected and the
> SelectedIndexChanged event fires. At that point there is no SelectedItems(0).
>
> Kerry Moorman
>
>
> "George" wrote:
>
> >
> > Thanks for that - it is straight to the point!
> >
> > I used the example, and it worked - sort of.  The first time I clicked on an
> > item, it returned the values fine.  But when I clicked on another item I got
> > the following error:
> >
> > InvalidArgument=Value of '0' is not valid for 'index'.
> >
> >
> > This is the code:
> >
> >         Dim item = lvCollection.SelectedItems(0)
> >         cmbCond.Text = item.Text
> >         txtQty.Text = item.SubItems(1).Text
> >
> > It works the first time, but not the second?
> >
> > Thanks in advance,
> >
> > George
> >
> > "Kerry Moorman" wrote:
> >
> > > George,
> > >
> > > Here is one way to get the first selected item:
> > >
> > >         Dim item As ListViewItem
> > >         If lvwInvoice.SelectedItems.Count > 0 Then
> > >             item = lvwInvoice.SelectedItems(0)
> > >             MsgBox(item.Text)
> > >             MsgBox(item.SubItems(1).Text)
> > >         End If
> > >
> > >
> > >
> > > "George" wrote:
> > >
> > > > Hi all,
> > > >
> > > > How can I get the value stored from the selected item and subitems of a
> > > > listview?
> > > >
> > > > Thanks in advance,
> > > >
> > > > George
Author
3 Apr 2006 4:24 PM
Kerry Moorman
George,

Something like this:

    Dim item as ListviewItem

    If lvCollection.SelectedItems > 0 Then
        item = lvCollection.SelectedItems(0)
        cmbCond.Text = item.Text
        txtQty.Text = item.SubItems(1).Text
        cmbLocation.Text = item.SubItems(2).Text
        txtColComment.Text = item.SubItems(3).Text
     End If

Kerry Moorman


Show quoteHide quote
"George" wrote:

> I was able to get to the following:
>
>         Dim itemall As ListView.SelectedListViewItemCollection =
> lvCollection.SelectedItems
>         Dim item As ListViewItem
>         For Each item In itemall
>             cmbCond.Text = item.Text
>             txtQty.Text = item.SubItems(1).Text
>             cmbLocation.Text = item.SubItems(2).Text
>             txtColComment.Text = item.SubItems(3).Text
>         Next
>
>
> But it seems a bit overdone, since my listview is not multi select.  Is
> there a more straightforward way to get what was selected?
>
> George
> "Kerry Moorman" wrote:
>
> > George,
> >
> > Are you using an IF statement to make sure that an item is selected, like I
> > did in my original example?
> >
> > If you are writing your code in the listview's SelectedIndexChanged event,
> > when you select a second item, the first item is de-selected and the
> > SelectedIndexChanged event fires. At that point there is no SelectedItems(0).
> >
> > Kerry Moorman
> >
> >
> > "George" wrote:
> >
> > >
> > > Thanks for that - it is straight to the point!
> > >
> > > I used the example, and it worked - sort of.  The first time I clicked on an
> > > item, it returned the values fine.  But when I clicked on another item I got
> > > the following error:
> > >
> > > InvalidArgument=Value of '0' is not valid for 'index'.
> > >
> > >
> > > This is the code:
> > >
> > >         Dim item = lvCollection.SelectedItems(0)
> > >         cmbCond.Text = item.Text
> > >         txtQty.Text = item.SubItems(1).Text
> > >
> > > It works the first time, but not the second?
> > >
> > > Thanks in advance,
> > >
> > > George
> > >
> > > "Kerry Moorman" wrote:
> > >
> > > > George,
> > > >
> > > > Here is one way to get the first selected item:
> > > >
> > > >         Dim item As ListViewItem
> > > >         If lvwInvoice.SelectedItems.Count > 0 Then
> > > >             item = lvwInvoice.SelectedItems(0)
> > > >             MsgBox(item.Text)
> > > >             MsgBox(item.SubItems(1).Text)
> > > >         End If
> > > >
> > > >
> > > >
> > > > "George" wrote:
> > > >
> > > > > Hi all,
> > > > >
> > > > > How can I get the value stored from the selected item and subitems of a
> > > > > listview?
> > > > >
> > > > > Thanks in advance,
> > > > >
> > > > > George