Results 1 to 4 of 4

Thread: [RESOLVED] Changing colour of a button depending on how many times its been click(Rubik Cube)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2017
    Posts
    4

    Resolved [RESOLVED] Changing colour of a button depending on how many times its been click(Rubik Cube)

    hello,so I am creating a program that will have the option to simulate a Rubik cube or solve a Rubik cube, and this program had a form where you would input the colours of each square on a rubik cube.
    I came up with a way by using case statements to change colour of button and the char in array(there are 6 3x3 arrays)

    My solution:
    Code:
         Public Sub BottomLeftG_Click(sender As Object, e As EventArgs) Handles BottomLeftG.Click
            Static count As Integer
            count += 1
            NoSquaredClick = NoSquaredClick + 1
            Select Case count
                Case = 1
                    BottomLeftG.BackColor = Color.Red
                    greenface(1, 1) = "R"
                    NoRed = NoRed + 1
                Case = 2
                    BottomLeftG.BackColor = Color.Blue
                    greenface(1, 1) = "B"
                    NoRed = NoRed - 1
                    Noblue = Noblue + 1
                Case = 3
                    BottomLeftG.BackColor = Color.Lime
                    greenface(1, 1) = "G"
                    Noblue = Noblue - 1
                    NoGreen = NoGreen + 1
                Case = 4
                    BottomLeftG.BackColor = Color.Yellow
                    greenface(1, 1) = "Y"
                    NoGreen = NoGreen - 1
                    NoYellow = NoYellow + 1
                Case = 5
                    BottomLeftG.BackColor = Color.OrangeRed
                    greenface(1, 1) = "O"
                    NoYellow = NoYellow - 1
                    NoOrange = NoOrange + 1
                Case = 6
                    BottomLeftG.BackColor = Color.White
                    greenface(1, 1) = "W"
                    NoOrange = NoOrange - 1
                    NoWhite = NoWhite + 1
            End Select
    
            If count = 6 Then
                count = count - 6
            End If
    
        End Sub
    img of buttons:
    http://puu.sh/tiVbi/b85dfd4ee4.png
    doing this for 54 buttons is very long and difficult to edit, was wondering if there is a shorter way to archive this.
    Last edited by Shaggy Hiker; Jan 11th, 2017 at 03:34 PM. Reason: Added CODE tags.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: Changing colour of a button depending on how many times its been click(Rubik Cube

    Welcome to the forums. I edited your post to add the [CODE][/CODE] tags. You can do this by pressing the # button and pasting (or typing) code between the tags, or you can add the code, select it, and then press the # button. Some folks prefer the VB button (which looks like VE), as it adds coloring, but it's slightly more work.

    I'd be inclined to have a single handler for ALL the buttons. It looks like you currently have a different handler for each button. There are two points to consider: The first is that the sender argument IS the button, so you always have the button that raised the click event when you click on the button. The second is the .Tag property that all controls have. The .Tag property is a grab-bag property that can hold whatever you need it to hold.

    I'm not sure what the greenface array holds, but I would guess that it's an array of faces and buttons. The .Tag property could replace that. Each button could keep track of where it was on the cube, and could keep track of the color, all in one. I can't say more than that, as I'm not quite sure what the logic is. You'd likely still need the Select statement, but you'd only need what you showed, because all the buttons would use the same handler, and the parameters to the greenface array would be either part of the .Tag (each button could keep track of its own x,y), or perhaps the array itself would become the .Tag. If the button kept track of its own x,y, it would also have whatever color it currently was, since you set it, so whatever role the greenface array holds could be filled by the buttons themselves....though it is quite possible that the logic would be easier using the greenface array rather than a list of the buttons.
    My usual boring signature: Nothing

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Changing colour of a button depending on how many times its been click(Rubik Cube

    Welcome to VBForums

    If you want to have very similar code (just acting on different variables etc) multiple times, you should create a Sub or a Function (they are basically the same, but a Function returns a value), or in terms of controls you could merge the handlers as Shaggy Hiker described.

    Based on the assumption that greenface is the array for one 3x3 section, and you have 6 differently named arrays, I think I would recommend a Function like this (based on your code, but using parameters):
    Code:
    Private Sub ButtonRotate(theButton as Button, ByRef count As Integer) as String
            Dim newValue as String
            count += 1
            NoSquaredClick = NoSquaredClick + 1
            Select Case count
                Case = 1
                    theButton.BackColor = Color.Red
                    newValue = "R"
                    NoRed = NoRed + 1
                Case = 2
                    theButton.BackColor = Color.Blue
                    newValue = "B"
                    NoRed = NoRed - 1
                    Noblue = Noblue + 1
                Case = 3
                    theButton.BackColor = Color.Lime
                    newValue = "G"
                    Noblue = Noblue - 1
                    NoGreen = NoGreen + 1
                Case = 4
                    theButton.BackColor = Color.Yellow
                    newValue = "Y"
                    NoGreen = NoGreen - 1
                    NoYellow = NoYellow + 1
                Case = 5
                    theButton.BackColor = Color.OrangeRed
                    newValue = "O"
                    NoYellow = NoYellow - 1
                    NoOrange = NoOrange + 1
                Case = 6
                    theButton.BackColor = Color.White
                    newValue = "W"
                    NoOrange = NoOrange - 1
                    NoWhite = NoWhite + 1
            End Select
    
            If count = 6 Then
                count = count - 6
            End If
    
            Return newValue
    End Function
    For each button click, you would then call the Function like this:
    Code:
         Public Sub BottomLeftG_Click(sender As Object, e As EventArgs) Handles BottomLeftG.Click
            Static count As Integer
            greenface(1, 1) = ButtonRotate(BottomLeftG, count)
        End Sub
    Merging the handlers could well be a more optimal solution, but is a bit harder to understand, and could be more work in terms of matching it up to your arrays.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2017
    Posts
    4

    Re: Changing colour of a button depending on how many times its been click(Rubik Cube

    Thanks for your help guys, The code works and it is a lot easier the mange now . going to have to get this thing done .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width