Call an event from the base class

K

kenny.deneef

Hey all

We are working on a project and have alot of forms with textboxes on.
Now we want to put some input validation keypress events on those
textboxes. We got a parent class where we putted some event into ex.

public void AlfaKeyPress(object sender, KeyPressEventArgs e)
{
// still have to put something into this
e.Handled = false;
}

But it seems inpossible to link to this event from a child class
without writing for all textboxes the eventhandlers. We thought, just
select all textboxes, go to the event tab en select the proper event,
but guess again it didn't work. The event was no where to be found. We
tried public, internal, internal protected and protected but zero
results.

Someone got an idee how to do this without adding it all by hand of by
adding all the event to each form.

Thanks in advanced
 
B

bob clegg

Hi,
Not sure I understand your problem.
I created 3 text boxes, selected them.
Double clicked the validating event from the events lists and got a
handler stub.
Checking inside InitializeComponent confirmed the stub was hooked to
all 3 text boxes.
Bob
 
J

Jon Skeet [C# MVP]

We are working on a project and have alot of forms with textboxes on.
Now we want to put some input validation keypress events on those
textboxes. We got a parent class where we putted some event into ex.

public void AlfaKeyPress(object sender, KeyPressEventArgs e)
{
// still have to put something into this
e.Handled = false;
}

But it seems inpossible to link to this event from a child class
without writing for all textboxes the eventhandlers. We thought, just
select all textboxes, go to the event tab en select the proper event,
but guess again it didn't work. The event was no where to be found. We
tried public, internal, internal protected and protected but zero
results.

You can't *call* an event from a derived class unless the base class
specifically makes that ability available to you. However, unless it's
a private event or an internal event from a different assembly, you
should be able to add a handler to it.

Effectively, an event is just a pair of methods to subscribe and
unsubcribe handlers. With "field-like" events, a delegate variable is
automatically created behind the scenes.

See http://pobox.com/~skeet/csharp/events.html for more details.

Now, if you were genuinely trying to *subscribe* to an event and
couldn't, please give more details, preferably with a short but
complete program demonstrating the problem.
 
K

kenny.deneef

You can't *call* an event from a derived class unless the base class
specifically makes that ability available to you. However, unless it's
a private event or an internal event from a different assembly, you
should be able to add a handler to it.

Effectively, an event is just a pair of methods to subscribe and
unsubcribe handlers. With "field-like" events, a delegate variable is
automatically created behind the scenes.

Seehttp://pobox.com/~skeet/csharp/events.htmlfor more details.

Now, if you were genuinely trying to *subscribe* to an event and
couldn't, please give more details, preferably with a short but
complete program demonstrating the problem.

The problem is not, I can't add the event with an eventhandler in the
code itself but we where wondering why we can't choose the events we
from the base-class in a derived class. Cause we thought cause its a
derived class all the events are avaible in this class to, and they
are when we use the eventhandlers and += operator and stuff to add
them to every textbox.

However the event don't show up in the list where you can choose those
autogenerated event textbox1_KeyPress, textbox1_TextChanged, ...
Those you find under the yellow lightningstrike when you select a
controle.

Only the event I write in this class are show up but not events from
the base class.

It's not a big problem but rather something that could speed-up the
development. Is it impossible to get those events into those listboxes
cause those are made with some sort of script that only checks for
events in the class itself and not for the derived class.

Thanks in advanced
 
J

Jon Skeet [C# MVP]

The problem is not, I can't add the event with an eventhandler in the
code itself but we where wondering why we can't choose the events we
from the base-class in a derived class. Cause we thought cause its a
derived class all the events are avaible in this class to, and they
are when we use the eventhandlers and += operator and stuff to add
them to every textbox.

However the event don't show up in the list where you can choose those
autogenerated event textbox1_KeyPress, textbox1_TextChanged, ...
Those you find under the yellow lightningstrike when you select a
controle.

So in other words, you're only talking about an IDE designer issue, not
a language issue? If so, I'm not really the one to help - I don't tend
to use the designer.
 
K

kenny.deneef

So in other words, you're only talking about an IDE designer issue, not
a language issue? If so, I'm not really the one to help - I don't tend
to use the designer.


Yea, but like i said. Its not realy "a problem" more something you
would think would work but doesn't. I has to be something with the way
VS 2005 only sees the events in the class "form" you're working and
not in a base class. So I guess, we want to do something thats not
possible because of this. if some1 got an methode how to make it work
its still welcome.

Thanks
 
J

Jon Skeet [C# MVP]

Yea, but like i said. Its not realy "a problem" more something you
would think would work but doesn't. I has to be something with the way
VS 2005 only sees the events in the class "form" you're working and
not in a base class. So I guess, we want to do something thats not
possible because of this. if some1 got an methode how to make it work
its still welcome.

No, what you're trying to do is perfectly possible (if I've understood
you correctly) it's just not working in the form designer. Does
Intellisense show you the event in the code view?
 
K

kenny.deneef

No, what you're trying to do is perfectly possible (if I've understood
you correctly) it's just not working in the form designer. Does
Intellisense show you the event in the code view?

Yea, the intel shows the events in the code view.
But like i said already its not a "problem" rather something to speed-
up the process, however now we already got it all written down with
the eventhandlers. Thanks anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top