Combobox resizing..

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
tasdvl9
Posts: 94
Joined: Fri Dec 06, 2013 3:55 am

Combobox resizing..

Post by tasdvl9 » Thu Sep 24, 2015 3:46 pm

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Combobox resizing..

Post by dunbarx » Thu Sep 24, 2015 5:40 pm

Hi.
I imagine I would need to scan all of the options in the list and find the one with the longest string.
You don't have to. Use the "formattedWidth" instead.

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Combobox resizing..

Post by bn » Thu Sep 24, 2015 5:56 pm

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.

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
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

tasdvl9
Posts: 94
Joined: Fri Dec 06, 2013 3:55 am

Re: Combobox resizing..

Post by tasdvl9 » Thu Sep 24, 2015 6:39 pm

Thank you all for the replies.

I'm going to try both suggestions.

Much appreciated!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Combobox resizing..

Post by dunbarx » Thu Sep 24, 2015 8:23 pm

Bernd.

Code: Select all

on mouseDown
 set the width of me to the formattedWidth of me + 40
end mouseDown
:D

Craig

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Combobox resizing..

Post by bn » Thu Sep 24, 2015 8:30 pm

Bernd.

CODE: SELECT ALL
on mouseDown
set the width of me to the formattedWidth of me + 40
end mouseDown
:D

Craig
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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Combobox resizing..

Post by dunbarx » Thu Sep 24, 2015 10:02 pm

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. :oops:

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
Craig

Post Reply