How to increment a counter when a GUIButton is clicked

I would like to increase the counter "test" by 1 each time I press the GUIButton, but the code below doesn't seem to work. Please help me fix this one, thank you in advance.

// control IDs 
idMyButton = 2; 

rc = GuiButton( "MyButton", idMyButton, 10, 20, 100, 20, notifyClicked ); 
test = 0;
code = GuiGetEvent(0, 0); 

if (code == idMyButton){
		test ++;
}
_TRACE(NumToStr(test, 1.0));

For example, when I clicked the button once, I got 1, if I click the button a second time, I want to see the counter test increase to 2, click one more time, I want to see the counter increase to 3, etc. Thanks.

1 Like
/// GUI, increment counter if clicked
/// @link https://forum.amibroker.com/t/how-to-increment-a-counter-when-a-guibutton-is-clicked/23840
#pragma enable_static_decl(my_counter)
static _test;

// control IDs 
idMyButton = 2; 
idMyButton2 = idMyButton+1;

button1 = GuiButton( "MyButton", idMyButton, 10, 20, 100, 20, notifyClicked ); 
button2 = GuiButton( "Reset", idMyButton2, 10, 40, 100, 20, notifyClicked ); 

code = GuiGetEvent(0, 0);
event = GuiGetEvent( 0, 1 );
is_clicked = event == notifyClicked;

reset = code == idMyButton2 AND is_clicked;
if ( reset ) _test = 0;

if (code == idMyButton AND is_clicked)
	_test++;

Title = "click counter: " + _test;
_TRACEF("%g", _test);
4 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.