-
May 1st, 2021, 04:36 AM
#1
Thread Starter
Lively Member
Getting the selected item in a ListView control
I am trying to get the selected item in a ListView control, the multi select is set to FALSE.
Is the SelectedIndexChanged event the best place to respond to a change in selection?
How do I get the current selected index, assuming only one item is selected, dealing with multi select will come later
I tried CurrentSelection = ListView_DirectorList.SelectedItems(0).Index
but it keeps throwing exceptions :
System.ArgumentOutOfRangeException: 'InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index'
Rick
-
May 1st, 2021, 04:49 AM
#2
Re: Getting the selected item in a ListView control
see if this helps
Code:
Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged
'get Listview Data to Textbox
'if you click on a Line in the Listview
'the Items will be put in the Textboxes
For Each lvItem As ListViewItem In ListView1.SelectedItems
MessageBox.Show(lvItem.Index)
TextBox1.Text = lvItem.SubItems(0).Text
TextBox2.Text = lvItem.SubItems(1).Text
TextBox3.Text = lvItem.SubItems(2).Text
Next
End Sub
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
May 1st, 2021, 04:56 AM
#3
Thread Starter
Lively Member
Re: Getting the selected item in a ListView control
I have multiselect set to false so I only need the index for the one item... I see how this could be made to work (looping through the selected items collection), but there must be a simpler way to just determine the currently selected item ???
-
May 1st, 2021, 04:56 AM
#4
Re: Getting the selected item in a ListView control
This is an issue that a lot of people encounter because the ListView works differently to other controls in this regard. If an item is selected and then you select a different item, the SelectedIndexChanged event is raised twice. The first event occurs when the previous item is unselected. At that point, no items are selected, hence your exception. The second event occurs when the new item is selected, at which point 0 will be a valid index. You need to check that the SelectedItems.Count is greater than zero before trying to get the selected item. That will effectively ignore the first event and only act on the second.
-
May 1st, 2021, 04:58 AM
#5
Thread Starter
Lively Member
Re: Getting the selected item in a ListView control
That is subtle, I realized that something wonky was happening because of the behavior, but did not necessarily connect it to the event firing twice. But I guess by strict definition of 'CHANGE' it should.
thanks
Rick
-
May 1st, 2021, 05:03 AM
#6
Re: Getting the selected item in a ListView control
Originally Posted by Since VB_DOS
I have multiselect set to false so I only need the index for the one item... I see how this could be made to work (looping through the selected items collection), but there must be a simpler way to just determine the currently selected item ???
well when I use a Listview I add a Checkbox
Code:
With ListView1
.Items.Clear()
.Columns.Clear()
.View = View.Details
.CheckBoxes = True '<-----
if I need the Data to process I use Clone
in Form2 I see the Data selected from Form1 in this example
this can be 1 Item checked or n Items
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'first clear the previous selected Items in Form2.Listview1
Form2.ListView1.Clear()
'show the Form
Form2.Show()
'now update the Listview in Form2 with Checked items from Form1
For Each Checkeditem As ListViewItem In ListView1.CheckedItems
Form2.ListView1.Items.Add(Checkeditem.Clone)
Debug.Print(Checkeditem.Index)
Next
End Sub
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
May 1st, 2021, 06:49 AM
#7
Thread Starter
Lively Member
Re: Getting the selected item in a ListView control
Another quick question, I cannot seem to figure out how to prevent the Listview control from showing multiple columns.... I really just want a single column list? The view is set to LIST.
Rick
-
May 1st, 2021, 07:02 AM
#8
Re: Getting the selected item in a ListView control
Originally Posted by Since VB_DOS
Another quick question, I cannot seem to figure out how to prevent the Listview control from showing multiple columns
Look at the title of this thread. What does that question have to do with that topic? Nothing. Please keep each thread to a single topic and each topic to a single thread. If you have a question on a different topic, just start another thread.
Anyway, you could try Details view with a single column but then you have to have a column header. If you want to see how a ListView works, spend some time with File Explorer in Windows.
-
May 2nd, 2021, 08:00 AM
#9
Re: Getting the selected item in a ListView control
If you want to learn about using a ListView including obtaining the selected single item see
TechNet Wiki article Windows Forms ListView: Examining Detail view with SQL-Server (VB.NET)
Full source
What is shown can help if you take time to study the code along get past it's not an exact match to your code.
-
May 2nd, 2021, 08:49 AM
#10
Thread Starter
Lively Member
Re: Getting the selected item in a ListView control
-
May 6th, 2021, 01:34 PM
#11
Re: Getting the selected item in a ListView control
I'd use Details View. If you just want one column, don't add SubItems...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|