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
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