如何处理用户控件按钮单击事件到父窗体

时间:2012-11-11 13:27:40

标签: c# winforms

我在FormStart上有用户控件NumericKeyPad,我需要,当我点击按钮时,例如Formstart textBoxEmployee上的button3(标签值为3)#它写3等等......

1 个答案:

答案 0 :(得分:0)

UserControl中创建一个自定义事件,单击句柄按钮并从那里提起它。然后,从父表单处理UserControl的自定义事件。为了说明,这里是一个代码示例(VB.NET - 我不是很擅长C#):

Public Class UserControl1
  Public Event ButtonClicked(tag As Object)
  Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
    RaiseEvent ButtonClicked(CType(sender, Button).Tag)
  End Sub
End Class

Public Class Form1
  Private Sub UserControl11_ButtonClicked(tag As Object) Handles UserControl11.ButtonClicked
    TextBox1.Text = tag
  End Sub
End Class