0

我正在创建一个使用诸如治疗、打击等技能的游戏。我希望他们的玩家能够将技能分配给热键。但是我正在寻找一种可以制作“变量”按钮的方法,它可以执行以下操作:将变量(例如“名称”的值)更改为已创建的按钮名称值。前任:

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
4

1 回答 1

0

您可以为按钮(或其他控件)的事件添加新的处理程序。可能希望同时删除先前添加的处理程序。由于您没有详细说明如何选择技能,我选择使用单选按钮(名称以 rb 开头)。当您单击其中一个单选按钮时,它会更改关联按钮的行为(我使用了两个)。

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
于 2013-09-28T22:05:14.537 回答