Page 1 of 1

Saber que buttons se selecciono

PostPosted: Tue Nov 09, 2010 7:44 pm
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.

Re: Saber que buttons se selecciono

PostPosted: Wed Nov 10, 2010 1:22 am
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 }

Re: Saber que buttons se selecciono

PostPosted: Wed Nov 10, 2010 1:54 am
by hag
so what does it mean?

Re: Saber que buttons se selecciono

PostPosted: Wed Nov 10, 2010 1:38 pm
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

Re: Saber que buttons se selecciono

PostPosted: Thu Nov 11, 2010 2:22 pm
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.

Re: Saber que buttons se selecciono

PostPosted: Thu Nov 11, 2010 5:36 pm
by James Bott
Prueba esto - probado y funciona.

Saludos,
James

Code: Select all  Expand view
#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