ComboBox

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Jason1234
Posts: 42
Joined: Sat Jun 18, 2011 9:20 am

ComboBox

Post by Jason1234 » Wed Jun 29, 2011 12:32 pm

Hi...

I am new to livecode so this may seem a simple question but I cannot find the answer in the forum or the help files.

I have a Combo box that I have pre entered a list of selections.

I am using

Code: Select all

   on menuPick itemPicked
  switch itemPicked
  case "Living Room"
    Put 0.80  into  roomsens
    break
  case "Lounge"
    Put 0.80 into roomsens
    break
case "Other"
     Ask "What is the room name"
     put return & it after button "theItem"
         put 0.80 into roomsens
    break
end switch
call MouseUp of button "Calc" of first card
end menuPick
This adds the entered text that was entered in the Ask box to the end of the ComboBox. My question is how would I set the field to display this new last item?

Any help appreciated.

Thanks

Jason
Windows / MAC / IOS / Android - Deployment
Build 5.5.4 / & Community Version 6.1

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: ComboBox

Post by Klaus » Wed Jun 29, 2011 1:26 pm

Hi Jasobm

hmmm, there is no field specified in your script, but anyway...
on menuPick itemPicked

Code: Select all

  switch itemPicked
  case "Living Room"
    Put 0.80  into  roomsens
    break
  case "Lounge"
    Put 0.80 into roomsens
    break
  case "Other"
     Ask "What is the room name"
     
     ## Store th ANSWER for later use:
     put it into tAnswer
     put return & tAnswer after button "theItem"
     put 0.80 into roomsens

    ## Now do whatever you want with tAnswer
    put tAnswer into fld "whatever field you mean..."
    break
  end switch

  ## Put QUOTES around the message to CALL or SEND!
  send "mouseup" to btn "Calc" of cd 1
end menuPick
I wopuld recommend to "outsource" the "mouseup" script of your button "Calc",
so there is no need to CALL or SEND!

Put the "calc" handlers into the stackscript and just use it wherever you need it.


Best

Klaus

Jason1234
Posts: 42
Joined: Sat Jun 18, 2011 9:20 am

Re: ComboBox

Post by Jason1234 » Wed Jun 29, 2011 11:12 pm

Thanks again...

I am learning.... just very slowly...

I appreciate the help. Thanks.
Last edited by Jason1234 on Thu Jun 30, 2011 2:03 am, edited 1 time in total.
Windows / MAC / IOS / Android - Deployment
Build 5.5.4 / & Community Version 6.1

Jason1234
Posts: 42
Joined: Sat Jun 18, 2011 9:20 am

Re: ComboBox

Post by Jason1234 » Thu Jun 30, 2011 2:03 am

This is odd... possibly because the ComboBox is a button not a field..?

Code: Select all

  case "Other"
           Ask "What is the room name"
          ## Store th ANSWER for later use:
     put it into tAnswer
     put return & tAnswer after button "theItem"
     put 0.80 into roomsens

    ## Now do whatever you want with tAnswer
    put tAnswer into fld "theItem"
    break
The error is:
button "theItem": execution error at line 18 (Chunk: no such object) near "theItem", char 17

If I change fld to btn the contents of the ComboBox are replaced with tAnswer the original pre-loaded choices are removed. The name of the Combo is theItem

Thanks
Windows / MAC / IOS / Android - Deployment
Build 5.5.4 / & Community Version 6.1

SparkOut
Posts: 2946
Joined: Sun Sep 23, 2007 4:58 pm

Re: ComboBox

Post by SparkOut » Thu Jun 30, 2011 2:45 am

Ah, I think you want to add a new line to the combo box and then show the newly chosen line as the currently selected item,right?
You probably need to look at setting the menuHistory and/or setting the label of the combobox button.
My take on your script:

Code: Select all

