Hello all
I have a fairly simple listbox into which I add, insert, and remove strings
It has the following property settings
HorizontalScrollbar - true
HorizontalExtent -
ScrollAlwaysVisible - Fals
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
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
Is this a known issue with listboxes or am I doing something dumb. I can't believe proper scrollbar operation requires added code
Any tips appreciated
Marku
Here is the code I had to add
private void UpdateHorzScroll()
Graphics g=Graphics.FromHwnd(Handle)
int wid=0
foreach (object obj in MyListBox.Items)
int swid=(int)(g.MeasureString(obj as string,Font).Width)
if (swid > wid) wid=swid
MyListBox.HorizontalExtent=wid