I am having exactly the same problem, made worse by the fact that =
MeasureString introduces a very significant slowdown.
Can anyone help?

Best wishes,
Alex.

markusszil (anonymous@discussions.microsoft.com) wrote in =
microsoft.public.dotnet.general:
> Hello all,
>=20
> I have a fairly simple listbox into which I add, insert, and remove =
strings.
>=20
> It has the following property settings:
> HorizontalScrollbar - true=20
> HorizontalExtent - 0
> ScrollAlwaysVisible - False
>=20
> The problem I am having is that the horizontal scrollbar range doesn't =
work right. If I add a string, the first time the sting width exceeds =
the control width the bar appears, but it's off by a few pixels so the =
last character is truncated. If I add longer items, it doesn't update so =
I can't see the end of them. If I insert items, it never updates. Delete =
only seems to work if the entire horizontal scrollbar should disappear.
>=20
> I ended up writing code (at end of this post) which goes through all =
the strings and uses the Graphics.MeasureString method, then sets =
HorizontalExtent to the maximum result. It works, but seems very =
cumbersome - and I can't find an event which fires when the =
Items-collection changes which could simply call this method. Instead, I =
need to go through my code and add calls to this at any point the =
collection might change.
>=20
> Is this a known issue with listboxes or am I doing something dumb. I =
can't believe proper scrollbar operation requires added code.
>=20
> Any tips appreciated.
> Markus
>=20
> Here is the code I had to add:
>=20
> private void UpdateHorzScroll()=20
> {
> Graphics g=3DGraphics.FromHwnd(Handle);
> int wid=3D0;
> foreach (object obj in MyListBox.Items)=20
> {
> int swid=3D(int)(g.MeasureString(obj as string,Font).Width);
> if (swid > wid) wid=3Dswid;
> }
> MyListBox.HorizontalExtent=3Dwid;
> }

--=20
Address email to user "response" at domain "alexoren" with suffix "com"

Re: Problem with listbox horizontal scrollbar by v-jetan

v-jetan
Mon Sep 19 22:07:35 CDT 2005

Hi Alex,

Yes, I can reproduce out this problem. However the correct solution for
this issue is using the MeasureString method to calculate HorizontalExtent
value.

Actually, if you view "ListBox.HorizontalExtent Property" in MSDN, you will
see that the sample for ListBox.HorizontalExtent Property also uses
MeasureString method to do this. Sample code listed below:

private void DisplayHScroll()
{
// Make no partial items are displayed vertically.
listBox1.IntegralHeight = true;

// Add items that are wide to the ListBox.
for (int x = 0; x < 10; x++)
{
listBox1.Items.Add("Item " + x.ToString() + " is a very large value
that requires scroll bars");
}

// Display a horizontal scroll bar.
listBox1.HorizontalScrollbar = true;

// Create a Graphics object to use when determining the size of the
largest item in the ListBox.
Graphics g = listBox1.CreateGraphics();

// Determine the size for HorizontalExtent using the MeasureString
method using the last item in the list.
int hzSize = (int) g.MeasureString(listBox1.Items[listBox1.Items.Count
-1].ToString(),listBox1.Font).Width;
// Set the HorizontalExtent property.
listBox1.HorizontalExtent = hzSize;
}

If your concern is performance side, I think we can cache the longest item
string length in a private field, then each time we adding a new item, just
compare the new item string count with the cached value. So we only need to
call MeasureString and set HorizontalExtent when the new item is larger
than cached value.

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.