on menuPick pItemName
   switch pItemName
      case "Living Room"
         put 0.80 into roomsens
         break
      case "Lounge"
         Put 0.80 into roomsens
         break
      case "Other"
         ask "What is the room name?"
         -- check if the user cancelled and only proceed if they didn't
         if the result is not "Cancel" then
            -- store the value they submitted
            put it into tAnswer
            -- stick it at the end of the list, overwriting the line for "Other"
            put  tAnswer into line -1 of me
            -- sort the new list with the extra item, but without having to worry about "Other" appearing in the middle of the list
            sort me ascending text
            -- add a new line at the end for the "Other" option to be the last item again
            put cr & "Other" after me
            -- you can set the label of the comboBox to show the currently selected item being the one newly inserted
            set the label of me to tAnswer
            -- or you can find out which line the newly inserted item is on and 
            -- set the menuHistory to the corresponding line number by uncommenting
            -- the relevant line below.
            -- if you set the menuHistory then another menuPick message is generated
            -- which may or may not be desired. In this case it would not have any different effect, but
            -- you may want to prevent a duplicate calculation, for instance
            -- set the menuHistory of me to lineOffset(tAnswer,the text of me)
            -- do anything else you need to
            put 0.80 into roomsens
         end if
         break
   end switch
   -- fire off the calculation handler, as Klaus said, change the on mouseUp script of your "Calc" button to 
   -- run the same command we do here:
   doTheCalc
   -- I picked an arbitrary name for the command handler, of course!
end menuPick

Jason1234
Posts: 42
Joined: Sat Jun 18, 2011 9:20 am

Re: ComboBox

Post by Jason1234 » Thu Jun 30, 2011 6:17 am

Thank you for your help.

With the comments it really helps.

I presume I have got this right (it seems to work) in the Stack Script..

I have now entered:

Code: Select all

Global roomsens
command TheCalc
      if the text of fld "Length" is a number or the text of fld "Width" is a number or the text of fld "Height" is a number then
         put fld "Length" *  fld "Width" * fld "Height" * roomsens into fld "Result"
      End if
end TheCalc
Regards

Jason
Windows / MAC / IOS / Android - Deployment
Build 5.5.4 / & Community Version 6.1

SparkOut
Posts: 2946
Joined: Sun Sep 23, 2007 4:58 pm

Re: ComboBox

Post by SparkOut » Thu Jun 30, 2011 10:50 am

If it's working then it seems like you have got the right idea. You will, no doubt be issuing the command "TheCalc" from the button script or your menupick script.

A word of advice though, while you have done nothing wrong*, you should get into the habit of using field operations as little as possible. Get a value from a field into a variable and do any validation and processing with the variable, and then write back into a field with the results only once. Field operations are exponentially longer than operations on variables, as there are so many more overheads in validation, message generation, etc.
*except for the "or" instead of "and" in the test for number

I would change your script to

Code: Select all

Global roomsens
command TheCalc
    put field "Length" into tLength
    put field "Width" into tWidth
    put field "Height" into tHeight
    if tLength is a number and tWidth is a number and tHeight is a number then
        put tLength * tWidth * tHeight * roomsens into field "Result"
    end if
end TheCalc
It won't make a lot of difference here, but it can, and probably will bite you at some point when you have a big repeat loop that updates values.

I was also under the impression that you are doing something in particular with the menuPick handler beyond simply setting the label. If that is the whole handler, and every case puts 0.80 into roomsens then you needn't make a case statement for each one. Just have a case statement for "Other" and a default for the rest, if they are all the same. You can also put multiple case statements together and have the same action for any one of them in the manner:

Code: Select all

switch pItemName
    case "Living Room"
    case "Lounge"
    case "Billiard Room"
    case "Study"
        put "Colonel Mustard" into theSuspect
        break
    case "Kitchen"
    case "Conservatory"
        put "Miss Scarlet" into theSuspect
        break
    default
        put "Professor Plum" into theSuspect
end switch
When any of the cases "Living Room", "Lounge", "Billiard Room" or "Study" are matched, then the action to put Colonel Mustard into theSuspect will be taken, and so on.

Jason1234
Posts: 42
Joined: Sat Jun 18, 2011 9:20 am

Re: ComboBox

Post by Jason1234 » Thu Jun 30, 2011 11:50 am

That helps me even though the code I have shown is only a small snippet where the values entered are different. However your advice makes perfect sense and I will do as you suggest where the values are the same.

I also understand the point about working with variable not the field so will do that too.

I need now to understand saving the variables and reloading them so as all the values are loaded and check boxes, combo's etc are all set correctly all from loading a set of saved variables from external file or database....

I somehow think that will be fun... oh and drawing a simple floor plan that can set to scale.... deep end and jumping... come to mind.

Thanks to all for the help. :wink:
Windows / MAC / IOS / Android - Deployment
Build 5.5.4 / & Community Version 6.1

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: ComboBox

Post by BarrySumpter » Thu Jun 30, 2011 9:36 pm

Field operations are exponentially longer than operations on variables, as there are so many more overheads in validation, message generation, etc.
Thats good to know.
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

Post Reply