Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to override a click method in a button on a form?
Message
De
17/06/2004 21:39:20
 
 
À
15/06/2004 01:36:36
Information générale
Forum:
ASP.NET
Catégorie:
Formulaires
Divers
Thread ID:
00913730
Message ID:
00914886
Vues:
12
Thank you for your very helpful response Bonnie! Actually, what let me to this, is that I'm working in MM.Net and I wanted to debug/step through the code that happens in one of the MM buttons (mmButtonClose for instance) I dropped on a form. In VFP this was so simple. I'd just put my breakpoint on a DODEFAULT command in the click() of the button that I dropped on the form. Then I can step through the base class code.

So, I can't do this in .Net? And therefore, would I have to go into the base class and put a breakpoint there? Seems weird to a VFP'er. I know VFP does things a little differently as far as oops and form containers.

A more practical question/example would maybe be this... If I create a button class that does some special stuff, and I create a "Hook" method in the class for the developer to put some custom code when he uses my button on his form. Is it true then that in order for the developer to be able to enter his hook method code, he would have to first, sub-class the button (saving it in a .cs file), enter his hook code there, and then drag his sub-classed button on the form? Gee, if that's true, then I'm really starting to miss VFP already! :)

Thanks again for your help!

>Mark,
>
>Well, the Click event of a button probably wasn't a very good example. I doubt there's really anything you'd want to put in a base class button's Click event handler anyway ... most of the time a button click event will be handled by the form or control that the button has been placed on, not the button itself. But, to answer your question, yes, you'd need a sub-class in order override your base button's event handler. And this sub-class needn't have any code in it's click eventhandler ... just the fact that you've overridden the event handler will keep your base button's event handler from firing:
>
>// Base Class
>public class MyButton : System.Windows.Forms.Button
>{
>	public MyButton()
>	{
>		this.Click += new EventHandler(ClickHandler);
>        }
>	protected virtual void ClickHandler(object sender, EventArgs e)
>	{
>		// Whatever code you need
>	}
>}
>
>// SubClass
>public class MySubButton : MyButton
>{
>	public MySubButton()
>	{
>		this.Click += new EventHandler(ClickHandler);
>        }
>	protected override void ClickHandler(object sender, EventArgs e)
>	{
>		// Whatever code you need that for this sub-class
>
>                // note that the base class, MyButton, ClickHandler will
>                // *not* fire unless you execute
>                // base.ClickHandler(sender, e);
>	}
>}
>
>// And on the form, if you want to add even more functionality
>MySubButton oButton = new MySubButton();
>this.oButton.Click += new EventHandler(oButton_Click);
>
>private void oButton_Click(object sender, EventArgs e)
>{
>	// Whatever additional code you need on the form
>        // Note that this *will* call the MySubButton's
>        // ClickHandler() method first.
>}
>
>
>Again, I don't think a Click event is a good example, but that's what I showed since it's what you asked about. Perhaps you have a need for this functionality that I haven't thought of.
>
>~~Bonnie
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform