ListBox with DrawMode = OwnerDrawFixed missing Horizontal ScrollBar/and an error

M

Mark Smith

hi

i use an ownerdraw method for coloring some items in the list different
then the others.

code:

private void ListBoxDrawItem(object sender, DrawItemEventArgs e)
{
ListBox lst = (ListBox)sender;
if (e.Index == -1)
{
return;
}
// Set the DrawMode property to draw fixed sized items.
lst.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;

// Determine the color of the brush to draw each item based
on the index of the item to draw.
myBrush = Brushes.Black;

String str = lst.Items[e.Index].ToString();
if (str.Contains(" READY "))
{
myBrush = Brushes.Red;
}

// Draw the current item text based on the current Font and
the custom brush settings.
e.Graphics.DrawString(lst.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();
}

1.

i add various items to the list and when " READY " is in the string the
item should be shown in red

but when items are too long i dont get a horizontal scrollbar, how can
this be fixed ? when i disable ownerdrawfixed i immediately get a
scrollbar

2.

i tend to add items to this list from another process via sendmessage
and LB_ADDSTRING

this works fine when i use DrawMode = Normal. but when i use
OwnerDrawFixed i get an exception on "String str =
lst.Items[e.Index].ToString();". the listbox doesnt contain any item at
this moment. whats the difference between listbox.items.add and
LB_ADDSTRING ? it seems that the item is not added to the list yet when
the drawitem method is called (using api)

i'd appreciate any answers. i am sitting on those problems quite long
already

greetings
Mark
 

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