This is a migrated thread and some comments may be shown as answers.

Checkbox does not trigger selected item

1 Answer 142 Views
CheckedListBox
This is a migrated thread and some comments may be shown as answers.
Claude
Top achievements
Rank 1
Claude asked on 29 Apr 2012, 11:01 AM
When You select a checkbox in the list view, the selected item is not updated. I want to limit the user to the number of checkboxes checked -10. How can this be done? I can keep count of the total, but when the user goes over the total, I can put up a messagebox but have no way of knowing what the 11th checked so I can uncheck it.

1 Answer, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 30 Apr 2012, 10:34 AM
Hello Claude, 

You can do this via the ItemCheckedChanging event. 
Please consider the following sample. 

If you find this helps, please remember to mark as answer. If you have further questions, please let me know
Thanks
Richard

public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    int _CheckCount = 0;
 
    public RadForm1()
    {
        InitializeComponent();
 
        this.Text = "Start " + DateTime.Now.ToLongTimeString();
 
        List<User> users = new List<User>();
        int i = 1;
        while (i <= 5000)
        {
            users.Add(new User(i, "User " + i.ToString()));
            i++;
        }
        this.radListView1.DataSource = users;
        this.radListView1.DisplayMember = "Name";
        this.radListView1.ValueMember = "Id";
 
 
        this.radListView1.ShowCheckBoxes = true;
        this.radListView1.ItemCheckedChanging += new ListViewItemCancelEventHandler(radListView1_ItemCheckedChanging);
        
    }
 
    void radListView1_ItemCheckedChanging(object sender, ListViewItemCancelEventArgs e)
    {
        // if it's currently on, it will be going off, and vice versa.
        if (e.Item.CheckState == Telerik.WinControls.Enumerations.ToggleState.Off)
        {
            if (_CheckCount == 10)
            {
                RadMessageBox.Show("Only 10 Checks are allowed");
                e.Cancel = true;
            }
            else
            {
                _CheckCount++;
            }
        }
        else
        {
            _CheckCount--;
        }
    }
}
 
public class User
{
    public User(int id, string name)
    {
        Id = id;
        Name = name;
    }
 
    public User()
    { }
 
    public int Id
    {
        get;
        set;
    }
 
    public string Name
    {
        get;
        set;
    }
}
Tags
CheckedListBox
Asked by
Claude
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Share this question
or