2008-11-29 39 views

Respuesta

30

Debe cambiar el DrawMode de listbox a DrawMode.OwnerDrawFixed. Echa un vistazo a estos artículos en MSDN:
DrawMode Enumeration
ListBox.DrawItem Event
Graphics.DrawString Method

también un vistazo a esta pregunta en los foros de MSDN:
Question on ListBox items

Un ejemplo sencillo (ambos artículos - Negro-Arial-10-Bold):

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

     ListBox1.Items.AddRange(new Object[] { "First Item", "Second Item"}); 
     ListBox1.DrawMode = DrawMode.OwnerDrawFixed; 
    } 

    private void ListBox1_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     e.DrawBackground(); 
     e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(), new Font("Arial", 10, FontStyle.Bold), Brushes.Black, e.Bounds); 
     e.DrawFocusRectangle(); 
    } 
} 
+0

Gran ejemplo y enlaces. ¡Ojalá pudiera votar esto dos veces! –

0

A continuación se muestra el código que demuestra lo mismo.

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication2 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      foreach (FontFamily fam in FontFamily.Families) 
      { 
       listBox1.Items.Add(fam.Name); 
      } 
      listBox1.DrawMode = DrawMode.OwnerDrawFixed; // 属性里设置 

     } 

     private void listBox1_DrawItem(object sender, DrawItemEventArgs e) 
     { 
      e.DrawBackground(); 
      e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), new Font(listBox1.Items[e.Index].ToString(), listBox1.Font.Size), Brushes.Black, e.Bounds); 
      //e.DrawFocusRectangle(); 
     } 
    } 
} 

Sample Output

1

Para añadir a la solución de Mindaugas Mozūras, que tenía un problema donde mi e.Bounds no era lo suficientemente grande y el texto se queden cortadas. Para resolver este problema (gracias a una publicación here), anula el evento OnMeasureItem y cambia su DrawMode a DrawMode.OwnerDrawVariable.

En diseñador:

listBox.DrawMode = DrawMode.OwnerDrawVariable; 

En manejador:

void listBox_MeasureItem(object sender, MeasureItemEventArgs e) 
{ 
    e.ItemHeight = 18; 
} 

solucionado mis problemas de tener la altura cortó texto.

+1

Solo agregué para decir que no tenía este problema en mi computadora de desarrollo, pero lo obtuve cuando lo enviaba a un servidor de terminal. Por lo tanto, recomiendo aplicar esto a su código para mayor seguridad. – Jonas

0

Un ejemplo más genérico que utiliza remitente y respeta el color de primer plano (si el elemento está seleccionado, por ejemplo, o el usuario usa otro conjunto de colores, donde el color de primer plano no es realmente legible) y la fuente ListBox actual:

private void listBoxDrawItem (object sender, DrawItemEventArgs e) 
    { 
     Font f = e.Font; 
     if (e.Index == 1) //TODO: Your condition to make text bold 
      f = new Font(e.Font, FontStyle.Bold); 
     e.DrawBackground(); 
     e.Graphics.DrawString(((ListBox)(sender)).Items[e.Index].ToString(), f, new SolidBrush(e.ForeColor), e.Bounds); 
     e.DrawFocusRectangle(); 
    } 

Debe tener DrawMode establecido en OwnerDrawFixed (por ejemplo, en el diseñador).

0

hace el artículo seleccionado en negrita

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

     ListBox1.Items.AddRange(new Object[] { "me", "myself", "bob"}); 

     // set the draw mode to fixed 
     ListBox1.DrawMode = DrawMode.OwnerDrawFixed; 
    } 

    private void ListBox1_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     // draw the background 
     e.DrawBackground(); 

     // get the font 
     Font font = new Font(e.Font, (e.State & DrawItemState.Selected) == DrawItemState.Selected ? FontStyle.Bold : FontStyle.Regular); 

     // draw the text 
     e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(), font, new SolidBrush(ListBox1.ForeColor), e.Bounds); 

     e.DrawFocusRectangle(); 
    } 
} 
Cuestiones relacionadas