Knowing which button was clicked

94 views
Skip to first unread message

valt...@valtecom.com.br

unread,
May 15, 2025, 6:53:03 AM5/15/25
to Harbour Minigui
Good morning my friends,
I created a window in which I create several buttons through a vector, which I will later pass to a DBF to be more flexible. I followed the example of the collaborator Jose Quintas, but I was unable to create a function that, when clicking on a button, knows which button was clicked to perform the necessary tasks. The names of the buttons are created automatically according to the records of the vector or DBF. When clicking on the button that was automatically named button001, I need to know, and when clicking on the button named button030, I need to know, so that the system can search the relevant DBFs for the data corresponding to each button.
A screenshot of the window is attached; each of the buttons was created automatically.
Waiting for your response.
Thank you.
Valteçom
Uberaba MG Brazil
mesas.jpg

C.J. Koot

unread,
May 15, 2025, 7:16:17 AM5/15/25
to minigu...@googlegroups.com

Hello Valtecom,

Maybe you can use 'This.Name' in the action function, like ONCLICK ButtonClick(This.Name) and in the function ButtonClick use the variable This.Name to detect which button is clicked.


Kind regards,

René Koot

Op 15-05-2025 om 12:53 schreef valt...@valtecom.com.br:
Uberaba MG Brazil --
Visit our website on https://www.hmgextended.com/ or https://www.hmgextended.org/
---
You received this message because you are subscribed to the Google Groups "Harbour Minigui" group.
To unsubscribe from this group and stop receiving emails from it, send an email to minigui-foru...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/minigui-forum/ab02e83e-adf7-4d55-bdc9-a0e10ec1d281n%40googlegroups.com.

Grigory Filatov

unread,
May 15, 2025, 7:39:00 AM5/15/25
to Harbour Minigui
Hello Valtecom,

> when clicking on a button, knows which button was clicked to perform the necessary tasks.

Please try the following example in the attachment.

I hope this gives you some idea.

Regards,
Grigory

четверг, 15 мая 2025 г. в 12:53:03 UTC+2, valt...@valtecom.com.br:
demo.prg

Daniel B

unread,
May 15, 2025, 2:38:33 PM5/15/25
to Harbour Minigui
Or something like:   ..... Action Yourfunc ( this.caption )

José M. C. Quintas

unread,
May 15, 2025, 3:46:47 PM5/15/25
to minigu...@googlegroups.com

There exists a detail about code blocks and variable:

if you create codeblock on this way:

FOR nCont = 1 TO 10

   bCode := { || DoClick( nCont ) }

NEXT

result will be 11 on each click.

It is because codeblock will use variable value on execution, and it will be 11

You need to isolate the value, or using a function to create a codeblock, or a FOR/EACH that does the same thing.

FOR nCont = 1 TO 10

   bCode := CreateCodeblock( nCont )

NEXT

...

FUNCTION CreateCodeblock( xValue ) // this xvalue have a unique value

LOCAL bCode

bCode := { || DoClick( xValue ) }

RETURN bCode


Another not common way is to use this feature on FOR/EACH, only to isolate the variable.

FOR nCont = 1 TO 10 // ncont change value

   FOR EACH xValue IN { nCont } // here xvalue will use ncont, but xvalue will have a unique value

      bCode := { || DoClick( xValue ) }

   NEXT

NEXT

Make a single test, on a single module, can be console.

Don't confuse STRING/CAPTION with codeblock

FOR nCont = 1 TO 10

   bCode := { || nCont }

NEXT

FOR nCont = 1 TO 10

   ? Eval( bCode )

NEXT

Test the 3 routines posted here, and you will see the difference.


José M. C. Quintas

--
Visit our website on https://www.hmgextended.com/ or https://www.hmgextended.org/
---
You received this message because you are subscribed to the Google Groups "Harbour Minigui" group.
To unsubscribe from this group and stop receiving emails from it, send an email to minigui-foru...@googlegroups.com.

José Quintas

unread,
May 15, 2025, 4:53:54 PM5/15/25
to Harbour Minigui
There exists another problem:
CH transforms the action on codeblock, another codeblock problem
Use SetProperty() to define codeblock.

1) FOR EACH WITH 2 elements
2) SetProperty() to define ACTION
3) function RowPos() to make all automatic and a clear code

Attached and same here, it is a small source

/*
test codeblock
*/

#include "minigui.ch"

// don't initialize values here
STATIC  nRowPos, nColPos

FUNCTION Main()

   LOCAL aList := {}, nCont, nMesa, cName, bCode

   nColPos := -1
   nRowPos := -1

   FOR nCont = 1 TO 35
      AAdd( aList, nCont )
   NEXT
DEFINE WINDOW ( "Form_1" ) ;
AT 0,0 ;
WIDTH 1000 ;
HEIGHT 500 ;
MAIN;
TITLE 'Teste de Mesa' ;
NOSIZE ;
NOMAXIMIZE

