vb.net 将字符串传递给按钮点击子

vb.net passing string into button click sub

有 2 个表单,第一个包含带标签的按钮,当所有表单都使用相同的表单但作为“暗淡表单 2 作为新表单时。第二个表单使用 select 大小写来确定要使用哪些按钮绘图和标题等。我正在努力解决的部分是我创建的新按钮,我需要在事件处理程序子中获得单独的名称。

所以当我点击新创建的按钮时,它们都执行相同的过程,我需要一种在 "button operation sub"

中将它们分开的方法
Public Class DiaryAddForms

Private Sub DiaryAddForms_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

End Sub


' gets the selected case from the button pushed on diary, then draws the appropreate buttons labels ect to the form//

Public Sub GetFormType(ByVal Type As String)

    Select Case Type

        Case "add"

            ' changes the text of the form to the button clicked,
            Me.Text = ("Add Appointment")


            ' passes the appropreate data for the buttons required for add appointment to the "button" sub''

            Button(x:=180, y:=200, name:="Cfind_Btn", title:="Find", hieght:=30, width:=50)
            Button(x:=235, y:=200, name:="Cnew_Btn", title:="New", hieght:=30, width:=50)


        Case "edit"
            Me.Text = ("Edit Appointment")

        Case "delete"
            Me.Text = ("Delete Appointment")

        Case "endday"
            Me.Text = ("End Day")

    End Select

End Sub

' rather than have to create each button individual the data types of each different button can be passed into this sub and then created. 

Public Sub Button(ByVal x As Integer, ByVal y As Integer, ByVal name As String, ByVal title As String, ByVal hieght As Integer, ByVal width As Integer)
    Dim btn As Button
    btn = New Button

    With btn
        .Location = New Point(x, y)
        .Text = title
        .Name = name
        .Width = width
        .Height = hieght
        Controls.Add(btn)

    End With

    AddHandler btn.Click, AddressOf BtnOperation
End Sub

Public Sub BtnOperation(ByVal sender As Object, ByVal e As EventArgs)

    'i need a way to fetch the btn.name'
    'so then with the name use "select case" to get appropreate action of the btn. ' 



End Sub
End Class

BtnOperation 中,sender 参数将成为您的按钮,因此您只需投射它并访问 Name 属性.

Public Sub BtnOperation(ByVal sender As Object, ByVal e As EventArgs)
    Dim btn As Button = DirectCast(sender, Button)
    Dim name As String = btn.Name
    ' do whatever
End Sub