vb.netbuttonmultiple-forms

Is there a way to use the same subprogram for multiple buttons across multiple forms in vb.net?


I'm relatively inexperienced with programming, but have been taking it as a subject for a few years. This year is my final year of school, which means I have to undertake a major work. I've chosen the game Mastermind in VB.net, but I've encountered a problem that I haven't learnt to fix. In the main game, I need to cycle through colours by clicking the four buttons each time the person guesses. I want to reuse the code, because it isn't small, but I don't know how to do this with multiple buttons, and more specifically multiple forms. At the moment, the subprogram is in a module with the intention that it can be accessed when a second player picks a code before the game starts. This is the code I'm using to cycle through each of colours. There is another three like this to form the four buttons needed:

CodeSelect1()
     BackColourRed1 = True
     BackColourOrange1 = False
     BackColourYellow1 = False
     BackColourGreen1 = False
     BackColourBlue1 = False
     BackColourPurple1 = False
     If BackColourRed1 = True Then
         frm2PlayerChooseCode.btnCode1.BackgroundImage = My.Resources.Orange
         BackColourRed1 = False
         BackColourOrange1 = True
     ElseIf BackColourOrange1 = True Then
         frm2PlayerChooseCode.btnCode1.BackgroundImage = My.Resources.Yellow
         BackColourOrange1 = False
         BackColourYellow1 = True
     ElseIf BackColourYellow1 = True Then
         frm2PlayerChooseCode.btnCode1.BackgroundImage = My.Resources.Green
         BackColourYellow1 = False
         BackColourGreen1 = True
     ElseIf BackColourGreen1 = True Then
         frm2PlayerChooseCode.btnCode1.BackgroundImage = My.Resources.Blue
         BackColourGreen1 = False
         BackColourBlue1 = True
     ElseIf BackColourBlue1 = True Then
         frm2PlayerChooseCode.btnCode1.BackgroundImage = My.Resources.Purple
         BackColourBlue1 = False
         BackColourPurple1 = True
     ElseIf BackColourPurple1 = True Then
         frm2PlayerChooseCode.btnCode1.BackgroundImage = My.Resources.Red
         BackColourPurple1 = False
         BackColourRed1 = True
     End If
End Sub

I considered a potential solution, which I will put below, but as far as I can work out, it only works for code on the one form. I tried making the variables public, but that gave me an error. I feel as if I'm tickling the edge of what I need to do, but I just can't figure it out:

Public Sub CodeSelect1(sender As Object, e As EventArgs) _
Handles frmEasy.btnCode1.Click, frmEasy.btnCode5.Click, btnCode9.Click, btnCode13.Click, btnCode17.Click, btnCode21.Click, btnCode25.Click, btnCode29.Click, btnCode33.Click, btnCode37.Click
    Dim myButton = DirectCast(sender, Button)

In summary, how do I adapt this code (or use different code entirely), to use across multiple forms? All help is appreciated, and Thank you


Solution

  • If you change your sub to the following, you'll be able to pass the button as a parameter to it. Then, in each button click handler in each form, just add the following code.

    CodeSelect1(CType(sender, Button))
    

    Here's the updated sub

    Friend Sub CodeSelect1(btn As Button)
        BackColourRed1 = True
        BackColourRed1 = False
        BackColourYellow1 = False
        BackColourYellow1 = False
        BackColourBlue1 = False
        BackColourPurple1 = False
        If BackColourRed1 Then
            btn.BackgroundImage = My.Resources.Orange
            BackColourRed1 = False
            BackColourOrange1 = True
        ElseIf BackColourOrange1 Then
            btn.BackgroundImage = My.Resources.Yellow
            BackColourOrange1 = False
            BackColourYellow1 = True
        ElseIf BackColourYellow1 Then
            btn.BackgroundImage = My.Resources.Green
            BackColourYellow1 = False
            BackColourGreen1 = True
        ElseIf BackColourGreen1 Then
            btn.BackgroundImage = My.Resources.Blue
            BackColourGreen1 = False
            BackColourBlue1 = True
        ElseIf BackColourBlue1 Then
            btn.BackgroundImage = My.Resources.Purple
            BackColourBlue1 = False
            BackColourPurple1 = True
        ElseIf BackColourPurple1 Then
            btn.BackgroundImage = My.Resources.Red
            BackColourPurple1 = False
            BackColourRed1 = True
        End If
    End Sub