Domanda

Quando si fa clic su un elemento nella casella di controllo, viene evidenziato. Come posso evitare questo effetto di evidenziazione?

Posso collegarmi all'evento SelectedIndexChanged e cancellare la selezione, ma l'evidenziazione continua e si vede un blip. In effetti, se si tiene premuto il clic del mouse, non rilasciandolo mai dopo aver fatto clic sull'area della casella di controllo, la selezione rimane evidenziata fino al rilascio del pulsante del mouse. In pratica, voglio eliminare del tutto questo effetto di evidenziazione.

È stato utile?

Soluzione

questo lo farà a parte il fatto che otterrai comunque il bit della linea tratteggiata.

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

anche se ora non puoi fare clic sulle caselle di controllo ... quindi dovrai fare qualcosa del genere:

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

          }

        }
    }

se tutto ciò non è ciò che cerchi .. puoi sempre crearne uno tuo. è un controllo abbastanza semplice.

Altri suggerimenti

Utilizzare quanto segue:

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

L'impostazione di SelectionMode su None ha alcuni strani problemi come la necessità di implementare l'evento Click. È possibile lasciare SelectionMode impostato su single e quindi creare una classe che sovrascrive CheckedListBox con solo OnDrawItem. Si noti che per disattivare un aspetto selezionato, è necessario disattivare lo stato Selezionato e impostare i colori come desiderato. Puoi ottenere il colore originale dal controllo come ho fatto qui. Questo è quello che mi è venuto in mente e dovresti iniziare a farlo apparire come desideri.

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

ooh cool aggiungi metti tutto il codice dalla risposta di Hath in un

 checkedListBox1_MouseMove(object sender, MouseEventArgs e)

aggiungi se il pulsante del mouse è entrato nello switch

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

e

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

e controllerà gli elementi evidenziati mentre muovi il mouse con il tasto sinistro premuto e deselezionali con il giusto

Sono un po 'in ritardo per dare una risposta qui. Ad ogni modo, questo deselezionerà tutte le caselle di controllo e rimuoverà l'effetto evidenziazione :

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;
  • Deseleziona tutte le caselle di controllo
  • Disabilita la selezione di checkListBox
  • Abilita la selezione di checkListBox
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top