Aligner à droite le texte d'une ListBox

Il faut passer la propriété DrawMode de la ListBox à OwnerDrawFixed et ajouter le code suivant dans l'événement OnDraw :

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();
    e.DrawFocusRectangle();
 
    ListBox lb = sender as ListBox;    
 
    // Calcule les dimensions du texte
    SizeF stringSize = new SizeF();
    stringSize = e.Graphics.MeasureString(lb.Items[e.Index].ToString(), e.Font);
 
    e.Graphics.DrawString(
        lb.Items[e.Index].ToString(), 
        e.Font, 
        new SolidBrush(e.ForeColor), 
        new PointF(e.Bounds.Right - stringSize.Width, e.Bounds.Y)
    );            
}

Si ça peut faire gagner 5 minutes à quelqu'un...

Poster un nouveau commentaire

Le contenu de ce champ ne sera pas montré publiquement. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Les adresses de pages web et de messagerie électronique sont transformées en liens automatiquement.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Les lignes et les paragraphes vont à la ligne automatiquement.
  • You can enable syntax highlighting of source code with the following tags: <code>, <php>.

Plus d'informations sur les options de formatage

CAPTCHA
La vérification ne tient pas compte des minuscules ou des majuscules.
Image CAPTCHA
Enter the characters shown in the image.