please give me just a clue.....

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

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

please give me just a clue.....

Post by chris25 » Thu Nov 14, 2013 8:42 pm

just as I thought I had nailed all the problems, one remains unsolved. It concerns line 4 (put...of grp etc etc), a right royal mess, I simply do not have the knowledge yet to know How to get to the start of a solution....and the easy way out is to make a calculate button and shove the code that I know into that...but I do not want the easy way out...i want a user friendly flow- something different.

User has inserted pixel dimensions (yes this is version 2.0 completely re-vamped due to extensive growth in wisdom) :? and when the user clicks a radio button (a nested group within a group) he immediately gets his magical answer.

Please no answers...a winsy clue just to annoy me further would be nice...thankyou

All tabs are on one card
All this code is in the group "printpapersize" which is nested within the larger group that holds the text entry field and the result field at the bottom of the card.

global gPixels, gSize
on mouseUp
put fld "pixelLongestSide" into gPixels
put of grp "printPaperSize" into gSize
if gSize = button "A2" then
put trunc (gPixels / 23.4) into fld "ppiResult"
end if
if gSize = button "A3+" then
put trunc (gPixels / 19.1) into fld "ppiResult"
end if
if gSize = button "A3" then
put trunc (gPixels / 16.5) into fld "ppiResult"
end if
if gSize = button "A4" then
put trunc (gPixels / 11.7) into fld "ppiResult"
end if
if gSize = button "A5" then
put trunc (gPixels / 8.3) into fld "ppiResult"
end if
if gSize = button "A6" then
put trunc (gPixels / 5.8) into fld "ppiResult"
end if
end mouseUp

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: please give me just a clue.....

Post by Mark » Thu Nov 14, 2013 9:24 pm

Hi,

I don't understand what your question is, so I'll just comment on your script.

The line

Code: Select all

put of grp "printPaperSize" into gSize
is missing a factor. Between "put" and "of" you need to write a container, e.g. a field or property. Propably you want to have the name of the hilitedButton of grp "printPaperSize".

Code: Select all

put the hilitedButtonName of grp "printPaperSize" into gSize
Also, you can greatly optimise your script:

Code: Select all

global gPixels, gSize

on mouseUp
   put fld "pixelLongestSide" into gPixels
   put the hilitedButtonName of grp "printPaperSize" into gSize
   if gSize = button "A2" then
      put trunc (gPixels / 23.4) into fld "ppiResult"
   else if gSize = button "A3+" then
      put trunc (gPixels / 19.1) into fld "ppiResult"
   else if gSize = button "A3" then
      put trunc (gPixels / 16.5) into fld "ppiResult"
   else if gSize = button "A4" then
      put trunc (gPixels / 11.7) into fld "ppiResult"
   else if gSize = button "A5" then
      put trunc (gPixels / 8.3) into fld "ppiResult"
   else if gSize = button "A6" then
      put trunc (gPixels / 5.8) into fld "ppiResult"
   end if
end mouseUp
Instead of an if-then-else control structure, you might also use a switch control structure. I'm not sure that you really need global variables. You might as well define local variables inside the handler.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: please give me just a clue.....

Post by chris25 » Thu Nov 14, 2013 9:41 pm

Ok, I had put so many things into the script where you thought that I had missed something out, that when I quoted it I left it blank Mark. I can not get this to work. The user inserts the dimensions in the top text field and then highlights one of the radio buttons. Then the answer should appear in the bottom text field, no calculate button. This is the problem.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: please give me just a clue.....

Post by Mark » Thu Nov 14, 2013 9:52 pm

Hi,

You could put a rawKeyUp handler in the field and check that the field contains a valid value. If it does contain a valid number, you can continue the script to do the calculations and display the answer in the bottom field. You do the same in a mouseUp handler in the radio buttons. Whenever the field doesn't contain a valid number, you could show a small warning icon next to the field.

Does this help?

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: please give me just a clue.....

Post by chris25 » Thu Nov 14, 2013 10:06 pm

I'll get right on it

No sorry, this is too advanced for me, I looked of course in the dictionary...enough said? I really do not make head or tail of the example or the description in the dictionary. Valid value? All numbers typed in will be ok, they will be valid because the user can type in anything, the maths is perfect..it is just that there is no returning value in the answer text box, it stays empty, the variables are being filled, the gPixels gets the correct number, but the gSize is getting some very strange numbers, so my guess is that the line here: put the hilitedbutton of grp "printPaperSize" into gSize is wrong, I tried the hilitedbuttonname but that does not apply, it did not work obviously. Would there be a problem because this group is nested within a group that contains the first text number input, whereas the buttons are of course in their own group, nested within because there is no other way to do it.

Sorry
chris

