C# Aligning 2 Strings via a listbox

In my listbox, I would like to display the following

String 1(left aligned - default)                 int value(right aligned)

Is there a way to do this, or can there only be 1 alignment in a listbox?


If you are Using in Winforms, you can use this code.

You can set the DrawMode property of the ListBox to DrawMode.OwnerDrawFixed

ListBox listBox = new ListBox();
listBox.DrawMode = DrawMode.OwnerDrawFixed;
listBox.DrawItem += new DrawItemEventHandler(listBox_DrawItem);

void listBox_DrawItem(object sender, DrawItemEventArgs e)
    ListBox list = (ListBox)sender;
    if (e.Index > -1)
        object item = list.Items[e.Index];
        Brush brush = new SolidBrush(e.ForeColor);
        SizeF size = e.Graphics.MeasureString(item.ToString(), e.Font);
        e.Graphics.DrawString(item.ToString(), e.Font, brush, e.Bounds.Left + (e.Bounds.Width / 2 - size.Width / 2), e.Bounds.Top + (e.Bounds.Height / 2 - size.Height / 2)); 

If you use WPF. This should work in XAML:

            <ColumnDefinition Width="Auto" MinWidth="100"/>
            <ColumnDefinition Width="Auto" MinWidth="100"/>
        <TextBlock Text="{Binding 1}" HorizontalAlignment="Left"/>
        <TextBlock Text="{Binding value}" HorizontalAlignment="Right" Grid.Column="1"/>

Out of memory! You may have to adjust it a bit.

By the way: Variable names like "1" and "value" belong to the category of bad naming ;-) Hope it was just for the post and not for your real working code.

Next time you can also make clear in which technology you want it to display (WinForms, WPF, ASP.net, etc.).


Need Your Help

using corona sdk(lua), After moving an object in grid is equal to last object present in the grid, why?

arrays lua 2d corona

I am newbie to corona,started learning with a sample app, Here I am facing a problem, need some suggestion, I am dragging an object to an empty position where it is 2 places after or before or up o...

Trying to apply a custom scroll bar

javascript jquery html css interface

I'm trying to apply a similar type of custom scroll bar into my website but I seem to be having some trouble.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.