Reputation: 257
As an attempt to color text in ListBox i found this guide C# : change listbox items color (i am using windows forms application on visual studio 2012). The code is working but the problem is that i want to use the textbox in a Right to Left mode, but when i change it in the ListBox settings it does not work, so i assume that it needs to be changed in the code somehow, this is what i need your help for. Thank you very much! Alon
Upvotes: 0
Views: 1229
Reputation: 98
Your y position is 0, so everytime you insert a message it's on the left side. To put it on the right side you have recalculate the postion.
Look at the following example.
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
MyListBoxItem item = listBox1.Items[e.Index] as MyListBoxItem; // Get the current item and cast it to MyListBoxItem
if (item != null)
{
e.Graphics.DrawString( // Draw the appropriate text in the ListBox
item.Message, // The message linked to the item
listBox1.Font, // Take the font from the listbox
new SolidBrush(item.ItemColor), // Set the color
width - 4, // X pixel coordinate
e.Index * listBox1.ItemHeight,
new StringFormat(StringFormatFlags.DirectionRightToLeft)); // Y pixel coordinate. Multiply the index by the ItemHeight defined in the listbox.
}
else
{
// The item isn't a MyListBoxItem, do something about it
}
}
Upvotes: 1
Reputation: 1058
A list box is natively left-justified and you cannot change this in the UIR editor. You can apply the appropriate justification elements while creating the string to pass to the list box: see the online help for Item Label parameter of the InsertListItem
function. All escape codes are to be applied to every line that you insert into the list box; there is no way to apply a default formatting style to the control.
Upvotes: 0