按钮变量VB.Net

时间:2013-09-28 20:44:31

标签: vb.net variables button

即时创建游戏使用技能,如治疗,罢工等。我希望他们的玩家能够将技能分配给热键。但我正在寻找一种方法,我可以做一个“变量”按钮,可以做这样的事情: 将“Name”的值之类的变量更改为已创建的按钮名称值。 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 个答案:

答案 0 :(得分:0)

您可以为按钮(或其他控件)的事件添加新处理程序。可能希望同时移除先前添加的处理程序。由于你没有详细说明如何选择技能,我选择使用radiobuttons(名称以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