Randomly generating buttons and assigning them a name

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
d.m.holdawayGA2553
Posts: 81
Joined: Mon Jan 09, 2012 4:48 pm

Randomly generating buttons and assigning them a name

Post by d.m.holdawayGA2553 » Mon Feb 27, 2012 12:01 am

Hello,

i was wondering if anyone could help me?

i have searched the forum for an answer but could not find one, i also tried google.

Using code i am trying to generate random buttons and then assigning the buttons either A,B or C

so far i have

Code: Select all


on createrandombutton

   put random(3) into tLetter
   if tLetter = 1 then put A into pLetter
   else 
      if tLetter = 2 then put B into pLetter
      else 
         if tLetter = 3 then put C into pLetter
      end if 
   end if 

create button ("button&pLetter) in group "buttongroup"

end createrandombutton 

am i going in the right direction, or is this just wrong?

so if anyone could shed some light on this issue or put me in the right direction that would be amazing

thanks for taking the time to view this post.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Randomly generating buttons and assigning them a name

Post by sturgis » Mon Feb 27, 2012 12:11 am

Don't know if its a typo or not but you will get an error on the create line because you don't have matching quotes around your string.

change this:
create button ("button&pLetter) in group "buttongroup"

to this:

create button ("button" & pLetter) in group "buttongroup"

In addition, there is an easier way to do the randomizing.

put any item of "A,B,C" into pLetter -- will grab a random letter from the string

So your handler can be simplified into

Code: Select all

on createrandombutton
   put any item of "A,B,C" into pLetter
   create button ("button" & pLetter) in group "buttongroup"
end createrandombutton 

d.m.holdawayGA2553
Posts: 81
Joined: Mon Jan 09, 2012 4:48 pm

Re: Randomly generating buttons and assigning them a name

Post by d.m.holdawayGA2553 » Mon Feb 27, 2012 12:55 am

yes it was a typo,

thanks you for the advice and code sample.

Post Reply