Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Create Buttons from Class ?

Status
Not open for further replies.

paispatin

Technical User
Oct 21, 2003
62
A1
Dear all,

I Already create a object form with 2 button with this code :

VIEW.CODE 1
Code:
frmMyForm = CREATEOBJECT('Form')  && Create a Form
frmMyForm.Closable = .t.  

frmMyForm.AddObject('button1','cmdMyCmndBtn')  && Add Command button
frmMyForm.button1.top=40
frmMyForm.button1.caption="Button 1"
frmMyForm.button1.Visible =.T.  

frmMyForm.AddObject('button2','cmdMyCmndBtn')  && Add Command button
frmMyForm.button2.top=80
frmMyForm.button2.caption="Button 2"
frmMyForm.button2.Visible =.T.  


frmMyForm.SHOW  && Display the form
READ EVENTS  && Start event processing


DEFINE CLASS cmdMyCmndBtn AS CommandButton  && Create Command button
   Left = 125  
   Top = 10  
   Height = 25  

   PROCEDURE Click
      MESSAGEBOX("Button Pressed")  
ENDDEFINE

As we see at that code (VIEW.CODE 1), it will create as many button as this we write this code (VIEW.CODE 2):

VIEW.CODE 2
Code:
frmMyForm.AddObject('button2','cmdMyCmndBtn')  && Add Command button
frmMyForm.button2.top=80
frmMyForm.button2.caption="Button 2"
frmMyForm.button2.Visible =.T.

My Problem is, How to make a button with that code (VIEW.CODE 1) which is create by looping process, such as Do While (maybe something like that).
So I can make 2 or 3 or 5 button with looping proccess, and when we press each button the messagebox will be show :
"Button 1" if I press button 1
"Button 2" if I press button 2
"Button 2" if I press button 2

Is that posible?
Please help me to answer this problem.

Thank you in advance.
 
How about:
Code:
For ln = 1 to 99
  lcName = "button" + alltrim(str(ln))
  frmMyForm.AddObject(lcName,'cmdMyCmndBtn')
  frmMyForm.&lcName.Top = 40 * ln
  frmMyForm.&lcName.Visible = .T.
next ln
The only awkward bit is using macro substitution to get "button1" etc into the object name.

Apologies for any syntax errors - don't have VFP here to test it.

Geoff Franklin
 
There is another (perhaps) valuable baseclass to you:
CommandGroup. Simply set the Buttoncount property...

Bye, Olaf.
 
Thank's for the reply.

Alvechurchdata, I already try your code, but it always said :
Code:
frmMyForm.&lcName.Top = 40 * ln
Button1Top is not found.

OlafDoschke, I still can not use the commandgroup for what I need.

Maybe someone could give me more help.
Thank you in advance.
 
Button1Top is not found

That's because there is a mistake in the code above that was posted. With Macro Substitution VFP will see any period (.) directly after it as a continuation...so in order to use it as was suggested above you would need to double-up the periods like this:
Code:
  frmMyForm.&lcName[b][COLOR=red]..[/color][/b]Top = 40 * ln
  frmMyForm.&lcName[b][COLOR=red]..[/color][/b]Visible = .T.

boyd.gif

 
Thank you guys, it work with double dot, it become like this :
Code:
frmMyForm = CREATEOBJECT('Form')  && Create a Form
frmMyForm.Closable = .t.  

For ln = 1 to 3
  lcName = "button" + alltrim(str(ln))
  frmMyForm.AddObject(lcName,'cmdMyCmndBtn')
  frmMyForm.&lcName..Top = 40 * ln
  frmMyForm.&lcName..Visible = .T.
next ln


frmMyForm.SHOW  && Display the form
READ EVENTS  && Start event processing


DEFINE CLASS cmdMyCmndBtn AS CommandButton  && Create Command button
   Left = 125  
   Top = 10  
   Height = 25  

   PROCEDURE Click
      MESSAGEBOX("Button Pressed")  
ENDDEFINE

That code wil give how much button that we need, but how to make if each button pressed, the messsagebox will be show the same button name count, so :

pressing button 1 will show "Button 1 Pressed"
pressing button 2 will show "Button 2 Pressed"
...
...
....
pressing button 6 will show "Button 6 Pressed"


Thank you in advance

 
There are many ways to do this. I'm not exactly sure what it is you want to accomplish as there's not much use to a button saying "I was pressed" in a real world application. I mean given your code above you could just modify the messagebox call to read...

Messagebox(this.name + " was pressed")

...it would give you essentially what you want. Or you could use ExecScript() and the tag property of the button, while you are adding the buttons put a string like this:

frmMyForm.&lcName..tag = "Messagebox([Button " + transform(ln) + " Pressed])"

...and then in the click event of your class you could put:

ExecScript(this.tag)

...these are just a couple of examples, as I say there are many ways to acheive this functionality. However, what this is really for would greatly influence which tact is best suited to meet your needs.


boyd.gif

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top