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

时间:2015-01-07 17:01:49

标签: vb.net string oop

有两种形式,第一种形式包含带标签的按钮,当所有形式都使用相同的形式,但作为“昏暗的形式2”作为新形式。第二种形式使用选择案例来确定绘制哪些按钮和标题等。我正在努力的部分,是我创建的新按钮,我需要在事件处理程序子中找到单独的名称。

因此,当我点击新创建的按钮时,他们都执行相同的过程我需要一种在“按钮操作子”中分离它们的方法

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

1 个答案:

答案 0 :(得分:1)

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