Combobox resizing..
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Combobox resizing..
Hi All,
I'm looking for a way to resize a Combobox before an option in the list is selected.
For instance,
If my options are.
option 1
option 2
option menu1 menu2 menu3
I'd like the width of the combobox to be resized to the length of option menu1 menu2 menu3
before selecting that option.
I imagine I would need to scan all of the options in the list and find the one with the longest string.
Is there a simpler way?
Any thoughts?
Thank you.
I'm looking for a way to resize a Combobox before an option in the list is selected.
For instance,
If my options are.
option 1
option 2
option menu1 menu2 menu3
I'd like the width of the combobox to be resized to the length of option menu1 menu2 menu3
before selecting that option.
I imagine I would need to scan all of the options in the list and find the one with the longest string.
Is there a simpler way?
Any thoughts?
Thank you.
Re: Combobox resizing..
Hi.
After each invocation of the button, store that value in a custom property, and then set the width on "mouseDown". This would be robust, since whenever you might change the contents of the combo, the new max width would be updated in the custom property.
Craig Newman
You don't have to. Use the "formattedWidth" instead.I imagine I would need to scan all of the options in the list and find the one with the longest string.
After each invocation of the button, store that value in a custom property, and then set the width on "mouseDown". This would be robust, since whenever you might change the contents of the combo, the new max width would be updated in the custom property.
Craig Newman
Re: Combobox resizing..
Hi tas,
the only thing I could come up with is a bit involved, as you say measure the longest line and get the width of it.
Maybe someone else has a better idea, I don't use combo boxes often.
Of course you don't have to put this into a mouseDown handler of the combo box, you can incorporate that into your code wherever you like, just reference the combobox not as me but as a valid reference to it.
Kind regards
Bernd
the only thing I could come up with is a bit involved, as you say measure the longest line and get the width of it.
Maybe someone else has a better idea, I don't use combo boxes often.
Code: Select all
on mouseDown
put the text of me into tText
put 0 into tLength
put 1 into theLineNo
repeat with i = 1 to the number of lines of tText
if length(line i of tText) > tLength then
put length(line i of tText) into tLength
put i into theLineNo
end if
end repeat
put measureText(line theLineNo of tText,me) into tTextWidth
put the topLeft of me into tTopLeft
lock screen
set the width of me to tTextWidth + 40 -- + 40 to accomodate the right side of box
set the topLeft of me to tTopLeft
unlock screen
end mouseDown
Kind regards
Bernd
Re: Combobox resizing..
Thank you all for the replies.
I'm going to try both suggestions.
Much appreciated!
I'm going to try both suggestions.
Much appreciated!
Re: Combobox resizing..
Bernd.
Craig
Code: Select all
on mouseDown
set the width of me to the formattedWidth of me + 40
end mouseDown

Craig
Re: Combobox resizing..
Craig,Bernd.
CODE: SELECT ALL
on mouseDown
set the width of me to the formattedWidth of me + 40
end mouseDown
![]()
Craig
I tried that before and came up with the awful piece of code above.
Now tried it again on a Mac and it works if you select the longest item, not if a shorter item is selected. Same as before. What do you see?
Go figure.
Kind regards
Re: Combobox resizing..
Bernd.
I never selected anything. Shows the value of really testing, as opposed to just fooling around and sending cute emoticons. The one-liner works fine with a field, which is what I was playing with at the time. I put that handler into the field script.
BUT. If you have in the combo:
123
123
1234567890
And now, I see what you do. I thought that the formattedWidth would extract the length of the longest line and do its thing, just as it does in a field. Something in the combo box relies on the selectedLine, and it makes the one-liner useless.
Anyway, this seems to get close to what the OP wanted, though I bet it needs a little TLC:
Craig
I never selected anything. Shows the value of really testing, as opposed to just fooling around and sending cute emoticons. The one-liner works fine with a field, which is what I was playing with at the time. I put that handler into the field script.
BUT. If you have in the combo:
123
123
1234567890
And now, I see what you do. I thought that the formattedWidth would extract the length of the longest line and do its thing, just as it does in a field. Something in the combo box relies on the selectedLine, and it makes the one-liner useless.

Anyway, this seems to get close to what the OP wanted, though I bet it needs a little TLC:
Code: Select all
on mouseDown
get me
sort it by the length of each
set the width of me to measureText(the last line of it,the name of me) + 40
end mouseDown