0

im creating a game the uses Skills such as heal, strike etc. i want they player to be able to assign the skill to a hotkey. but im looking for a way where i can make a "Variable" button, that can do things like this: change a variable like "Name"'s value to an already created button name value. EX:

Dim bla As New Button
Private Sub btnHot3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHot3.Click
    bla = New Button
    bla.Name = "btnHot3"
    Hotkey(SkillUsed, bla)
End Sub
Sub Hotkey(ByVal skillused As Integer, ByVal bla As Button)
    If SkillSelected = 1 Then
        btnHot3.Image = My.Resources.Heal
    ElseIf SkillSelected = 2 Then
        bla.Image = My.Resources.Strike
    ElseIf SkillSelected = 3 Then
        bla.Image = My.Resources.Finisher
    End If
End Sub

1 Answer 1

0

You can add a new handler to the events of a button (or other control). It may be desirable to remove the previously added handler(s) at the same time. As you didn't give much detail as to how the skill would be selected, I chose to use radiobuttons (with names starting with rb). When you click one of the radiobuttons, it changes the behaviour of an associated button (I used two).

Public Class Form1

Dim currentClickHandlers As New Dictionary(Of Button, List(Of EventHandler))

Sub SetOnlyClickHandler(target As Button, handler As EventHandler, label As String)
    ' remove any previously stored click event handlers if they exist
    If currentClickHandlers.ContainsKey(target) Then
        For Each hndlr In currentClickHandlers(target)
            RemoveHandler target.Click, hndlr
        Next
        currentClickHandlers(target).Add(handler)
    Else
        Dim evntHandlers = New List(Of EventHandler)
        evntHandlers.Add(handler)
        currentClickHandlers.Add(target, evntHandlers)
    End If

    AddHandler target.Click, handler
    target.Text = label

End Sub

Sub SetHotButtonAction(target As Button, action As Integer)
    ' depending on which button is the target, set appropriate actions
    Select Case True

        Case target.Equals(Button1)

            Select Case action
                Case 1
                    target.BackColor = Color.Green
                    SetOnlyClickHandler(target, AddressOf Heal, "Heal")
                Case 2
                    target.BackColor = Color.Yellow
                    SetOnlyClickHandler(target, AddressOf Strike, "Strike")
                Case 3
                    target.BackColor = Color.Red
                    SetOnlyClickHandler(target, AddressOf Finisher, "Finisher")
            End Select

        Case target.Equals(Button2)

            Select Case action
                Case 1
                    target.BackColor = Color.Purple
                    SetOnlyClickHandler(target, AddressOf RunAway, "Run away")
                Case 2
                    target.BackColor = Color.Pink
                    SetOnlyClickHandler(target, AddressOf DuckAndCover, "Duck and cover")
            End Select

    End Select

End Sub

' methods to be used for the actions
Sub Heal(sender As Object, e As EventArgs)
    MsgBox("Heal")
End Sub

Sub Strike(sender As Object, e As EventArgs)
    MsgBox("Strike")
End Sub

Sub Finisher(sender As Object, e As EventArgs)
    MsgBox("Finisher")
End Sub

Sub RunAway(sender As Object, e As EventArgs)
    MsgBox("Flee!")
End Sub

Sub DuckAndCover(sender As Object, e As EventArgs)
    MsgBox("Duck and cover")
End Sub

' set the actions of the radiobuttons
Private Sub rbHeal_Click(sender As Object, e As EventArgs) Handles rbHeal.Click
    SetHotButtonAction(Button1, 1)
End Sub
Private Sub rbStrike_Click(sender As Object, e As EventArgs) Handles rbStrike.Click
    SetHotButtonAction(Button1, 2)
End Sub
Private Sub rbFinisher_Click(sender As Object, e As EventArgs) Handles rbFinisher.Click
    SetHotButtonAction(Button1, 3)
End Sub

Private Sub rbRunAway_Click(sender As Object, e As EventArgs) Handles rbRunAway.Click
    SetHotButtonAction(Button2, 1)
End Sub

Private Sub rbDuckAndCover_Click(sender As Object, e As EventArgs) Handles rbDuckAndCover.Click
    SetHotButtonAction(Button2, 2)
End Sub

End Class
Sign up to request clarification or add additional context in comments.

6 Comments

thank you very much for your time and effort. im compleatly new to the code and things that you used in the answer above, and thus have no idea how to apply it to directly how my code is layed out, would i be able to paste all the code related to the skills prtion of my program for you to take a look at? because im not too sure how to manage this all.
@user2826539 I suggest that you make a new Windows Forms project just for the code I posted so that you can see how it works. You will need to add five radiobuttons named rbHeal, rbStrike, rbFinisher, rbRunAway and rbDuckAndCover, and two buttons (leave their names as the default Button1 and Button2). Run it and see how it behaves - does it work in some fashion similar to what you want?
i get 2 errors for the following lines(square brakets around error): Dim evntHandlers = New List(Of EventHandler)[ From {handler} ] currentClickHandlers.Add(target, [evntHandlers)]
That syntax is for more recent versions of Visual Basic. I'll try to edit my answer for earlier versions... give it a minute and look again.
alright, i got this all to work on a new form, but im not sure how to apply it to how my program works. i have a main battle form, that looks like this: tinypic.com/view.php?pic=2z6xyrd&s=5#.Ukdgr4aURiK and i also have a "skill" form, that shows the skill hierarchy. when you click on a button, that has just the image of the skill, it sets a variable to a value corisponding to the skill it is. so heal button returns a value of "1". then the user can then click on the "add a hotkey" button, on the same form, which onpens the hotkey adding form. there are 3 buttons on that form, hotkey1-3.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.