日期:2012-08-14 浏览次数:20461 次
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e as _ System.EventArgs) Handles Button1.Click Static Counter as Integer = 0 Counter += 1 MessageBox.Show (" 此按钮已被单击 " & _ Counter.ToString() & " 次。")End Sub
Protected Overridable Sub Button1_Click(ByVal sender As System.Object, _ ByVal e as System.EventArgs) Handles Button1.Click Static Counter as Integer = 0 Counter += 1 MessageBox.Show (" 此按钮已被单击 " & _ Counter.ToString() & " 次。")End Sub
注意:不要在方法中添加 Handles 子句。事件处理程序已经与基类中的事件相关联,并且此关联将传递到继承类中。也就是说,当引发事件时将执行此方法,而不再需要额外的 Handles 子句。
Protected Overrides Sub Button1_Click(ByVal sender As System.Object, _ ByVal e as System.EventArgs) Static Counter as Integer = 0 Counter += 1 MessageBox.Show (" 此继承的按钮已被单击 " & _ Counter.ToString() & " 次。")End Sub
' 不正确的代码Protected Overrides Sub Button1_Click(ByVal sender As System.Object, _ ByVal e as System.EventArgs) Handles Button1.Click Static Counter as Integer = 0 ' 每次单击按钮时,此变量将递增 ' 两次。 Counter += 1 ' 每次单击按钮时,消息框将显示 ' 两次,并显示不准确的信息。 MessageBox.Show (" 此继承的按钮已被单击 " & _ Counter.ToString() & " 次。")End Sub