I'm doing ListView Wrong

I have winforms ListView to which I have added items that have subitems. Looks good. I have enabled CheckBoxes. I have a ListView.Click handler enabled. When I click on a row, nothing happens... surprising, but OK.

When I click on a checkbox in a row, the ListView.Click() handler fires but thows an exception on the first line:

ListViewItem item = alarmList.SelectedItems[0];

In fact, SelectedItems has count of 0. It is like the line is not selected, even tho it responded to the Click. How can this be?

How can I get the Item for the checkBox I just clicked on?

Dave
davecoveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anarki_jimbelSenior DeveloperCommented:
Use another event. Try
private void button3_Click(object sender, EventArgs e)
        {
            ListViewItem i1 = new ListViewItem("hello");
            ListViewItem.ListViewSubItem lvsi1 = new ListViewItem.ListViewSubItem( i1,"abc");
  
            i1.SubItems.Add(lvsi1);
            ListViewItem i2 = new ListViewItem("goodbye");
            ListViewItem.ListViewSubItem lvsi2 = new ListViewItem.ListViewSubItem(i2,"def");
            i2.SubItems.Add(lvsi2);
            this.listView1.Items.Add(i1);
            this.listView1.Items.Add(i2);
        }


        private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            ListViewItem item = e.Item;
            MessageBox.Show(item.Text);
        }

Open in new window

davecoveAuthor Commented:
I tried ItemChecked, but that also fires off when an item is added to the list. Apparently being instantiated counts as a checked event.

 I am doing a lot of add/delete in this list and I don't want to get triggered on the .Add()s.... only when the user actually does something.  That's why the .Click()

Dave
anarki_jimbelSenior DeveloperCommented:
Add some flag like below. Do you really think this question worth 125 :)?
private bool isBeingAdded = false;

        private void button3_Click(object sender, EventArgs e)
        {
            isBeingAdded = true;
            ListViewItem i1 = new ListViewItem("hello");
            ListViewItem.ListViewSubItem lvsi1 = new ListViewItem.ListViewSubItem( i1,"abc");
  
            i1.SubItems.Add(lvsi1);
            ListViewItem i2 = new ListViewItem("goodbye");
            ListViewItem.ListViewSubItem lvsi2 = new ListViewItem.ListViewSubItem(i2,"def");
            i2.SubItems.Add(lvsi2);
            this.listView1.Items.Add(i1);
            this.listView1.Items.Add(i2);
            isBeingAdded = false;

        }

        private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (!isBeingAdded)
            {
                ListViewItem item = e.Item;
                MessageBox.Show(item.Text);
            }

        }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.