vb.net结合了两个或更多事件

时间:2013-12-13 07:24:30

标签: vb.net

我有四个textbox的IP地址。每个textbox都适用于每个octate。

当控件失去焦点或用户按下ENTER时,它将检查该值是否大于255。

Private Sub txtParametersIpFirst_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtParametersIpFirst.KeyPress
    If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
        'Check if Value is more than 255
    End If
End Sub

Private Sub txtParametersIpFirst_LostFocus(sender As Object, e As EventArgs) Handles txtParametersIpFirst.LostFocus
    'Check if Value is more than 255
End Sub

这两个事件是否结合在一起?

2 个答案:

答案 0 :(得分:0)

为什么不去这样屏蔽###。###。###。###并使用Validation事件确认输入的值是有效的IP地址。这样就没有必要了四个不同的文本框

This linkMSDN

上有一些很好的资料

答案 1 :(得分:0)

组合两个事件的最佳方法是调用方法..

Private Sub Logic()
  'Check if Value is more than 255
End Sub

Private Sub txtParametersIpFirst_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtParametersIpFirst.KeyPress
    If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then

      Logic()

    End If
End Sub

Private Sub txtParametersIpFirst_LostFocus(sender As Object, e As EventArgs) Handles txtParametersIpFirst.LostFocus

  Logic()

End Sub

但是,如果您要组合两个相同的事件,例如两个不同文本框的两个Lostfocus事件,那么您只需添加句柄;

Private Sub txtText1_LostFocus(sender As Object, e As EventArgs) Handles txtText1.LostFocus, txtText2.LostFocus, 

  ' Code similar for both textbox
  ' You can use the Sender param to verify who's triggering the event

End Sub