G
Guest
I have a ListBox, and have changed the DrawMode to DrawMode.OwnerDrawFixed.
I'm using the ListBox.DrawItem event to do some custom coloring in my ListBox
object.
My item strings contain tabs, but after drawing the items myself, the tabs
are not intepreted. Currently I'm just using the sample code given in the
MSDN docs for the event handler:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
private void listBox1_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
// Set the DrawMode property to draw fixed sized items.
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
// Draw the background of the ListBox control for each item.
e.DrawBackground();
// Define the default color of the brush as black.
Brush myBrush = Brushes.Black;
int mod = e.Index % 3;
// Determine the color of the brush to draw each item based on the index
of the item to draw.
switch (mod)
{
case 0:
myBrush = Brushes.Red;
break;
case 1:
myBrush = Brushes.Orange;
break;
case 2:
myBrush = Brushes.Purple;
break;
}
// Draw the current item text based on the current Font and the custom
brush settings.
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font,
myBrush,e.Bounds,StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected
item.
e.DrawFocusRectangle();
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
As I mentioned, my items contain tabs, and when I intercept this event, my
strings look like this:
"Name\taddress\tetc..."
Also, in case it matters, I'm setting my own tab stops in the list box:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
private void SetDeviceListTabStops()
{
// A tab-stop location of 4 equates to the width of one average
character for the specified font
int[] listBoxTabs = new int[] { 20, 75, 110, 160 };
// Send an LB_SETTABSTOPS message to the ListBox control.
int result = SendMessage(this.listBox1.Handle.ToInt32(),
LB_SETTABSTOPS, listBoxTabs.Length, ref listBoxTabs[0]);
// Refresh the List Box control.
this.listBox1.Refresh();
}
I'm using the ListBox.DrawItem event to do some custom coloring in my ListBox
object.
My item strings contain tabs, but after drawing the items myself, the tabs
are not intepreted. Currently I'm just using the sample code given in the
MSDN docs for the event handler:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
private void listBox1_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
// Set the DrawMode property to draw fixed sized items.
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
// Draw the background of the ListBox control for each item.
e.DrawBackground();
// Define the default color of the brush as black.
Brush myBrush = Brushes.Black;
int mod = e.Index % 3;
// Determine the color of the brush to draw each item based on the index
of the item to draw.
switch (mod)
{
case 0:
myBrush = Brushes.Red;
break;
case 1:
myBrush = Brushes.Orange;
break;
case 2:
myBrush = Brushes.Purple;
break;
}
// Draw the current item text based on the current Font and the custom
brush settings.
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font,
myBrush,e.Bounds,StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected
item.
e.DrawFocusRectangle();
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
As I mentioned, my items contain tabs, and when I intercept this event, my
strings look like this:
"Name\taddress\tetc..."
Also, in case it matters, I'm setting my own tab stops in the list box:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
private void SetDeviceListTabStops()
{
// A tab-stop location of 4 equates to the width of one average
character for the specified font
int[] listBoxTabs = new int[] { 20, 75, 110, 160 };
// Send an LB_SETTABSTOPS message to the ListBox control.
int result = SendMessage(this.listBox1.Handle.ToInt32(),
LB_SETTABSTOPS, listBoxTabs.Length, ref listBoxTabs[0]);
// Refresh the List Box control.
this.listBox1.Refresh();
}