How to disable a checkbox in a checkedlistbox?

后端 未结 11 719
遥遥无期
遥遥无期 2020-12-03 10:46

I have some items in a CheckedListBox, I want to disable the CheckBox of first item in it.
i.e. I want to disable the first item in the C

相关标签:
11条回答
  • 2020-12-03 11:01

    Here's how I did it in a helpdesk application I wrote:

    First, I made it so the check box was greyed out as I added it to the list during form load:

        private void frmMain_Load(object sender, EventArgs e)
        {
            List<string> grpList = new List<string>();
            ADSI objADSI = new ADSI();
    
            grpList = objADSI.fetchGroups();
    
            foreach (string group in grpList)
            {
                if (group == "SpecificGroupName")
                {
                    chkLst.Items.Add(group, CheckState.Indeterminate);
    
                }
                else
                {
                    chkLst.Items.Add(group);
                }
    
            }
    

    Then I used an event so that when clicked it ensures it stays clicked:

        private void chkLst_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (chkLst.SelectedItem.ToString() == "SpecificGroupName")
            {
                chkLst.SetItemCheckState(chkLst.SelectedIndex, CheckState.Indeterminate);
            }
        }
    

    The idea here is that on my form it's set so that the box checks on item click/select. This way I could kill two birds with one stone. I could keep this event from causing problems when the item is first checked and added during form load. Plus making it check on select allows me to use this event instead of the item checked event. Ultimately the idea is to keep it from messing up during the load.

    You'll also notice that it doesn't matter what the index number is, that variable is unknown because in my app it's grabbing a list of groups from AD that exist in a specific OU.

    As to whether this is a good idea or not, that's dependent on the situation. I have another app where the item to disable is dependent on another setting. In this app I just want the helpdesk to see that this group is required so they don't go removing them from it.

    0 讨论(0)
  • 2020-12-03 11:05

    The solution is to use the event ItemChecking:

    _myCheckedListBox.ItemChecking += (s, e) => e.Cancel = true;
    

    This will cancel all the checking on every item, but you can always do more refined solution but testing the current .SelectedItem

    0 讨论(0)
  • 2020-12-03 11:07

    Though this post is pretty old, the last added answer has been submitted in April this year,
    and I hope this will help someone.
    I was after something similar : a checked list box that behaves like a lot of installers, which offer a list of options where some features are required and thus are both checked and disabled.
    Thanks to this post (Can I use a DrawItem event handler with a CheckedListBox?) I managed to do that, subclassing a CheckedListBox control.
    As the OP in the linked post states, in the CheckedListBox control the OnDrawItem event is never fired, so subclassing is necessary.
    It's very basic, but it works. This is what it looks like (the CheckBox above is for comparison) :

    checked list box

    NOTE: the disabled item is really disabled : clicking on it has no effects whatsoever (as far as I can tell).

    And this is the code :

    public class CheckedListBoxDisabledItems : CheckedListBox {
        private List<string> _checkedAndDisabledItems = new List<string>();
        private List<int> _checkedAndDisabledIndexes = new List<int>();
    
        public void CheckAndDisable(string item) {
            _checkedAndDisabledItems.Add(item);
            this.Refresh();
        }
    
        public void CheckAndDisable(int index) {
            _checkedAndDisabledIndexes.Add(index);
            this.Refresh();
        }
    
        protected override void OnDrawItem(DrawItemEventArgs e) {
            string s = Items[e.Index].ToString();
    
            if (_checkedAndDisabledItems.Contains(s) || _checkedAndDisabledIndexes.Contains(e.Index)) {
                System.Windows.Forms.VisualStyles.CheckBoxState state = System.Windows.Forms.VisualStyles.CheckBoxState.CheckedDisabled;
                Size glyphSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, state);
                CheckBoxRenderer.DrawCheckBox(
                    e.Graphics,
                    new Point(e.Bounds.X + 1, e.Bounds.Y + 1), // add one pixel to align the check gliph properly
                    new Rectangle(
                        new Point(e.Bounds.X + glyphSize.Width + 3, e.Bounds.Y), // add three pixels to align text properly
                        new Size(e.Bounds.Width - glyphSize.Width, e.Bounds.Height)),
                    s,
                    this.Font,
                    TextFormatFlags.Left, // text is centered by default
                    false,
                    state); 
            }
            else {
                base.OnDrawItem(e);
            }
        }
    
        public void ClearDisabledItems() {
            _checkedAndDisabledIndexes.Clear();
            _checkedAndDisabledItems.Clear();
            this.Refresh();
        }
    }
    

    Use it like this:

    checkedListBox.Items.Add("Larry");
    checkedListBox.Items.Add("Curly");
    checkedListBox.Items.Add("Moe");
    
    // these lines are equivalent
    checkedListBox.CheckAndDisable("Larry");
    checkedListBox.CheckAndDisable(0);
    

    Hope this can help someone.

    0 讨论(0)
  • 2020-12-03 11:07

    Try Below Code:

    Private Sub CheckedListBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles CheckedListBox1.MouseUp
            If (Condition) Then
             Me.CheckedListBox1.SelectedIndex = -1
            End If
    End Sub
    
    0 讨论(0)
  • 2020-12-03 11:08

    The CheckedListBox will not work in this way. CheckedListBox.Items is a collection of strings so they cannot be "disabled" as such.

    Here are some discussions about possible solutions that might help you: here and here.

    0 讨论(0)
  • 2020-12-03 11:11

    Disabling items isn't a great idea, the user will have no good feedback that click the check box won't have any effect. You cannot use custom drawing to make it obvious. Best thing to do is to simply omit the item.

    You can however easily defeat the user with the ItemCheck event:

        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
            if (e.Index == 0) e.NewValue = e.CurrentValue;
        }
    
    0 讨论(0)
提交回复
热议问题