How to get rid of checkedlistbox selection highlighting effect?
Asked Answered
E

5

15

When an item is clicked in the checkedlistbox, it gets highlighted. How can I prevent this highlighting effect?

I can hook into the SelectedIndexChanged event and clear the selection, but the highlighting still happens and you see a blip. In fact, if you hold down the mouse click, never releasing it after you clicked on the checkbox area, the selection remains highlighted until you release the mouse button. I basically want to get rid of this highlighting effect altogether.

Espinal answered 2/12, 2008 at 16:42 Comment(0)
L
13

this will do it apart from you still get the dotted line bit.

this.checkedListBox1.SelectionMode = System.Windows.Forms.SelectionMode.None;

although now you can't click the check boxes... so you'll have to do something like so:

  private void checkedListBox1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < checkedListBox1.Items.Count; i++)
        {


          if (checkedListBox1.GetItemRectangle(i).Contains(checkedListBox1.PointToClient(MousePosition)))
          {
              switch (checkedListBox1.GetItemCheckState(i))
              {
                  case CheckState.Checked:
                      checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
                      break;
                  case CheckState.Indeterminate:
                  case CheckState.Unchecked:
                      checkedListBox1.SetItemCheckState(i, CheckState.Checked);
                       break;
              } 

          }

        }
    }

if all that isn't what your after.. you can always just make your own one. its a fairly simple control.

Loraineloralee answered 2/12, 2008 at 16:54 Comment(1)
Seems like it would be more efficient to use checkedListBox1.IndexFromPoint(e.x, e.y) in the MouseDown handler, instead of looping through the GetItemRectangle results.Foretopsail
M
30

Use the following:

private void checkedListBox1__SelectedIndexChanged(object sender, EventArgs e)
        {
            checkedListBox1.ClearSelected();
        }
Mota answered 8/6, 2012 at 10:19 Comment(6)
tanks this is driving me crazy. it's like the checked list box has multiple selection states.. why would want this as a default?!Incept
Winner, winner. Chicken Dinner.Moultrie
Just for clarification, @horgh is incorrect in saying that this would prevent it from checking or unchecking items. The act of clicking sets the selection and also sets the checkbox state. Clearing the selection does not clear the checkbox state.Lubber
For the record, this method still causes the text to briefly switch to the highlighted color. But good enough for today.Carbineer
I wasn't happy with the brief flash of highlighted color. I opted to ClearSelected() when creating the form and ClearSelected() when the the CheckBoxList loses focus. For me, this was the best compromise.Equitation
Short and simple. It saved lots of time.Goddamned
L
13

this will do it apart from you still get the dotted line bit.

this.checkedListBox1.SelectionMode = System.Windows.Forms.SelectionMode.None;

although now you can't click the check boxes... so you'll have to do something like so:

  private void checkedListBox1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < checkedListBox1.Items.Count; i++)
        {


          if (checkedListBox1.GetItemRectangle(i).Contains(checkedListBox1.PointToClient(MousePosition)))
          {
              switch (checkedListBox1.GetItemCheckState(i))
              {
                  case CheckState.Checked:
                      checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
                      break;
                  case CheckState.Indeterminate:
                  case CheckState.Unchecked:
                      checkedListBox1.SetItemCheckState(i, CheckState.Checked);
                       break;
              } 

          }

        }
    }

if all that isn't what your after.. you can always just make your own one. its a fairly simple control.

Loraineloralee answered 2/12, 2008 at 16:54 Comment(1)
Seems like it would be more efficient to use checkedListBox1.IndexFromPoint(e.x, e.y) in the MouseDown handler, instead of looping through the GetItemRectangle results.Foretopsail
C
7

Setting the SelectionMode to None has some weird issues like having to implement the Click event. You can leave the SelectionMode set to single and then create a class that overrides the CheckedListBox with just OnDrawItem. Note that in order to turn off a selected appearance, you have to turn off the Selected state and set the colors to what you want. You can get the original color from the control like I did here. This is what I came up with and should get you started in making it appear however you want.

public partial class EnhancedCheckedListBox : CheckedListBox
{
    /// <summary>Overrides the OnDrawItem for the CheckedListBox so that we can customize how the items are drawn.</summary>
    /// <param name="e">The System.Windows.Forms.DrawItemEventArgs object with the details</param>
    /// <remarks>A CheckedListBox can only have one item selected at a time and it's always the item in focus.
    /// So, don't draw an item as selected since the selection colors are hideous.  
    /// Just use the focus rect to indicate the selected item.</remarks>
    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        Color foreColor = this.ForeColor;
        Color backColor = this.BackColor;

        DrawItemState s2 = e.State;

        //If the item is in focus, then it should always have the focus rect.
        //Sometimes it comes in with Focus and NoFocusRect.
        //This is annoying and the user can't tell where their focus is, so give it the rect.
        if ((s2 & DrawItemState.Focus) == DrawItemState.Focus)
        {
            s2 &= ~DrawItemState.NoFocusRect;
        }

        //Turn off the selected state.  Note that the color has to be overridden as well, but I just did that for all drawing since I want them to match.
        if ((s2 & DrawItemState.Selected) == DrawItemState.Selected)
        {
            s2 &= ~DrawItemState.Selected;

        }

        //Console.WriteLine("Draw " + e.Bounds + e.State + " --> " + s2);

        //Compile the new drawing args and let the base draw the item.
        DrawItemEventArgs e2 = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, s2, foreColor, backColor);
        base.OnDrawItem(e2);
    }
}
Capitalism answered 4/2, 2012 at 21:49 Comment(1)
I love this solution, but without one minor tweak it was causing my Form1.cs [Design] view to crash. Adding a simple try/catch around the whole method block, with the following code fixed the problem for me and it's now working beautifully: catch (Exception) { base.OnDrawItem(e); }Saurian
C
0

ooh cool add put all the code from answer from Hath in a

 checkedListBox1_MouseMove(object sender, MouseEventArgs e)

add in if mouse button bit in to the switch

case CheckState.Checked:
   if (e.Button == MouseButtons.Right)
   {
       checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
   }                     
   break;

and

case CheckState.Unchecked:
   if (e.Button == MouseButtons.Left)
   {
       checkedListBox1.SetItemCheckState(i, CheckState.Checked);
   }
   break;

and it will check the highlighted items as you move the mouse with the left button pressed and un-highlight them with the right

Cuboid answered 4/7, 2014 at 10:42 Comment(0)
T
0

I am a little late to give an answer here. Anyway, this will uncheck all checkboxes and remove the highlighting effect:

foreach (int i in clb.CheckedIndices)  //clb is your checkListBox
    clb.SetItemCheckState(i, CheckState.Unchecked);
clb.SelectionMode = System.Windows.Forms.SelectionMode.None;
clb.SelectionMode = System.Windows.Forms.SelectionMode.One;
  • Uncheck all checkboxes
  • Disable checkListBox's selection
  • Enable checkListBox's selection
Tallahassee answered 19/2, 2017 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.