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
新闻热点
疑难解答
图片精选