469,268 Members | 1,045 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,268 developers. It's quick & easy.

Inheriting Button

Hi,

I have a form that I need to place 3 buttons on. I would like the 3
buttons to call the same method, perhaps with different arguments.

In Powerbuilder, I could create a visual button object and code for
it, then inherit from that and place it on my form/window.

Does C# have anything like this? Is this possible?

Thanks!

Oct 12 '07 #1
4 4353
Soulless wrote:
Hi,

I have a form that I need to place 3 buttons on. I would like the 3
buttons to call the same method, perhaps with different arguments.
Why not simply provide the same event handler for all three buttons?
If they will all call the same method with different arguments, you may
as well just have three separate event handlers.
If say two of the three will be the same, it might be simpler to have
them share an event handler then check the sender, a la:

Button first = new Button("One");
Button second = new Button("Two");
Button third = new Button("Three");

first.Click += new EventHandler(mySpecialButton_Click);
second.Click += new EventHandler(mySpecialButton_Click);
third.Click += new EventHandler(mySpecialButton_Click);

private void mySpecialButton_Click(object sender, EventArgs e)
{
if (sender == first)
SomeOtherMethod("arg1", "arg2", overloaded_int);
else
SomeOtherMethod("arg1", "arg2");
}
Either way, it doesn't look like you need inheritance at all.

Chris.
Oct 12 '07 #2
On Oct 12, 3:15 pm, Chris Shepherd <c...@nospam.chsh.cawrote:
Soulless wrote:
Hi,
I have a form that I need to place 3 buttons on. I would like the 3
buttons to call the same method, perhaps with different arguments.

Why not simply provide the same event handler for all three buttons?
If they will all call the same method with different arguments, you may
as well just have three separate event handlers.
If say two of the three will be the same, it might be simpler to have
them share an event handler then check the sender, a la:

Button first = new Button("One");
Button second = new Button("Two");
Button third = new Button("Three");

first.Click += new EventHandler(mySpecialButton_Click);
second.Click += new EventHandler(mySpecialButton_Click);
third.Click += new EventHandler(mySpecialButton_Click);

private void mySpecialButton_Click(object sender, EventArgs e)
{
if (sender == first)
SomeOtherMethod("arg1", "arg2", overloaded_int);
else
SomeOtherMethod("arg1", "arg2");

}

Either way, it doesn't look like you need inheritance at all.

Chris.
Thanks!!!!

Oct 12 '07 #3
Soulless,
this is called overriding method in the Object oriented programming, find
below a good guide for that
http://www.devhood.com/tutorials/tut...utorial_id=328

regards,
Husam Al-A'araj
www.aaraj.net

"Soulless" wrote:
Hi,

I have a form that I need to place 3 buttons on. I would like the 3
buttons to call the same method, perhaps with different arguments.

In Powerbuilder, I could create a visual button object and code for
it, then inherit from that and place it on my form/window.

Does C# have anything like this? Is this possible?

Thanks!

Oct 12 '07 #4
Soulless <dg*******@gmail.comwrote:
I have a form that I need to place 3 buttons on. I would like the 3
buttons to call the same method, perhaps with different arguments.

In Powerbuilder, I could create a visual button object and code for
it, then inherit from that and place it on my form/window.

Does C# have anything like this? Is this possible?
Well, you *can* derive from Button - but I wouldn't advise it in this
case. Why not just use three separate instances of the normal button
class, and hook the Click events up to the same method?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 12 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Noah Coad [MVP .NET/C#] | last post: by
2 posts views Thread by Charles Law | last post: by
5 posts views Thread by Billy | last post: by
4 posts views Thread by Xero | last post: by
3 posts views Thread by Alex Satrapa | last post: by
9 posts views Thread by Dominique | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.