2010-09-08 32 views

risposta

28

È necessario ignorare l'evento Drawitem e impostare la DrawMode proprietà DrawMode.OwnerDrawFixed

controllo questo campione

private void listBox1_DrawItem(object sender, DrawItemEventArgs e) 
{ 
    if (e.Index<0) return; 
    //if the item state is selected them change the back color 
    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) 
     e = new DrawItemEventArgs(e.Graphics, 
            e.Font, 
            e.Bounds, 
            e.Index, 
            e.State^DrawItemState.Selected, 
            e.ForeColor, 
            Color.Yellow);//Choose the color 

    // Draw the background of the ListBox control for each item. 
    e.DrawBackground(); 
    // Draw the current item text 
    e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault); 
    // If the ListBox has focus, draw a focus rectangle around the selected item. 
    e.DrawFocusRectangle(); 
} 

alt text

+0

Sono riuscito ad applicare questo codice ma gli articoli sulla lista hanno il colore bianco sullo sfondo e in primo piano. Non riesco a capire perché lo farebbe? – Qosmo

+1

imposta la proprietà 'DrawMode' a' DrawMode.OwnerDrawFixed'? – RRUZ

+1

Sì, altrimenti se in Normal è il colore predefinito di Windows. – Qosmo

1

Il codice qui sotto fa esattamente quello che stai dicendo:

Nel metodo InitializeComponent:

this.listBox1.DrawMode = DrawMode.OwnerDrawFixed; 
this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(listBox1_DrawItem); 
this.listBox1.SelectedIndexChanged += new System.EventHandler(listBox1_SelectedIndexChanged); 

E i gestori di eventi:

void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) 
{ 
    this.listBox1.Invalidate(); 
} 

void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) 
{ 
    int index = e.Index; 
    Graphics g = e.Graphics; 
    foreach (int selectedIndex in this.listBox1.SelectedIndices) 
    { 
     if (index == selectedIndex) 
     { 
      // Draw the new background colour 
      e.DrawBackground(); 
      g.FillRectangle(new SolidBrush(Color.Red), e.Bounds); 
     } 
    } 

    // Get the item details 
    Font font = listBox1.Font; 
    Color colour = listBox1.ForeColor; 
    string text = listBox1.Items[index].ToString(); 

    // Print the text 
    g.DrawString(text, font, new SolidBrush(Color.Black), (float)e.Bounds.X, (float)e.Bounds.Y); 
    e.DrawFocusRectangle(); 
} 

codice è tratto da:

http://www.weask.us/entry/change-listbox-rsquo-selected-item-backcolor-net

+0

sto diventando un problema con questo. La selezione diventa rossa ma rimane anche su tutti gli elementi che ho selezionato in precedenza, vanificando lo scopo di avere un elemento "visivo" selezionato. Cosa potrebbe essere? – Qosmo

6

Speriamo che questo vi aiuterà qualcuno in futuro, come il codice di cui sopra mi ha aiutato, ma non al 100%

Ho ancora avuto i seguenti problemi:
- quando ho selezionato un altro indice, l'indice appena selezionato evidenziava anche il rosso.
- quando ho cambiato la dimensione del carattere della casella di riepilogo, l'area evidenziata sarebbe troppo piccola.

Sotto correzioni problema

  • cambiamento del DrawMode a OwnerDrawVariable
  • creare un MeasurItem ed evento DrawItem per la casella di riepilogo
private void lstCartOutput_MeasureItem(object sender, MeasureItemEventArgs e) 
{ 
    // Cast the sender object back to ListBox type. 
    ListBox listBox = (ListBox)sender; 
    e.ItemHeight = listBox.Font.Height; 
} 

private void lstCartOutput_DrawItem(object sender, DrawItemEventArgs e) 
{ 
    ListBox listBox = (ListBox)sender; 
    e.DrawBackground(); 
    Brush myBrush = Brushes.Black; 

    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) 
    { 
     myBrush = Brushes.Red; 
     e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0, 64, 64)), e.Bounds); 
    } 

    else 
    { 
     e.Graphics.FillRectangle(Brushes.White, e.Bounds); 

    } 

    e.Graphics.DrawString(listBox.Items[e.Index].ToString(),e.Font, myBrush, e.Bounds); 
    e.DrawFocusRectangle(); 
} 


Ho anche fatto riferimento il sito MSDN .

+0

Questa dovrebbe essere la risposta corretta –

0

Ho lo stesso problema.

Sfortunatamente la mia origine dati è un elenco di classe entità. Così ho lo stesso codice con la risposta accettata sopra, ma con lievi modifiche per selezionare la proprietà esatta sulla mia classe che ho bisogno su coulisse per la mia ListBox:

if (e.Index < 0) return; 
     if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) 
      e = new DrawItemEventArgs(e.Graphics, 
             e.Font, 
             e.Bounds, 
             e.Index, 
             e.State^DrawItemState.Selected, 
             e.ForeColor, 
             Color.Yellow); 

    e.DrawBackground(); 
    //This is my modification below: 
    e.Graphics.DrawString(ctListViewProcess.Items.Cast<entMyEntity>().Select(c => c.strPropertyName).ElementAt(e.Index), e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault); 
    e.DrawFocusRectangle(); 
Problemi correlati