Click here to Skip to main content
13,633,451 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hey Team. I'm having some items inside my list view with check boxes.
I have one radio button outside list view control to select/deselect all check boxes inside list view control.
Thanks in advance for the help!

Below are the Criteria which i want to satisfy:

1.if radio button is fired for the first time, all the check boxes inside list view control should be checked.
2.If any of the check boxes is unchecked manually, clicking the radio button should once again select all the check boxes.
3.If the same radio button is fired again, all the check boxes inside list view control should be unchecked.
3.If any of the check boxes is checked manually, clicking the radio button should once again deselect all the check boxes.

What I have tried:

Was trying to google it, but unfortunately i could not get exactly what i want to satisfy.
Posted 2-Jan-18 17:28pm
Updated 11-Jul-18 3:59am
v2
Comments
Karthik Bangalore 3-Jan-18 1:59am
   
using one radio button? how it is possible to check/uncheck the control?
use checkbox instead.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You may try this:

bool isChecked = false;
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    for (int i = 0; i < routeNamesListView.Items.Count; i++)
    {
        if (radioButton1.Checked)
            routeNamesListView.Items[i].Checked = true;
        else
            routeNamesListView.Items[i].Checked = false;
    }
    isChecked = radioButton1.Checked;
}

private void radioButton1_Click(object sender, EventArgs e)
{
    if (radioButton1.Checked && !isChecked)
        radioButton1.Checked = false;
    else
    {
        radioButton1.Checked = true;
        isChecked = false;
    }
}
  Permalink  
Comments
Arvi.S 3-Jan-18 0:46am
   
Pradeep. There seems to be no errors but it is not working. I now tell you here that all the check boxes in list view control is checked in default as i use the below code.

routeNamesListView.CheckBoxes = true;
{
for (int i = 0; i < routeNamesListView.Items.Count; i++)
{
routeNamesListView.Items[i].Checked = true;
}
}

Will it be good to select/Deselect check boxes using a single check box? If so kindly let me know
Pradeep Arthanari 3-Jan-18 0:56am
   
Pls refer my above code. It has got a radio button to select/deselect all the checkboxes in the list view control.

You need to add a radio button and add the 2 events for the radio button given above (Checkchanged & Click)
Arvi.S 3-Jan-18 1:01am
   
Yeah..I did whatever you have coded above. But its not working and throws zero error.
As i mentioned in the above comment, my check boxes comes checked in default which i did not mention in my question. Should i have to change anything from your code?

And in the code which you have sent, if the radio button is clicked once, it is not accepting for the next click action.
Pradeep Arthanari 3-Jan-18 1:28am
   
Could you copy your code.
Arvi.S 3-Jan-18 1:31am
   
Declared "bool isChecked = false;" globally.

private void t1RadioButton_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i < routeNamesListView.Items.Count; i++)
{
if (t1RadioButton.Checked)
routeNamesListView.Items[i].Checked = true;
else
routeNamesListView.Items[i].Checked = false;
}
isChecked = t1RadioButton.Checked;
}

private void t1RadioButton_Click(object sender, EventArgs e)
{
if (t1RadioButton.Checked && !isChecked)
t1RadioButton.Checked = false;
else
{
t1RadioButton.Checked = true;
isChecked = false;
}
}

Once again I'm repeating, all the check boxes in list view control is checked by default!
Pradeep Arthanari 3-Jan-18 1:57am
   
Try to keep the ListView and radio button check in Sync. I mean check your check box on page load itslef as your listview is checked by default.

t1RadioButton.Checked = true;
Pradeep Arthanari 3-Jan-18 2:06am
   
I would suggest you to use one common check box instead of radio button to select/unselect. you can try the below code for that:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i < routeNamesListView.Items.Count; i++)
{
if (checkBox1.Checked)
routeNamesListView.Items[i].Checked = true;
else
routeNamesListView.Items[i].Checked = false;
}
}

And enable your checkbox in the page load itself as your listview is checked by default.

checkBox1.Checked = true;
Arvi.S 4-Jan-18 2:18am
   
Thank you so much pradeep!!. This works very well!!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

To deselect specific buttons I used code like below:

RadioButton3.checked:=false;

It works fine in my app.
Hope this helps you.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.180712.1 | Last Updated 11 Jul 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100