Topic
6 replies Latest Post - ‏2013-02-18T15:39:14Z by SystemAdmin
SystemAdmin
SystemAdmin
3180 Posts
ACCEPTED ANSWER

Pinned topic How can I identify the button which fired the event ...

‏2013-02-15T12:01:57Z |

Hello

Actually I've the following code:
 

void CloseDB(DB dbMain) {
    release(dbMain);
}
 
void DoSomething(DBE dbeButtonX) {
        // btn_1 or btn_2?
        print "Click: "
}
 
 
void Demo() {
 
        DB dbMain = create("Title");
        
        // ...
        
        
        DBE btn_1 = button(dbMain, "Button-1", DoSomething);
        DBE btn_2 = button(dbMain, "Button-2", DoSomething);
 
        close(dbMain, false, CloseDB) 
 
        block(dbMain); 
        
        // ...
        
        destroy(dbMain); 
}
 
Demo();

 


There are two buttons which fires the same Event. The Eventhandler has the parameter "DBE dbeButtonX" and this should be either btn_1 or btn_2. My question is: Is it possible to identify the button which fires the event?

Thanks for every help

Best regards
Wolfgang

 

Updated on 2014-01-06T13:22:17Z at 2014-01-06T13:22:17Z by iron-man
  • MichaelGeorg
    MichaelGeorg
    53 Posts
    ACCEPTED ANSWER

    Re: How can I identify the button which fired the event ...

    ‏2013-02-15T13:28:10Z  in response to SystemAdmin

    Hello Wolfgang,

    if you declare the buttons globally you can go like this:
     

    DBE btn_1, btn_2
     
    void CloseDB(DB dbMain) {
        release(dbMain);
    }
     
    void DoSomething(DBE dbeButtonX) {
            // btn_1 or btn_2?
            if(dbeButtonX == btn_1) {
                    print "Click btn_1"
            }
            else {
                    print "Click: btn_2"
            }
    }
     
     
    void Demo() {
     
            DB dbMain = create("Title");
            
            // ...
            
            
            btn_1 = button(dbMain, "Button-1", DoSomething);
            btn_2 = button(dbMain, "Button-2", DoSomething);
     
            close(dbMain, false, CloseDB) 
     
            block(dbMain); 
            
            // ...
            
            destroy(dbMain); 
    }
     
    Demo();
    

     


    Best regards,
    Michael

     

    Updated on 2014-01-06T13:22:34Z at 2014-01-06T13:22:34Z by iron-man
    • llandale
      llandale
      2943 Posts
      ACCEPTED ANSWER

      Re: How can I identify the button which fired the event ...

      ‏2013-02-15T18:58:53Z  in response to MichaelGeorg
      Yes. This is another reason to routinely define your DB and DBE variables globally at the top.
      • SystemAdmin
        SystemAdmin
        3180 Posts
        ACCEPTED ANSWER

        Re: How can I identify the button which fired the event ...

        ‏2013-02-18T06:56:00Z  in response to llandale

        I've feared it.

        Otherwise there would be a solution for the following code
         

        void Demo() {
         
            DBE btn_1 = button(dbMain, "Button-1", DoSomething);
                DBE btn_2 = button(dbMain, "Button-2", DoSomething);
         
                void CloseDB(DB dbMain) {
                        release(dbMain);
                }
         
                void DoSomething(DBE dbeButtonX) {
                        // btn_1 or btn_2?
                        print "Click: "
                }
         
         
                DB dbMain = create("Title");
                
                // ...
                
                close(dbMain, false, CloseDB) 
         
                block(dbMain); 
                
                // ...
                
                destroy(dbMain); 
        }
         
        Demo();
        

         


        But the most important rule of dxl is: Make it as complicate as possible.

         

        Updated on 2014-01-06T13:22:56Z at 2014-01-06T13:22:56Z by iron-man
        • Mathias Mamsch
          Mathias Mamsch
          1938 Posts
          ACCEPTED ANSWER

          Re: How can I identify the button which fired the event ...

          ‏2013-02-18T10:41:09Z  in response to SystemAdmin

          Well I think it is not too much to do something like this, if you insist on having local dialogs:
           

          void Demo() {
              void DoSomething(string sButton) {
                          print "Click: " sButton "\n"
                  }
           
                  void CloseDB(DB dbMain) {
                          release(dbMain); hide (dbMain)
                  } 
           
           
                  DB dbMain = create("Title");
           
                void DoSomethingButton1 (DBE) { DoSomething("Button-1") }
                void DoSomethingButton2 (DBE) { DoSomething("Button-2") }
           
                  DBE btn_1 = button(dbMain, "Button-1", DoSomethingButton1);
                  DBE btn_2 = button(dbMain, "Button-2", DoSomethingButton2);
                  
                  // ...
                  
                  close(dbMain, false, CloseDB) 
           
                  block(dbMain); 
                  
                  // ...
                  
                  destroy(dbMain); 
          }
           
          Demo();
          

           


          If you dynamically create buttons, then you need to resort to a global Skip list for storing your elements. But one global variable is not so bad.

          Regards, Mathias

           

           

           


          Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

           

           

          Updated on 2014-01-06T13:23:12Z at 2014-01-06T13:23:12Z by iron-man
        • llandale
          llandale
          2943 Posts
          ACCEPTED ANSWER

          Re: How can I identify the button which fired the event ...

          ‏2013-02-18T15:37:14Z  in response to SystemAdmin
          That is a different "feature". A nested function cannot access variables in the main function; "non local variable access" or something. Thus, nested functions don't serve much purpose and I (almost) never use them.
          • SystemAdmin
            SystemAdmin
            3180 Posts
            ACCEPTED ANSWER

            Re: How can I identify the button which fired the event ...

            ‏2013-02-18T15:39:14Z  in response to llandale
            It's a renamed solution ...