Saber que buttons se selecciono

Post Reply
jgayoso
Posts: 170
Joined: Sat Aug 07, 2010 11:36 pm
Location: Chile

Saber que buttons se selecciono

Post by jgayoso »

Consulta:

Tengo una funsión con un dialogo con x buttons, todos retornan al modulo que llano esta función. Pero la ideas es que retorne un numero que indique que button se selecciono, los botones estan en un arreglo.

Function .....
.
.
DEFINE DIALOG oDlg FROM 15, 30 TO 17+len(tG)+4, 82 FONT oFnt_Texto
for nI:=1 to len(tG)
@nI-1,2 say tG[nI] Font oFnt_Texto
next nI
for nI:=1 to len(aButtons)
@ len(tG), nC BUTTON aButtons[nI] OF oDlg SIZE nLB*5,14 Font oFnt_Button ;
ACTION ( lExit := .T., oDlg:End() )
nC+=nLB
next nIB
ACTIVATE DIALOG oDlg VALID lExit
Return nSeleccion

No he dado con la solución, ¿ Alguna idea ? ¿ Como puedo retornar en la variable "nSeleccion" un valor distinto para cada button ?

Sin son 3 buttons y presiono el primer button retorne 1, 2 el segundo y 3 el tercero.

Se agradece cualquier ayuda.
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: Saber que buttons se selecciono

Post by James Bott »

@ len(tG), nC BUTTON aButtons[nI] OF oDlg SIZE nLB*5,14 Font oFnt_Button ;
ACTION ( nSelccion:= eval( makeBlock(nI) ), lExit := .T.,oDlg:End() )

...

function makeBlock( i )
return {| u | i }
hag
Posts: 598
Joined: Tue Apr 15, 2008 4:51 pm
Location: LOs Angeles, California
Contact:

Re: Saber que buttons se selecciono

Post by hag »

so what does it mean?
Thank you
Harvey
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: Saber que buttons se selecciono

Post by James Bott »

Harvey,

He wants to return a number depending on which button was pressed. The compilcation is that you cannot use i directly since it will always be the last value of the loop (in this case 3) becuase the loop has already completed when the dialog is displayed. So the trick is to put i in a codeblock and thus the value of i at the time it was put in the codeblock remains in the codeblock. This technique is called a "detached local."

Regards,
James
jgayoso
Posts: 170
Joined: Sat Aug 07, 2010 11:36 pm
Location: Chile

Re: Saber que buttons se selecciono

Post by jgayoso »

James, probe lo indicado pero sin son 2 buttons me responde siempre 3, independiente del buttons presionado.

Local nSeleccion:=1
.
.
.
DEFINE DIALOG oDlg FROM 15, 30 TO 17+len(tG)+4, 82 FONT oFnt_Texto
for nI:=1 to len(tG)
@ len(tG), nC BUTTON aButtons[nI] OF oDlg SIZE nLB*5,14 Font oFnt_Button ;
ACTION ( nSeleccion:=eval( makeBlock(nI) ), lExit := .T., oDlg:End() )
nC+=nLB
next nI

ACTIVATE DIALOG oDlg VALID lExit

Return nSeleccion

function makeBlock( i )
return {| u | i }

Quedo a la espera de alguna respuesta.
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: Saber que buttons se selecciono

Post by James Bott »

Prueba esto - probado y funciona.

Saludos,
James

Code: Select all

#include "fivewin.ch"

function main()
   msgInfo( Test() )
return nil


Function Test()
   Local nSeleccion:=1, nI:=0, tG:="123", lExit:=.f.
   Local aButtons:={}, aPrompts:={"1","2","3"},nLB:=2
   Local oFnt_Button, oDlg, nC:=4, aSeleccion:={1,2,3}, oBtn

   define font oFnt_Button name "arial"

   DEFINE DIALOG oDlg FROM 15, 30 TO 17+len(tG)+4, 82 //FONT oFnt_Texto

      for nI:=1 to len(tG)

         @ len(tG), nC BUTTON oBtn prompt aPrompts[nI] OF oDlg SIZE nLB*5,14 Font oFnt_Button
         nC+=nLB

         aadd(aButtons,oBtn)
         aButtons[nI]:Cargo:= nI
         aButtons[nI]:bAction:= {|self| nSeleccion:= ::cargo, lExit := .t., oDlg:end() }

      next nI

   ACTIVATE DIALOG oDlg VALID lExit

Return  nSeleccion
Post Reply