Cannot bind an event handler to the 'Click' event because it is read only.

  • Thread starter Thread starter Randy Fraser
  • Start date Start date
R

Randy Fraser

What is the proper way of declaring an override on a button click event for
a base form.

I am getting " Cannot bind an event handler to the 'Click' event because it
is read only." listed in the events. It works but should I be getting this
message.

Base form:

Protected Overridable Sub btnSave_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnSave.Click

'Override this function from the derived form

End Sub

Derived form

Protected Overrides Sub btnSave_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnSave.Click

'Save code here

End Sub



Best Regards

Randy
 
b1fhmq63 said:
What is the proper way of declaring an override on a button click event for
a base form.

You don't override events, you subscribe to them. You override methods.
If you're creating a subclass of a "Button" and want to change what
happens when it's clicked, you'll want to override the protected OnClick
method.
 
Private Sub btnSave_Click(ByVal sender As System.Object, ByVa
e As System.EventArgs) Handles btnSave.Clic

ABC(
End Su

Protected Overridable Sub ABC(

'Override this function from the derived for

End Sub
 
I don't quite understand.

I am creating a subclass of a form, not a button. On the base form it has
buttons for CRUD operations. Depending on the derived form, these click
events may require different code so I have to override these events. The
code I showed above works but gives me the error in the task list.

Can I also have code that can be executed everytime in the base class event
as well as extra code in the derived form for the same event? Or do I just
call a base class method from the derived class. (That is extra work adding
one line of code though)

Thanks for your help.
 
b1fhmq63 said:
I am creating a subclass of a form, not a button. On the base form it has
buttons for CRUD operations. Depending on the derived form, these click
events may require different code so I have to override these events. The
code I showed above works but gives me the error in the task list.

I think what you'll want to do on your base form is have a "protected
overridable" method that is called from the click events of your base
form buttons. For example (pseudo-code):

Protected Overrideable Sub SaveRecord()
' stuff to save a record
End Sub

In your derived class, override the SaveRecord method to add your own
custom logic:

Protected Overrides Sub SaveRecord()
' derived stuff
End Sub

If you want to run both the base form logic and your derived logic, add
a call to the base forms' SaveRecord:

Protected Overrides Sub SaveRecord()
MyBase.SaveRecord()
' derived stuff
End Sub

Hope this helps.
 
Back
Top