2 Replies Latest reply on Dec 11, 2017 11:12 PM by kduffy

    Passing Button Name  to function

    alanR

      Hi ,

       

      I have several button  with names that have the same name as elements in AF system explorer. Im trying to make each button call the same function . So for example i have a Button called Room_A , on click it calls a function called TestButtonToggle_Click(). In this function i would like to pass in the button name  which would return a value from AF with an element of the same name.

      How would i go about doing this?

       

      What i have tried below;

       

      My code;

      Public Sub TestButtonToggle_Click(ByVal lvarX As Long, ByVal lvarY As Long)

       

       

          Dim pbButtons As SelectedButton

          Dim pbButton As Button

          Dim Cell As String

       

          Set pbButtons = ThisDisplay.SelectedButtons

          If pbButtons.Count = 0 Then

              Exit Sub

          End If

       

          For Each pbButton In pbButtons

              If InStr(buttonName.name, "Room") Then

                  Room = Mid(buttonName.name, 6, Len(buttonName.name))

                    'Pass number to function below

                  ToggleRoomState (Room)

                  Exit Sub

              End If

          Next pbSymbol

      End Sub

        • Re: Passing Button Name  to function
          messingerj

          If you are going to call the same function from multiple Button Click() event handlers, you should not bind that function to any control. So your target function TestButtonToggle_Click() should be a completely standalone function - currently it's signature looks like an event handler for a button control. Once you have it properly defined, you simply add a string parameter to your method signature. When you call this function from one of your button event handlers, you pass the button name as the parameter to the function.

           

          Looking at your code however, you can probably simplify it even more and just call your ToggleRoomState() function directly from the button click event handlers and pass in the appropriate name value. There's no reason that I can see from the limited information available that you can't just directly call the ToggleRoomState() function from each button's event handler and pass in a hard-coded string value appropriate for that button.

          2 of 2 people found this helpful
          • Re: Passing Button Name  to function
            kduffy

            Hi Alan,

             

            Did John's answer get you on the right track? I would agree on both accounts with him; it sounds like you can directly call the ToggleRoomState function with a hard coded button name from each button click event, and if it's actually more complex than this example, you can implement a custom function that each of the button click events pass their button names to.

             

            Or was your concern about keeping the button names in sync with the AF elements if they were to ever change, therefore not hard coding anything?

             

            Kelsey