0

The code bellow allow to select only item in a listView. That means there is only one tick in the listView - always.

But I would like to change the code bellow that will allow to unselect too, that there will be no tick at all.
So, no tick in the listView, or only one.

This is the code I would like to change. And I can not succeed it.

bool isChecking;
bool canCheck;
private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            if (!listView1.GetItemAt(e.X, e.Y).Checked)
            {
                canCheck = true;
                listView1.GetItemAt(e.X, e.Y).Checked = true;
            }
        }


        private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (!isChecking && canCheck)
            {
                isChecking = true;
                foreach (ListViewItem item in listView1.Items)
                {
                    item.Checked = false;
                }
                listView1.Items[e.Index].Checked = true;
                e.NewValue = CheckState.Checked;
                canCheck = false;
                isChecking = false;
            }
            else
            {
                if (isChecking)
                {
                    e.NewValue = CheckState.Unchecked;
                }
                else
                {
                    e.NewValue = e.CurrentValue;
                    //Now I have a method here, when the new tick is checked!
                }
            }
        }
1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Mitja Bonca
0

I have added some code, and it works, but its not th best solution I would say... its a bit complicated.

What do you think?

bool isUnckecked;
bool whenUnckecked;
bool isChecking;
bool canCheck;
private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            if (!listView1.GetItemAt(e.X, e.Y).Checked)
            {
                canCheck = true;
                listView1.GetItemAt(e.X, e.Y).Checked = true;
            }
            else
                isUnchecked = true;
        }


        private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (isUnchecked)
                {
                    isUnckecked = false;
                    isChecking = true;
                    listView1.Items[e.Index].Checked = false;
                    e.NewValue = CheckState.Unchecked;
                   //MY NEW CODE FOR WHEN THERE IS NO ITEM CHECKED!!
                    isChecking = false;
                    whenUnckecked = true;
                }

            if (!isChecking && canCheck)
            {
                isChecking = true;
                foreach (ListViewItem item in listView1.Items)
                {
                    item.Checked = false;
                }
                listView1.Items[e.Index].Checked = true;
                e.NewValue = CheckState.Checked;
                canCheck = false;
                isChecking = false;
            }
            else
            {
                if (isChecking || whenUnckecked) 
                {
                    e.NewValue = CheckState.Unchecked;
                    whenUnckecked = false;
                }
                else
                {
                    e.NewValue = e.CurrentValue;
                    //Now I have a method here, when the new tick is checked!
                }
            }
        }

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. 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.