Share your opinion
WIDGETS GAMBAS
GAMBAS 3 SOFTWARES
TUTORIALS
Library
|
BUTTON - Simplify code
Use ACTION and GROUP to simplify code
Hi Today for this tutorial, we will create one simple application with 5 buttons. When i click on one button, other buttons are disabled. One Reset button return all buttons enabled. A) create interface. Let create 4 buttons like this : and one button named Button5 with property Text is : Reset who will enable all 4 buttons. B) Let write code Naturaly, we will write this code bellow : Public Sub Button1_Click() Button1.Enabled = True Button2.Enabled = False Button3.Enabled = False Button4.Enabled = False End Public Sub Button2_Click() Button1.Enabled = False Button2.Enabled = True Button3.Enabled = False Button4.Enabled = False End Public Sub Button3_Click() Button1.Enabled = False Button2.Enabled = False Button3.Enabled = True Button4.Enabled = False End Public Sub Button4_Click() Button1.Enabled = False Button2.Enabled = False Button3.Enabled = False Button4.Enabled = True End Public Sub Button5_Click() Button1.Enabled = True Button2.Enabled = True Button3.Enabled = True Button4.Enabled = True End C) Simplify code using Group property We could simplify code using group property. Group property are used to do group of widget, All event will be grouped in a same group. In GROUP property of every button write this text : toto . All these button with be in toto group. Now in Tag property of every button, write text 0,1,2,3 (button1.tag = 0 button2.tag = 1 etc...) Result is interesting, we will have only one event click like this : PUBLIC SUB toto_Click() SELECT CASE LAST.tag CASE 0 bouton0.Enabled = FALSE bouton1.Enabled = TRUE bouton2.Enabled = TRUE bouton3.Enabled = TRUE CASE 1 bouton0.Enabled = TRUE bouton1.Enabled = FALSE bouton2.Enabled = TRUE bouton3.Enabled = TRUE CASE 2 bouton0.Enabled = TRUE bouton1.Enabled = TRUE bouton2.Enabled = FALSE bouton3.Enabled = TRUE CASE 3 bouton0.Enabled = TRUE bouton1.Enabled = TRUE bouton2.Enabled = TRUE bouton3.Enabled = FALSE END SELECT END D) simplify code with 3 lines We could simplify more this code. We use only enabled property of every button. We could use ACTION property of every button. Write for every action property of every button this text : bouton. With group and action properties, we will write this simple code : PUBLIC SUB toto_Click() Action["bouton", ME].Enabled = TRUE LAST.Enabled = FALSE END Let explain this code : public Sub toto_click When i click on widget in toto group Action["bouton", ME].Enabled = TRUE property enabled will be true on this widget with action named bouton LAST.Enabled = FALSE and last widget clicked will be disabled. In conclusion, we simplify all this code with only 3 lines. It's this gambas power. Thanks for your attention. Creation date : 08/04/2022 @ 11:31 Reactions to this article
| QUICK MENU
SEARCH
LANGUAGE
COMPONENTS
Other
LANGUAGE
Where, who?
VISITS
visitors visitors online ARCHIVES GAMBAS 1/2
|