Dinamic CheckBox

Dinamic CheckBox

Postby eduardo » Mon Jun 20, 2011 7:47 pm

Good afternoon
i'm tryng develop a program where the number of checkbox is variable
i got it make appear on the monitor
but, on the moment of allocation, It does'nt allocate on the variable due
my code:

Code: Select all  Expand view

function filtraPedido()

local oDlg4, x:=1,ou:=""  //pra por os 'or' no filtro
     
local oTipoProd:={},        ;
      arrayTProd:={},       ;
      consulta:=""

local bConfirma,bCancela

TPROD->(dbgotop())
do while TPROD->(!eof())
 aadd(arrayTProd,TPROD->TProd)
 aadd(oTipoProd,.t.)
 TPROD->(dbskip())
 x++
enddo
aadd(arrayTProd,TPROD->TProd)
aadd(oTipoProd,.f.)
x++

define dialog oDlg4 from 2,58 to 2.8*x+3.2,128 //18,73 to 31,118
x:=1
oDlg4:SetFont(oFntDlg)

@ 0.2,2 say "Tipo do Produto:"

do while(x<len(arrayTProd))

 @ 1.5*x,1.5 checkbox oTipoProd prompt arrayTProd[x] of oDlg4// ;

  x++

 enddo

@ 1.14*x,2  button bConfirma prompt "&Confirmar" size 39,9 action( oDlg4:end(1)) of oDlg4

@ 1.14*x,12 button bCancela prompt "C&ancelar" size 39,9 action(oDlg4:end()) of oDlg4

activate dialog oDlg4


if oDlg4:nResult=1

 x:=1
 select PED
 do while(x<len(arrayTProd))
  if(oTipoProd[x])
   consulta:=consulta+ou+"(alltrim(PED->TipoProd) == '"+alltrim(arrayTProd[x])+"')"
   ou:=" .or. "
  endif
  x++
 enddo
 msginfo(consulta)
 set filter to &consulta

else
 PED->(dbclearfilter())
endif
return NIL
 

thanks a lot for the atetion
eduardo
 
Posts: 6
Joined: Mon Oct 04, 2010 6:06 pm

Re: Dinamic CheckBox

Postby James Bott » Mon Jun 20, 2011 11:38 pm

Eduardo,


I see two issues. First, you can't assign mutiple checkbox objects to the same variable oTipoProd. But, assuming this was to hold the checkbox control object, I don't think you need these anyway.

What you are missing is a varible to hold the values of the checkboxes (perhaps that is what you were using oTipoProd for?). This variable is a logical and MUST be an array since you may have multiple values, so lets call it aTipoProd.

The other problem is that the dialog is activated after the DO WHILE loop is finished and at that point x is fixed at the last value it was after the loop was completed. If instead you put x into a codeblock then all the codeblocks generated while the loop is executing still will return the value of x when the codeblock was created. So you are encapsulating the var x into a codeblock. These are called "detached locals."

Code: Select all  Expand view
do while(x<len(arrayTProd))

 @ 1.5*x,1.5 checkbox oTipoProd[makeBlock(x)];
            prompt arrayTProd[ makeBlock(x) ] of oDlg4

  x++

 enddo


// Make a detached local codeblock
function makeBlock( i )
return {| u | i }
 


One more point. Generally, using a variable number of checkboxes is not a good design. First you are limiting the app to the available screen size. Second it is not a good user design to have screens that change their shape. Users get used to a dialog's layout and it is confusing and frustrating when it is constantly changing on them.

Perhaps a better design would be a browse with two columns, one for the checkbox and one for the description. There may be other designs, depending on what these values are.

Regards,
James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 110 guests