item checklist checkedlistbox1 checkeditems c# winforms checkedlistbox

checkedlistbox1 - checklist c#



¿Cómo deshacerse del efecto de resaltado de selección de checklistbox? (5)

Cuando se hace clic en un elemento en el checklistbox, se resalta. ¿Cómo puedo evitar este efecto de resaltado?

Puedo engancharme en el evento SelectedIndexChanged y borrar la selección, pero el resaltado aún ocurre y se ve una señal. De hecho, si mantiene presionado el botón del mouse, nunca lo suelta después de hacer clic en el área de la casilla de verificación, la selección permanece resaltada hasta que suelta el botón del mouse. Básicamente quiero deshacerme de este efecto de realce por completo.


esto lo hará aparte de que aún consigues el bit de la línea punteada.

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

aunque ahora no puede hacer clic en las casillas de verificación ... por lo que tendrá que hacer algo como esto:

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; } } } }

si todo eso no es lo que buscas ... siempre puedes hacer tu propio. es un control bastante simple.


Establecer SelectionMode en None tiene algunos problemas extraños como tener que implementar el evento Click. Puede dejar SelectionMode en single y luego crear una clase que anule CheckedListBox con solo OnDrawItem. Tenga en cuenta que para desactivar una apariencia seleccionada, debe desactivar el estado Seleccionado y establecer los colores según lo que desee. Puedes obtener el color original del control como lo hice aquí. Esto es lo que se me ocurrió y debería comenzar a hacer que parezca como quieras.

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); } }


Use lo siguiente:

private void checkedListBox1__SelectedIndexChanged(object sender, EventArgs e) { checkedListBox1.ClearSelected(); }


ooh cool add puso todo el código de la respuesta de Hath en un

checkedListBox1_MouseMove(object sender, MouseEventArgs e)

agregar si el botón del mouse mordió el interruptor

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

y

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

y comprobará los elementos resaltados al mover el mouse con el botón izquierdo presionado y desmarcarlos con el derecho


Estoy un poco tarde para dar una respuesta aquí. De todos modos, esto desmarcará todas las casillas de verificación y eliminará el efecto de resaltado :

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;

  • Desmarque todas las casillas de verificación
  • Deshabilitar la selección de checkListBox
  • Habilitar la selección de checkListBox