A fired event bug in VB.net 2003?!

  • Thread starter Thread starter Yuanxi Liu
  • Start date Start date
Y

Yuanxi Liu

Hi, everyone. I get a very interesting thing in VB.net
2003.

First you create a base form, and then add a button in
the baseform. Write an overridable function to fire a
event when the buton is clicked. I just write the
following code:
Overridable sub btnOK_Click(...) handles btnOK.click
me.close
End sub

Second you create a new form. inherit it from the base
form. Make a new override function about the button OK. I
just write down:
Overrides sub btnOK_Click(...) handles btnOK.click
msgbox("Oh, no",...)
End sub

Finally you run this project, and click the button in the
child form. You will find the msgbox pops out twice, which
means the override function executes twice!
..
 
Yuanxi Liu said:
Hi, everyone. I get a very interesting thing in VB.net
2003.

First you create a base form, and then add a button in
the baseform. Write an overridable function to fire a
event when the buton is clicked. I just write the
following code:
Overridable sub btnOK_Click(...) handles btnOK.click
me.close
End sub

Second you create a new form. inherit it from the base
form. Make a new override function about the button OK. I
just write down:
Overrides sub btnOK_Click(...) handles btnOK.click
msgbox("Oh, no",...)
End sub

Finally you run this project, and click the button in the
child form. You will find the msgbox pops out twice, which
means the override function executes twice!
.

That's how I'd expect it to be. You add two event handlers by using the
Handles keyword twice. The Handles statement in the base form points to
btnOk_Click, but as it is "overriden" in the derived Form, the procedure in
the derived form is added as an event handler. The second time you add the
handler is in the derived Form because there is another Handles keyword. It
also points to the procedure in the derived form, so your msgbox comes
twice.
 
Back
Top