'Need help with Error message

ALX

Well-known member
Joined
Nov 16, 2005
Messages
253
Location
Columbia, SC
Programming Experience
10+
Why do I keep getting this error message for Form classes that inherit from another Form...?

" Cannot bind an event handler to the 'Click' event because it is read only."
 
http://www.dotnet247.com/247reference/msgs/52/261089.aspx
"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."
 
Well, I went through the class and removed code blocks until the error messages cleared, and it seems that what's triggering these messages are click events in the derived class for buttons (or labels, actually) in the base class. These are pretty straight forward event handlers...

This code is in the derived class:
Protected Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
If Edit Then SavePicks(0)
EndSub

The SaveButton is in the base class. There are no event handlers for the SaveButton in the base class, so there is no overriding going on here. Maybe I need to make a method to override or can you override a method that doesn't exist ?

 
Well there's abolutely no point in overriding an event that doesn't exist, what would you be overriding? I must admit i use inheritance regularly in my apps and have never had this problem. One more thing why are you putting controls in a base class then not providing base events for them to override in the derived classes?. Just my opinion but i think it defeats the object of inheritance, in part anyway.
 
The control is made available in the base class for use by all derived classes. Each derived class handles this event differently. To avoid having to add and position the same button (in the same location) in each derived form, it was put in the base form. The base form is never executed by itself. It is always inherited by another form (hence there is no need to code for this button click in the base class). The code executes properly, it's just that nagging message in the task list keeps popping up when the form is loaded in the development environment.
 
How was the (button) control "made available" ?
I have done similar stuff as you describe above, and I had to use a withevents variable to the base form button to subscribe to it's events in an inherited form.
- baseform inherits System.Windows.Forms.Form and contains button1
- inhForm inherits baseform
withevents b as button = me.button1 'enables events for baseform button
sub b_click(sender, e) handles b.click

 
The button (which is actually a label) is declared as "Friend" in the base class and I presume that's how it's made available to the derived classes.
The declaration for the "SAVE" button in the base form is:

Friend With Events SaveButton As System.Windows.Forms.Label

The code for the sub in the derived class is:

Protected
Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click

As I said, the code executes perfectly. I guess I'm going to have to make a dummy (blank) sub in the base class to trap the event and override it in the inherited classes. Seems tacky..... When I get these error messages, I can only assume that I'm doing things the wrong way.
The books I use for reference are great at discussing slightly advanced theory but don't help newbies with the mundane basics. I'd like to find a book that teaches VB.NET from the ground - up, for people that do not have experience with VB4, 5 or 6, but need more than "VB for Idiots". Right now my best reference is "Programming Microsoft Visual Basic .NET" by Francesco Balena. This is a great book (much better the third time through) but several topics go right over my head.
 
Last edited:
In your baseform change:
Friend WithEvents SaveButton As System.Windows.Forms.Label
to:
Public WithEvents SaveButton As System.Windows.Forms.Label

that should solve it
 
Yes, I didn't catch the error you got since I in inheritedform specifically setup my own withevents pointing to the baseform button. Once I addressed the event directly that error popped, but only in Design View of inherited form, so it wasn't obvious to begin with.
 
Back
Top