By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,896 Members | 1,709 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,896 IT Pros & Developers. It's quick & easy.

Problem with listbox horizontal scrollbar

P: n/a
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

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

Jul 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.