Announcement

Collapse
No announcement yet.

Related Toggle Buttons - Alternative Code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Related Toggle Buttons - Alternative Code

    Hi,

    Im on a project that contains many Toggle Buttons, and are arranged in groups , so each Toggle Button is related to other buttons in the same group Like Radio Buttons.

    I want the Radio buttons Functionality but with Toggle Buttons Due to Design issues, i made an example and im looking if there is another way to do it better that my way.

    Click image for larger version

Name:	Capture.JPG
Views:	96
Size:	16.0 KB
ID:	300077

    Regards
    Attached Files

  • #2
    Kinda hard to tell from the limited info (and exactly what it is that you're aiming to do). But yeah, what you have works. Or, you could save yourself a lot of repetition by just iterating over a table from Globals, instead. Try this on for size:

    Place this table in Globals:
    Code:
    tButtons = {
        "Button1";
        "Button2";
        "Button3";
        "Button4";
        "Button5";
        "Button6";
        "Button7";
        "Button8";
        "Button9";
        "Button10";
    };
    And place this for/do loop in each Button On Click event:
    Code:
    for index, value in pairs (tButtons) do
        Button.SetState(value, BTN_UP);
    end
    
    Button.SetState("Button1", BTN_DOWN);
    Obviously, in the Button On Click event, you'd change the above button number to match each corresponding button. But you get the idea, yeah? Lot more efficient this way.

    Happy days, camper.

    Comment


    • #3
      Hi Sameer Delete all codes just you Can Used This Code "On Click" On All Buttons :

      Code:
      for i= 1, 10 do
         if string.match("Button"..i, '%d+')~=string.match(this, '%d+') then
             Button.SetState("Button"..i, BTN_UP);
         end
      end

      Comment


      • #4
        Hi , Sidiamur

        Your Code is very neat and compact , but it has two issues,

        First : If u Click on the same Button the state will change , it should stay at Down State. (maybe u can Fix it)
        Second : BioHazard Code can be used as function and that's better for future changes ( I tried ur Code as function but ddint work, maybe i did somthing wrong)

        Here is my way depending on Biohazard Code:

        On Global :

        Code:
        function Radio_Buttons()
        
        tButtons = {
            "Button1";
            "Button2";
            "Button3";
            "Button4";
            "Button5";
            "Button6";
            "Button7";
            "Button8";
            "Button9";
            "Button10";
        };
        
        for index, value in pairs (tButtons) do
            Button.SetState(value, BTN_UP);
        end
        
        Button.SetState("Button"..tbNOW, BTN_DOWN);
        ---and I also put all other Codes here ----

        And On Each Button :
        Code:
        tbNOW = 1; ---change the number on each Button---
        
        Radio_Buttons()

        Thank you so much for your Code , i will use it for sure in something else unless u made ur changes on it

        Comment


        • #5
          you can also used tbNOW = Button.SetState(this, BTN_DOWN);

          or Button.SetState(this, BTN_DOWN);

          Comment


          • #6
            Well now, let's not be 'too' hasty here. Sidiamur's code actually demonstrates a pretty cluey use of lateral thinking. Hadn't even considered using 'pattern-matching' for something like this. Let's take a closer looksee, yeah?

            The pattern item %d+ is used to match a sequence of one or more digits. We have a count-loop going from 1 to 10 which looks for a string.match on these numbers. And it instructs Button Numbers not equaling the Button Number currently being pressed into the Down state, to be set to the Up state.

            I think it's a clever approach.
            .............................
            .
            Originally posted by sameer valva View Post
            ... If u Click on the same Button the state will change , it should stay at Down State. (maybe u can Fix it)
            Okay, that's an easy fix. Just need to add in an 'else' statement to his code.
            But there's a couple of ways to go about it:
            .
            Method One:
            - Change your state of Button1 to the Up state (so that all 10 buttons are in the Up state at runtime).
            - Then just change sidiamur's code to this:
            Code:
            [I][COLOR=#008000]-- ############################
            -- This goes in [U]all[/U] 10 Buttons
            -- ############################[/COLOR][/I]
            
            for i= 1, 10 do
                if string.match("Button"..i, '%d+')~=string.match(this, '%d+') then
                    Button.SetState("Button"..i, BTN_UP);
                else
                    Button.SetState("Button"..i, BTN_DOWN);
                end
            end
            Method Two:
            If you'd prefer to leave Button1 as you already have it (in the Down state at runtime),
            - Change sidiamur's code to this:
            Code:
            [I][COLOR=#008000]-- ###########################
            -- This goes in Button-1 [U]only[/U]
            -- ###########################[/COLOR][/I]
            
            for i= 1, 10 do
                if string.match("Button"..i, '%d+')~=string.match(this, '%d+') then
                    Button.SetState("Button"..i, BTN_UP);
                else
                    Button.SetState([COLOR=#FF0000]"Button1"[/COLOR], BTN_DOWN);
                end
            end
            
            
            [I][COLOR=#008000]-- #############################
            -- This goes in Buttons 2 [U]to[/U] 10[/COLOR]
            [COLOR=#008000]-- #############################[/COLOR][/I]
            
            for i= 1, 10 do
                if string.match("Button"..i, '%d+')~=string.match(this, '%d+') then
                    Button.SetState("Button"..i, BTN_UP);
                else
                    Button.SetState([COLOR=#FF0000]"Button"..i[/COLOR], BTN_DOWN);
                end
            end
            Nb.
            Haven't looked at what you mentioned about 'using as a Function' ,as you've not yet defined the function's arguments. Happy to help later if needs be. Cool bananas, baby!

            Comment


            • #7
              Edit,
              Okay, think I see where you're headed with this whole 'function' angle.

              LOL, your approach initially threw me for a loop with the way you named your variable, tbNOW. Think maybe you meant tbROW (as in tblRow or tRow), yeah? Although strictly speaking, it's just an nVar. All good, though. Let us know if u need more help, dude.
              ...
              Nb. You'll need to close off that function with an 'end' statement.

              Comment


              • #8
                You Guys are giving a Great help teaching us how to do things in different approaches and learning from you new things that sometimes can't be found in Manual.

                I'm building a big project (at least for me is big ) and making a group of toggle buttons acts as radio buttons is the simple part of the project comparing with other issues.

                I have a lot of questions and i have been trying days and nights to find solutions to my workflow without asking the forum, since i appreciate the time of members, and i find my questions stupid, also I'm very shy of asking too much and i don't want to look like i need someone to do my project for me. I made a huge progress but i need to set things in a correct way.

                I'm really exhausted from trying too much and I'm loosing time.

                Let us know if u need more help, dude.
                Sure i will need your help

                I will share my project and I'm sure i will find my solutions with your help.

                Comment


                • #9
                  Originally posted by sameer valva View Post
                  ... I'm building a big project (at least for me is big ) and making a group of toggle buttons acts as radio buttons is the simple part of the project comparing with other issues ....
                  Just a suggestion - for sake of clarity and (LOL) your own sanity, you might want to consider separating your code as it grows in scope (providing it doesn't interfere with function/sequence). Personally, I'd do something like this:
                  Code:
                  [COLOR=#008000]-- ###########
                  -- Globals --
                  -- ###########[/COLOR]
                  tButtons = {
                      "Button1";
                      "Button2";
                      "Button3";
                      "Button4";
                      "Button5";
                      "Button6";
                      "Button7";
                      "Button8";
                      "Button9";
                      "Button10";
                  };
                  
                  [COLOR=#008000]-- ################
                  -- Page On Show --
                  -- ################[/COLOR]
                  function Radio_Buttons()    
                      for index, value in pairs (tButtons) do
                          Button.SetState(value, BTN_UP);
                      end
                      Button.SetState("Button"..tbNOW, BTN_DOWN);
                  end
                  
                  [COLOR=#008000]-- ###################
                  -- Button On Click --
                  -- ###################[/COLOR]
                  tbNOW = 1;
                  Radio_Buttons()
                  .
                  Originally posted by sameer valva;n300096...
                  I'm really exhausted from trying too much and I'm loosing time...
                  LOL yeah , I know, right?! After slipping into the coding-trance state, fatigue starts distorting time. And then the hallucinations begin. Wait until you start imagining little coding elves running across your script-editor!

                  Comment


                  • #10
                    You can convert my code as a function like this:
                    Put this Code in " On Global Functions " :

                    Code:
                    function ButtonClick(Name)
                        for i= 1, 10 do
                            if string.match("Button"..i, '%d+')~=string.match(Name, '%d+') then
                               Button.SetState("Button"..i, BTN_UP);
                            end
                        end
                    end
                    and you Can Used that Function Who is Named ButtonClick(Name) On all Button like this :

                    Code:
                    ButtonClick(this)
                    just that What is the second required?

                    Comment


                    • #11
                      If u Click on the same Button the state will change , it should stay at Down State. (maybe u can Fix it)
                      Here you are Put this on "Global Function"

                      Code:
                      function ButtonClick(Name)
                          for i= 1, 10 do
                              if string.match("Button"..i, '%d+')~=string.match(Name, '%d+') then
                                 Button.SetState("Button"..i, BTN_UP);
                              end
                          end
                          Button.SetState(Name, 1);
                      end
                      and you can used the Following Code On all Button Like this :

                      Code:
                      ButtonClick(this)
                      any service

                      Comment


                      • #12
                        To prevent the button from going UP when you clicked on it if it was DOWN you need to add on all Buttons :

                        Code:
                        Button.SetState(this, BTN_DOWN);
                        Thank you again, Be sure Huge questions are on the way

                        Comment


                        • #13
                          you are welcome

                          Comment


                          • #14
                            Very nice work, sidiamur. Some high-order thinking demonstrated in your code.
                            Love the way your mind works!

                            Comment


                            • #15
                              thanks BioHazardyou are Welcome

                              Comment

                              Working...
                              X