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"