Drawing tabs in ListBox.DrawItem event

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();
}
 
G

Guest

Answered my own question...

By default, when drawing a string using the

e.Graphics.DrawString()

method, the tab stops aren't set! By specifying the tab stops in the format
string as a parameter to this method, I was able to get the tabs to display
properly:

StringFormat format = StringFormat.GenericDefault;
format.SetTabStops(0f, new float[] { 20f, 75f, 110f, 160f });

// Draw the current item text based on the current Font and the custom
brush settings.
e.Graphics.DrawString(lstDevices.Items[e.Index].ToString(), e.Font,
myBrush, e.Bounds, format);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top