DEFINE MAIN MENU

        FOR nCont := 1 TO Len( aList )

           // two variables uniques
           FOR EACH nMesa, cName IN ;
              { nCont }, ;
              { "Mesa" + Chr(13) + Chr(10) + StrZero( nCont,2 ) }

           @ RowPos(), nColPos BUTTONEX ( cName ) ;
        CAPTION "Mesa Nº" + CRLF + StrZero( aList[ nCont ], 2 ) ;
        PICTURE "button.bmp" ;
        BACKCOLOR GREEN GRADIENTFILL { { 1, GREEN,GREEN } } ;
        WIDTH 120 ;
        HEIGHT 60

              bCode := { || MostraMesa( nMesa, cName ) }
              SetProperty( "FORM_1", cName, "ACTION", bCode )
            NEXT
        NEXT

   END WINDOW

   CENTER WINDOW ( "Form_1" )

   ACTIVATE WINDOW ( "Form_1" )

RETURN Nil

STATIC FUNCTION RowPos()

   IF nColPos == -1
      nRowPos := 5
      nColPos := 5
   ELSE
      nColPos += 125
      IF nColPos > GetProperty( "form_1", "width" ) - 200
         nRowPos += 65
         nColPos := 5
      ENDIF
   ENDIF

   RETURN nRowPos

STATIC FUNCTION MostraMesa( nMesa, cName )

   MsgInfo( "Mesa " + StrZero( nMesa, 2 ) + " selecionada" )
   SetProperty( "FORM_1", cName, "BACKCOLOR", RED )
   SetProperty( "FORM_1", cName, "GRADIENTFILL", { { 1, RED, RED } } )

RETURN Nil

imagem.png

José M. C. Quintas

José Quintas

unread,
May 15, 2025, 5:08:35 PM5/15/25
to Harbour Minigui

I see this after post:

           FOR EACH nMesa, cName IN ;
              { nCont }, ;
              { "Mesa" + Chr(13) + Chr(10) + StrZero( nCont,2 ) }

           @ RowPos(), nColPos BUTTONEX ( cName ) ;
        CAPTION "Mesa Nº" + CRLF + StrZero( aList[ nCont ], 2 ) ;

It is wrong, because I mix button name with caption, and values are not array count, but array value
It is needed to change about nMesa and cname
Testing sample this is not visible, because array 1=1, array2=2, arrayn=n

           FOR EACH nMesa, cName IN ;
              { aList[ nCont ] }, ; // here
              { "Mesa" + Chr(13) + Chr(10) + StrZero( aList[ nCont ],2 ) } // here


           @ RowPos(), nColPos BUTTONEX ( cName ) ;
        CAPTION "Mesa Nº" + CRLF + StrZero( nMesa, 2 ) ; // here

José M. C. Quintas

José Quintas

unread,
May 15, 2025, 5:28:05 PM5/15/25
to Harbour Minigui

It is incredible!!!!
There exists another problem caused by hmg extended CHs !!!!!!!
I do not see this before !!!

Solution was call RowPos() before create the button
              RowPos()
      @ nRowPos, nColPos BUTTONEX ( cName ) ;

See numbers on image before change, they are wrong

mesanum.png

And now after change on source code, using rowPos() before button.
hmgeok.png

But ok, all have solution.

/*
test codeblock
*/

#include "minigui.ch"

// don't initialize values here
STATIC  nRowPos, nColPos

FUNCTION Main()

   LOCAL aList := {}, nCont, nMesa, cName, bCode

   nColPos := -1
   nRowPos := -1

   FOR nCont = 1 TO 35
      AAdd( aList, nCont + 50 )

   NEXT
DEFINE WINDOW ( "Form_1" ) ;
AT 0,0 ;
WIDTH 1000 ;
HEIGHT 500 ;
MAIN;
TITLE 'Teste de Mesa' ;
NOSIZE ;
NOMAXIMIZE

DEFINE MAIN MENU

        FOR nCont := 1 TO Len( aList )

           // two variables uniques
           FOR EACH nMesa, cName IN ;
              { aList[ nCont ] }, ;
              { "Mesa" + StrZero( nCont,2 ) }

              RowPos()
           @ nRowPos, nColPos BUTTONEX ( cName ) ;

        CAPTION "Mesa Nº" + CRLF + StrZero( nMesa, 2 ) ;

José M. C. Quintas
hmgmesas.prg

José Quintas

unread,
May 15, 2025, 5:43:10 PM5/15/25
to Harbour Minigui
small correction in my information:
The position was wrong, not the numbers

About the issue:
CH gets the values from source code line, do not wait RowPos() calculate new position.
Moving RowPos() to previous line ok, values are ready to use.

José M. C. Quintas

José Quintas

unread,
May 15, 2025, 6:11:10 PM5/15/25
to Harbour Minigui

Attention about this:

// don't initialize values here
STATIC  nRowPos, nColPos

FUNCTION Main()

   LOCAL aList := {}, nCont, nMesa, cName, bCode

   nColPos := -1
   nRowPos := -1

Why not to initialize on STATIC ?
Because you need that they start on first line on each dialog execution.
If you initialize on STATIC, on next execution they will continue from last position, and not on first line.
It is all. (I think)

José M. C. Quintas
Reply all
Reply to author
Forward
0 new messages