2015-01-07 5 views
0

Существует 2 формы, первая содержит помеченные кнопки, и когда все используют одну и ту же форму, но как «тусклую форму2 в качестве новой формы, вторая форма использует случай выбора, чтобы определить какие кнопки для рисования и названия и т. д. часть, с которой я борюсь, - это новые кнопки, которые я создал, мне нужно получить отдельные имена в обработчике событий.vb.net передача строки в кнопку click 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 
+0

Поставить 'sender' на экземпляр' Button'. В C# вы можете использовать '((Button) отправитель) .Name'. – Spooky

ответ

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 
+0

отлично, спасибо большое – notme61

Смежные вопросы