none
ListView.CheckBoxes: how to make checkboxe work like a RadioButton.

    Question

  • Hi All,

    I have a VB.Net Winform showing checkboxes inside the ListView which works fine.

    I want to know how to make the checkbox work like the RadioButton. ie. I should allow the user to click only one

    checkbox on the listview. If there are 2 checkboxes in 2 rows and then when the 2nd checkbox is checked, the first checkbox "Checked" should be cleared. When the 2nd checkbox is clicked the 1st checkbox "check" to be cleared.
     
    If anyone had done this, let me know the way..

     Thanks,

    sankar.




    sankar sv


    sankar sv
    Friday, July 25, 2008 7:56 PM

All replies

  • This?

        Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
            Me.CheckBox2.Checked = Not Me.CheckBox1.Checked
        End Sub

        Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
            Me.CheckBox1.Checked = Not Me.CheckBox2.Checked
        End Sub
    Friday, July 25, 2008 8:16 PM
  • That would be the way to do it, but ordinarily, you shouldn't: you would use radio buttons.

    The whole point of radio buttons being different from check boxes is that the user knows that more than one check box can be selected, but only one radio button can be selected.

    Stephen J Whiteley
    Friday, July 25, 2008 8:53 PM
    Moderator
  • Hi

    firstoff, thanks for ur quick reply...

    I am not having only 2 checkboxes, i just gave an example.
     
    i am having a ListView with  "10" columns and "X" number of  Dynamic Rows and each rows will have a "CheckBox" for the all the rows.

    Now, what i want is if user clicks the first checkbox, and checks the 2nd the 1st 'Check" should go off, and if they check 3rd checkbox and try to check the 4th one, the 3rd 'check' should goes of. 
    Basically, user should be allowed adn restricted to 'Check' only one checkbox on the listview.

    thanks for ur reply

    sankar


    sankar sv
    Friday, July 25, 2008 10:28 PM
  • I know the question was asked a while, but I did not find an answer on the Internet for that.
    So, I'll post my solution here:

    When I want to select only one box, I used the "ItemCheck":

    foreach (ListViewItem item in listView1.Items)
    {
        item.Checked = false;
    }
    listView1.Items [e.Index]. Checked = true; 

    But this causes an StackOverflowException error. This error occurs because the function calls itself every time you change the value "Checked" of an item and never returns.

    What you should do is:
    - Say to the function you are working and not finished the job yet, so she will direct, returning to the original code.

    ItemCheck()->ItemCheck()->return;

    Then, the code looks like this:

    bool isChecking;
    private void listView1_ItemCheck (object sender, ItemCheckEventArgs e)
    {
        if (!isChecking)
        {
            isChecking = true;
            foreach (ListViewItem item in listView1.Items)
            {
                item.Checked = false;
            }
            listView1.Items [e.Index]. Checked = true;
            e.NewValue = CheckState.Checked;
            isChecking = false;
        }
    }

    This code works as a "RadioButton", only one box will be marked forever.

    For only check with MouseDoubleClick on a Item, we put a lock.
    So always use to unlock:

    // Releases the lock
    canCheck = true;
    // Now we check the item
    item.Checked = true;

    The code with a lock:

    bool isChecking;
    bool canCheck;
    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;
            }
        }
    }
    
    private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        // We can't unlock the lock if the value doesnt change.
        if (!listView1.GetItemAt(e.X, e.Y).Checked)
        {
            canCheck = true;
            listView1.GetItemAt(e.X, e.Y).Checked = true;
        }
    }

    akyryz
    Friday, October 23, 2009 4:55 AM
  • Please check whether this is Ok   

     Private Sub Listview1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles Listview1.ItemCheck
     
        Dim item As ListViewItem
        
        For Each item In Listview1.Items
          If Not item.Index = e.Index Then
            item.Checked = False
          End If
    
        Next
    
      End Sub



    Regards,
    Rjkani
    • Proposed as answer by Rjkani Monday, March 14, 2011 11:10 PM
    Monday, March 14, 2011 11:10 PM
  • Hey Guys,

    This is exactly what I was looking for. As I was reading akyryz idea, I though surely you could just add a NOT in the For..Each loop - then I read Rjkani response.

    Its a bit of a shame that that this functionality isn't part of the CheckBoxes property of the ListView control. Instead of just a boolean value (True/False), it would be nice if you had the options "None", "CheckBoxes", "RadioButton", and then they could implement the RadioButton control rather than the CheckBox so it looks like a option select.

    I was after looking to use the ListView in a form that is used to set default selections for an application. The option values are read in from a DB, then the user can select the option they want, and the selction can then be saved either back to a separate table in the DB, or to a local file.

    Chris


    Chris

    Saturday, September 22, 2012 4:29 PM