Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to override a click method in a button on a form?
Message
From
15/06/2004 01:36:36
 
 
To
14/06/2004 22:35:33
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
00913730
Message ID:
00913759
Views:
18
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


>Hi, I'm trying to learn C# and MM.net, so please forgive me for asking a very basic question... I drop a button control on a form, then I double-click on the button to create the Click event handler Btn1_Click() on the form. I understand that this is a handler that gets executed *after* the click() method in the button class fires. My question is, how can you actually override a click method of a button dropped on a form? If that button class had some code in its click method that I didn't want to fire, how would I do it? Do I have to create a sub-class of the button first and then drop that on the form, and put my code in the sub-classed click method? And do that for each button I want to put code in? Seems it would get confusing with all those sub-classed controls. Thanks IA.
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform