OwnerDraw listbox does not redraw correctly when resized

J

Joshua Flanagan

I have a listbox set to OwnerDrawFixed. I anchored the listbox to the
left and right side of the form, so the listbox resizes when the form
resizes. I also set the StringFormat to use EllipsisWord trimming when
the contents of a listbox item is too long to fit.
I have added 2 long strings to the listbox, so that by default, they are
trimmed (the entire string is not visible in the listbox).

The problem is, when I resize the form (and hence the listbox), the item
that is NOT selected does not redraw correctly. It looks like it gets
partially erased, but there are various artifacts left behind.

To reproduce:

1) Create a Windows Forms project, with a single form

2) Add a listbox and size it to be the height of the form, but only
about half the width of the form. Set the following properties on the
listbox:
Name = listBox1
DrawMode = OwnerDrawFixed
Anchor = Top, Bottom, Left, Right

3) Add the following to the Form.Load event:
// This should only be 2 lines, don't wrap within the quoted text
// Any bit of long text will do.
listBox1.Items.Add("The quick brown fox jumped over the lazy dog.");
listBox1.Items.Add("Take me out to the ballgame. Take me out to the
game. Buy me some peanuts and cracker-jack.");

3) Add the following to the listBox1.DrawItem event:

e.DrawBackground();
e.DrawFocusRectangle();
if (e.Index < 0)
return;

SolidBrush brush = new SolidBrush(e.ForeColor);
string caption = listBox1.Items[e.Index] as string;
if (caption != null)
{
using (StringFormat format = new StringFormat(StringFormatFlags.NoWrap))
{
format.Trimming = StringTrimming.EllipsisWord;
RectangleF stringArea = new RectangleF(e.Bounds.X, e.Bounds.Y,
e.Bounds.Width, e.Bounds.Height);

e.Graphics.DrawString(caption, e.Font, brush, stringArea, format);
}
}
brush.Dispose();

4) Run the application. Select the first item in the listbox (there
should be at least 2. Resize the form so that the text in the listbox
is cut off (you should see the ellipse at the end). Now resize the form
to enlarge it. The item that is not selected will not redraw correctly.
Select the other item in the listbox and repeat. The unselected item
still does not redraw correctly.

I haven't seen this problem posted anywhere else. Am I missing
something simple? Any help would be appreciated.

Thanks

Joshua Flanagan
http://flimflan.com/blog
 
R

Rhett Gong [MSFT]

Hi Joshua,
I can have the problem reproed in my side. From my research, this problem
is caused by drawing the string to the incorrect rectangle. Seems the
non-activated item keeps the previous rect and use it to draw string.

Currently, we could workaround this problem by calling the overloaded
DrawString methods such as:
public void DrawString(string, Font, Brush, PointF, StringFormat);
public void DrawString(string, Font, Brush, float, float, StringFormat);
Or we could handle the resize event to validate the listbox again to make
the listbox corrently drawn.

Thanks for your reporting! I will forward this problem to the proper team.

Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
R

Rhett Gong [MSFT]

:) Yes, it is somewhat ugly.

I have forwarded this problem to proper team. If I get anything new, I will
update you here. But, as you know, I can not guarantee that. :(

Also, if you feel there are other things we can do, please don't hesitate
to let me know. Thanks.


Have a nice day!
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 

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