Change width of a combo box based on the Value

musclecarlover07

Registered User.
Local time
Today, 12:13
Joined
May 4, 2012
Messages
236
So how would I go about changing the width of a combo box to the length of the value in vba?
 
Right, I got that. What I want is for the combo box to rezize based on the value of the combo box. So it the value is Apple then it resizes itself so that it fits Apple. Then if I change it to something like New Jersey it resizes to fit New Jersey.
 
Why would you need to do that anyway? If you set the List Width property to Auto it will size the drop down list accordingly.

You can't easily determine the length (i.e. measurement) of a string by just counting how many characters it returns.
 
Its OCD kicking in. I have 12 items in the dropdown box and the shortest selection is 10 characters then the longest selection is 20+ characters. When I select something that is smaller then there is all this empty space and its annoying to see. So if I could fix that it would be awesome.
 
Its OCD kicking in. I have 12 items in the dropdown box and the shortest selection is 10 characters then the longest selection is 20+ characters. When I select something that is smaller then there is all this empty space and its annoying to see. So if I could fix that it would be awesome.
For you it is annoying but for other users I'm sure they won't mind. In fact most well-built applications don't implement this behaviour.
 
Ok. Well If it is possible I still would like to have it. It attention to detail. I mean a simple i don't know or it can't be done would have sufficed. I have actually seen several apps that implement this behavior.
 
Like I already indicated it's not a simple task to determine the width of text.
Each character is not the same width and each font type have different widths per character. It's only monospace fonts (e.g. Courier) that have similarly spaced fonts. So determining the correct width of a string is determined by the kind of font you use.

You need to use an API called DrawText to determine the width of a string. And I think you need another one to set/get the font types.

The applications that implement this behaviour do it during runtime to determine the widest possible config for the control, not something that's done on-the-fly.
 
>>> A simple I don't know or it can't be done would have sufficed<<<

I can't speak for vb of course but i get the impression he was trying to say nicely that there's no way that "you" would be able to do it...
 
JHB, I had forgotten about the Wizhook object. Just shows it's been a while since I touched Access.
I was even thinking along the lines of the TextWidth and TextHeight properties of a report but that doesn't support multi-line text.

Now from memory I don't think TwipsFromFont supports multi-line text but it won't matter in this case.
 
Thanks JHB. That works really nicely.
Well I agree. I think JHB has done really well to come up with what you wanted. But I hate it. I particularly dislike that if you choose a short word by mistake it can be difficult to then find the correct selection when you can only see the first two or three letters. As a user, I think I would much prefer a static combo box.
 
I particularly dislike that if you choose a short word by mistake it can be difficult to then find the correct selection when you can only see the first two or three letters. As a user, I think I would much prefer a static combo box.

Ok to fix that I changed the list width to be the same size as the longest choice. This way no matter what I choose the list width is always the same size.

I love this because I like the design of a project. When I see a field with a bunch of empty space it bothers be and ruins the overall design. Everything has a specific place. Evey thing has to be the same distance from each other. Design is everything. The first thing to an app a db etc they see is the design. If it is appealing they will be more likely to use it. This is my belief and it hasn't let me down yet.
 
Ok to fix that I changed the list width to be the same size as the longest choice. This way no matter what I choose the list width is always the same size.

I love this because I like the design of a project. When I see a field with a bunch of empty space it bothers be and ruins the overall design. Everything has a specific place. Evey thing has to be the same distance from each other. Design is everything. The first thing to an app a db etc they see is the design. If it is appealing they will be more likely to use it. This is my belief and it hasn't let me down yet.
Well I suppose it's true when they say that, "Beauty is in the eye of the beholder" and "One mans meat is another mans poison". I hope you will forgive me if I decline any offer in the future of dinner and a blind date.:eek:
 

Users who are viewing this thread

Back
Top Bottom