GuiToggle() label assignment

Tags: #<Tag:0x00007f2d7a821a28>



Is there an example for the GuiToggle() anywhere?

A toggle function normally has two labels, this function has only one label…are we expected to handle the label assignment separately?

happy coding,


Hello Herman,

If you create the button using for example:

GuiButton( "Start", 1, 10, 25, 75, 25, 1 );

… you can change it’s label (when the button is clicked) using:

GuiSetText( "Stop", 1 );

This GuiSetText() line can be even at the end of the code and it will change the GuiButton() label even if this line was at the beginning of the code. It will happen in one run - without the need of any additional refreshes. For this reason it’s very useful.

For example take a look at my Buttons Hunter Game code. One “Start/Stop” button which lets the user toggle between six different stages of the game and has six different labels:

I 'm not using GuiToggle() now, because in AmiBroker 6.25 GuiGetCheck() might sometimes return inappropriate value (-1) which causes problems. It will be probably solved by Tomasz in the next release. To avoid this problem, we can use Static Variable to store the state (0,1) or states (i.e. 0,1,2,3,0) of the button. For example:

id = GuiGetEvent( 0, 0 ); event = GuiGetEvent( 0, 1 );
MyChartID = GetChartID();
ToggleButton1 = Nz( StaticVarGet( "ToggleButton1" + MyChartID ), 0 );

GuiButton( "On/Off", 1, 5, 30, 50, 25, 1 );

if( id == 1 && event == 1 )  // If ToggleButton1 is clicked
    if( ToggleButton1 ) // if ToggleButton1 was on, now it will be off
        GuiSetText( "Off", 1 );
        StaticVarSet( "ToggleButton1" + MyChartID, 0, Persistency = 0 );
        Say( "Switch Off", 1 );
        GuiSetText( "On", 1 );
        StaticVarSet( "ToggleButton1" + MyChartID, 1, Persistency = 0 );
        Say( "Switch On", 1 );
else // If ToggleButton1 is not clicked
    if( ToggleButton1 ) GuiSetText( "On", 1 );
    else GuiSetText( "Off", 1 );

If Persistency is set to 1, the state of the button, will be saved and retrieved after the restart of AmiBroker. The code above doesn’t need any extra refreshes to work properly. In the next releases, using static variables in such simple cases probably won’t be necessary.



Thank you Milosz,

I didn’t know that the text could be assigned anywhere in the code; that should solve the problem nicely!

Re the Toggle solution, I had something similar in mind and will use that.

Thanks for your helpful hints!