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