The hilitedbutton line is putting in the number of the button into the variable, when I originally started this it was putting in the name of the group into the variable, so I know it is a matter of simple starightforward syntax that is to blame here, trouble is I have no idea what the syntax should be and I can not really look it up otherwise I would have done that a couple of hours ago.

I should add that actually I first used this: Put the target of grp etc etc into the variable, many example scripts used this but it obviously gave the group name, so I am thinking now, use the target, but how to set the target to mean the 'size' as in dimensions? I have the name of each button set up like this so I am at a loss now.

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

Re: please give me just a clue.....

Post by dunbarx » Thu Nov 14, 2013 11:10 pm

Chris.

You are zooming along, all right.

You do use a lot of groups. Are these necessary? They may place a layer of uncertainty between you and the objects of interest. I see you fixed the two typos in your text code example when you placed it into the code field. So not an issue, right?

Anyway, one thing at a time. Have you gotten a result in your field "ppiResult" yet? If not, a good detective tool is to place a breakpoint at each line WITHIN each "if" section (at the "put" line). This will only catch if that "if" condition is met. When you do that, does the variable gPixels contain a useful value? If so, it sure looks like the math gizmo ought to place a number into the field.

What Mark alluded to, and I assume you have not run into this yet, is that if the user places a string that is not a number your script will throw an error. He suggested that you validate the user input. But this for just a little later.

So, do you get a value in gPixels?

Craig

EDIT:

I actually made all the button, fields and groups and named them as you did. If I have a number in fld "pixelLongestSide", then I get a value in fld "ppiResult". No problems. I do think you could do this much more simply BUT THAT IS NOT IMPORTANT HERE. It may be better that you are making this ugly. It will teach you much when you go back and rethink the structure, and then rewrite the code. Keep at it...

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: please give me just a clue.....

Post by Mark » Thu Nov 14, 2013 11:21 pm

Hi Chris,

So, let's concentrate on the line

Code: Select all

 put the hilitedbutton of grp "printPaperSize" into gSize
for a second. What exactly is the name of the group containing the radio buttons? What happens if you replace "printPaperSize" with that name? Does that make the line execute correctly?

You might or might not get an error in another line now. If you do get an error, please post the exact, entire text of the error message here. You can select and copy the text of the error and paste it here.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: please give me just a clue.....

Post by chris25 » Thu Nov 14, 2013 11:44 pm

Hallo gentlemen, well after sometime experimenting I got it working. I changed the line to TARGET as you see. I then got a strange thing in the variable as you can see down at the bottom, then I remembered some time ago something simon said about this - that the variable was seeing the whole button and not the name, so I added a quote structure to the code to get rid of that word button from the varibale so that it would only see the paper size. You can now flick through the paper size and immediately get different results without having to add the extra 'calculate' button. Yes I have 4 groups because that is the only way I could do a tabbed structure across the top of the screen. One group has that nested group, the buttons. Each tab brings up a different window and has to be grouped I believe, at least that is the only way I found how to do it.

The only issue I have now is that although I have a preopencard to empty all variables and fields upon first opening the app, when the user goes form tab to tab, he comes back and the fields are still populated with old data, I tried a preopencontrol, and I actually searched for a preopen group and a preopentab, but none exists, so I do not know how to empty these fields between navigation of tabs.

Kind regards
chris

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

Re: please give me just a clue.....

Post by dunbarx » Thu Nov 14, 2013 11:44 pm

Mark.

I assume he (properly) made a group of radio buttons and named that group "printPaperSize".

No? Chris?

I just think, like you do, that a rethinking of the whole structure is in order, and it can be much smaller and simpler. But as I said, and actually Chris is game for the effort, the learning will be better with the clunker.

Craig

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: please give me just a clue.....

Post by Mark » Thu Nov 14, 2013 11:58 pm

Hi Craig,

I don't know what you mean with going from tab to tab. A "tab" is part of a tab button. Maybe you mean cards. If you mean cards, then you can use the preOpenCard handler instead of the preOpenStack handler to make fields and variables empty.

You really shouldn´t write

Code: Select all

if something then
  else
    if something else then
    else
      if something else then...
Please change this into

Code: Select all

if something then
else if something else then
else if something else then...
end if
Look at the script that I posted earlier to see what I mean exactly.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: please give me just a clue.....

Post by chris25 » Fri Nov 15, 2013 12:14 am

Hallo, yes I do mean from tab to tab. I have constructed four tabs and they are all on one card, all groups and one nested group. I do not mind actually to post the whole stack for your thoroughly ravenous critique, though bare in mind this is only the end week 4 for me, and no coding experience before this, so I know that this could be done perhaps more compact, even more efficiently, but this is version 2.0 you should see version 1.0!

