I successfully implement in my application a listview that can get the values of selected items using a for loop

for (int i = 0; i < eventlv.Items.Count; i++)
                if (eventlv.Items[i].Checked == true)

and using this to get the specific value on my parameter


Though, I found out that a listview also supports checkbox so I decided to work on it but I'm having difficulty. How can I determine which is checked and get the value of subitem[0].

Edited by AngelicOne: n/a

7 Years
Discussion Span
Last Post by Mitja Bonca

you code of selected item is incorect, you have to check for "Selected" property, not "Checked".

Checked property is especially used for checkBoxes.


Here is an example code how to use checkBoxes in listView:

public partial class Form1 : Form
        List<string> list;
        public Form1()
            listView1.View = View.Details;
            listView1.CheckBoxes = true;
            listView1.Columns.Add("name", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("age", 70, HorizontalAlignment.Center);
            listView1.Columns.Add("country", -2, HorizontalAlignment.Center);

            ListViewItem item1 = new ListViewItem(new string[] { "mitja", "28", "Slovenia" });
            ListViewItem item2 = new ListViewItem(new string[] { "john", "29", "USA" });
            ListViewItem item3 = new ListViewItem(new string[] { "sara", "22", "Germany" });
            listView1.Items.AddRange(new ListViewItem[] { item1, item2, item3 });

        private void button1_Click(object sender, EventArgs e)
            list = new List<string>();
            //I will put the names and ages into the list where tick is added:
            for (int i = 0; i < listView1.Items.Count; i++)
                if (listView1.Items[i].Checked)
                    list.Add("NAME: " + listView1.Items[i].Text + "  -  " + "AGE: " + listView1.Items[i].SubItems[1].Text);
            if (list.Count > 0)
                //showing the names:
                string names = null;
                foreach (string name in list)
                    names += name + Environment.NewLine;
                MessageBox.Show("Selected names are:\n\n" + names);
                MessageBox.Show("No names selected.");

Edited by Mitja Bonca: n/a

Votes + Comments
Showing great effort.

It's .Selected property, just mistyped. Thanks, I understand it now. Though I used

foreach (ListViewItems item in eventlv.Items

to work on the listviewitems itself.


Sure no problem, you simply chenge the code a bit to:

//I will put the names and ages into the list where tick is added:
            foreach (ListViewItem item in listView1.Items)
                if (item.Checked)
                    list.Add("NAME: " + item.Text + " - AGE: " + item.SubItems[1].Text);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.