Inheriting Button

S

Soulless

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!
 
C

Chris Shepherd

Soulless said:
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.
 
S

Soulless

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!!!!
 
J

Jon Skeet [C# MVP]

Soulless said:
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?
 

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