Checkbox does not trigger selected item

2 posts, 1 answers
  1. Claude
    Claude avatar
    63 posts
    Member since:
    Apr 2009

    Posted 29 Apr 2012 Link to this post

    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.
  2. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 30 Apr 2012 Link to this post

    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;
        }
    }
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top