2015-01-07 4 views
0

첫 번째 단추에는 레이블이 지정된 단추가 들어 있으며 모두 같은 양식이지만 "희미한 form2는 새 양식"으로 사용됩니다. 두 번째 양식에서는 select case를 사용하여 어떤 버튼을 그리고 제목 요법. 내가 고민하고있는 부분은 내가 만든 새 단추입니다. 나는 이벤트 핸들러 하위에 별도의 이름을 가져야합니다.vb.net 단추를 클릭하여 문자열을 전달

그래서 내가 새로 만든 버튼을 모두 클릭하면 상기 senderBtnOperation의 "버튼 작동 부"

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

'Button'인스턴스에 '보낸 사람'을 전송하십시오. C#에서는'((Button) sender) .Name'을 사용할 수 있습니다. – Spooky

답변

1

그들을 분리시키는 방법이 필요 동일한 공정을 수행 매개 변수가 버튼이므로 그냥 캐스팅하고 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

관련 문제