No, the stack opens everything empty on a preopencard handler no problem, but once the user fills in data and then moves on, then comes back, the data fields are still populated, preopen...didgeries do not seem to do the trick at this juncture. We are talking moving between tabbed groups on a single card, or tabbed windows if you like, not sure if you have a proper term for this?

Mark, as for the way I have written the code, tis actually helps me to see things visually better, I used to do HTML like this and CSS (though that was years ago), and I know that many people prefer this sort of manner of writing, for me it is actually easier to visualize the structure of the code and what is fitting with what.

kindest regards
chris

I am also struggling with this:

on closefield
if fld "desired resolution" <> 360,300,240,180 then
answer "360, 300, 240 or 180"
end if
end closefield

I tried ...if me, instead of field. I also tried using the ..value is not a...but this did not work, I don't know what else to look up except that "on mouseleave" does actually work but very inconsistently indeed in this instance

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

Re: please give me just a clue.....

Post by Klaus » Fri Nov 15, 2013 12:39 am

Hi Chris,

doesn't this look a bit cleaner and yet understandable? 8)

Code: Select all

global gPixels, gSize
on mouseUp
  put fld "pixelLongestSide" into gPixels

  ## This will give you the short name of the currently hilited radiobutton.
  ## No need to mess with QUOTE & whatever & QUOTE! ;-)
  put the hilitedbuttonname of grp "printPaperSize" into gSize
  
  switch gSize
    case "A2"
      put 23.4 into tValue
      break
    case "A3+"
      put 19.1 into tValue
      break
    case "A3"
      put 16.5 into tValue
      break
    case "A4"
      put 11.7 into tValue
      break
    case "A2"
      put 23.4 into tValue
      break
    case "A5"
      put 8.3 into tValue
      break
    case "A6"
      put 5 into tValue
      break
  end switch

  ## Save typing by first filling a VARIABLE with the correct value and THEN write once:
  put trunc (gPixels/tValue) into fld "ppiResult"
end mouseUp
Best

Klaus

P.S.
I never liked more than 1 nested "if... then" clauses! :)

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: please give me just a clue.....

Post by Mark » Fri Nov 15, 2013 12:42 am

Chris,

It seems that you think that in LiveCode

Code: Select all

if something then
  else
    if
is the same as

Code: Select all

if something then
else if
but really, it is not. The first variant is slow and inefficient and you shouldn't do that if you are going to share your software with anyone at a later stage. Of course, there are situations where this would be the right thing to do, but not in this case.

You're not using tabs. You are using buttons with mouseDown or mouseUp handlers that show and hide groups. If you had a tab menu button, then you would be using tabs.

Probably you should extend the mouseUp or mouseDown handlers to clear any fields. For example, if you have a mouseUp handler,

Code: Select all

on mouseUp
  // put your original script here
  // to show and hide your groups
  put empty into gPixels
  put empty into gSize
  put empty into fld "ppiResult"
  // add any more lines here if you need to
  // clear any additional fields or variables
end mouseUp
If you always want to make the same fields and variables empty, you could do this:

Code: Select all

on mouseUp
  // put your original script here
  // to show and hide your groups
  put empty into gPixels
  put empty into gSize
  put empty into fld "ppiResult"
  clearAllFieldsAndVars
end mouseUp
and put this at card level:

Code: Select all

on clearAllFieldsAndVars
  put empty into gPixels
  put empty into gSize
  put empty into fld "ppiResult"
  // add any more lines here if you need to
  // clear any additional fields or variables
end clearAllFieldsAndVars
Edit:

If you want to make sure that the user can only enter a particular number then you can do this:

Code: Select all

on closefield
  if the text of me is not among the items of "360,300,240,180" then
    answer error "360, 300, 240 or 180"
    set the text of me to 360
    // the next line is optional
    // don't use it if it causes problems
    send "select the text of me" to me in 0 millisecs
  end if
end closefield
This is something for which you need to read the documentation and perhaps an occasional book ;-) Reading while learning to code will help you a lot.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: please give me just a clue.....

Post by chris25 » Fri Nov 15, 2013 1:14 am

Ok, Mark and Klaus, firstly Klaus, yes you are right, it does look better. In fact I did read that using the switch is better than if and else especially when you have several like have. I intended to make another copy of this stack and strip the code and use a switch method. Thankyou for your help.

Mark, Another save as, and I will experiment with your code as well, actually you have both given me what I need, variety and a better way to accomplish things, so back to the drawing board to see how things work and compare three different methods. To think I only asked for a clue.....thankyou. Yes, to read or not to read...I was waiting for my zachtgeld to accumulate....

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: please give me just a clue.....

Post by Mark » Fri Nov 15, 2013 1:38 am

Hi,

I thought of the switch, but didn't want to use it, because too many new things might be confusing. The switch is even faster than if-then-else.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Post Reply