locked
C#: How to select all items in a ListView control with CheckBoxes from programs ? RRS feed

  • Question

  • I have developing a C# program by using a ListView control with CheckBoxes. How can I select all entries from a C# program (all CheckBoxes "checked"), or to de-select all entries (CheckBoxes "un-checked")?

    Thanks,

    • Moved by CoolDadTx Tuesday, August 30, 2011 1:35 PM Not IDE related (From:Visual C# IDE)
    Tuesday, August 30, 2011 3:12 AM

Answers

  • Hi TFukuda,

    Is that "a ListView control with CheckBoxes" means a ListView with CheckBoxes property set to true? If so, you can check or uncheck all the items like:

    private void button3_Click(object sender, EventArgs e)
        { 
           for (int i = 0; i < listView2.Items.Count; i++)
          {
            listView2.Items[i].Checked = true;
          }
        }

    If you have any concern, please feel free to let me know.

    Regards,

     


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, August 31, 2011 9:38 AM
  • Below is the function that does the work for you. Just pass your listview and checkstate to the function.

    public void CheckAllItems(ListView lvw, bool check)
    {
        lvw.Items.OfType<ListViewItem>().ToList().ForEach(item => item.Checked = check);
    }
    

    Hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!
    Wednesday, August 31, 2011 10:10 AM
  • Or:

    foreach (ListItem item in ListBox1.Items )
    {
       item.Selected = true;
    }
    



    Mitja
    Wednesday, August 31, 2011 10:03 AM

All replies

  • Hi TFukuda,

    Is that "a ListView control with CheckBoxes" means a ListView with CheckBoxes property set to true? If so, you can check or uncheck all the items like:

    private void button3_Click(object sender, EventArgs e)
        { 
           for (int i = 0; i < listView2.Items.Count; i++)
          {
            listView2.Items[i].Checked = true;
          }
        }

    If you have any concern, please feel free to let me know.

    Regards,

     


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, August 31, 2011 9:38 AM
  • Or:

    foreach (ListItem item in ListBox1.Items )
    {
       item.Selected = true;
    }
    



    Mitja
    Wednesday, August 31, 2011 10:03 AM
  • Below is the function that does the work for you. Just pass your listview and checkstate to the function.

    public void CheckAllItems(ListView lvw, bool check)
    {
        lvw.Items.OfType<ListViewItem>().ToList().ForEach(item => item.Checked = check);
    }
    

    Hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!
    Wednesday, August 31, 2011 10:10 AM
  • Hi Helen Zhou,

    It works. Thank you very much for your response.

    Takashi Fukuda

    Wednesday, August 31, 2011 12:44 PM
  • Hi Mitja,

    Thank you very much for your advice. I will try this approach also.

    Takashi Fukuda

     

    Wednesday, August 31, 2011 12:46 PM
  • Hi Adavesh,

    Thank you very much for your advice. It looks very interesting.

    Takashi Fukuda

    Wednesday, August 31, 2011 12:47 PM
  • You just need to write a code below button_Click 


                foreach (ListViewItem listItem in listView1.Items)
                {
                    checkBox1.Checked = true;
                    listItem.Checked = true;
                }

    if you have taken checkBox, below the listview for one click it should select all the listItems as well uncheck all the items in a listView!

    for checkBox code:

                                        

     private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                foreach (ListViewItem listItem in listView1.Items)
                {

                    if (checkBox1.Checked == true)
                    {
                        listItem.Checked = true;
                    }

                    if (checkBox1.Checked == false)
                    {
                        listItem.Checked = false;
                    }
                }

            }

    Enjoy!


    Wednesday, September 9, 2015 8:12 AM