简体   繁体   中英

How do I make buttons do the same thing?

I just started programming, and I want to use WinForms to make multiple buttons that you can click on to change from white to lime-green and back to white. I have done this for one button:

private void button1_Click(object sender, EventArgs e)
    {
        if (button1.BackColor != Color.Lime)
        {
            button1.BackColor = Color.Lime;
        }
        else
        {
            button1.BackColor = Color.White;
        }
    }

Now I could copy and paste that for all of the buttons, but I know that is inefficient; and if I use winforms to reference button1 on button2, it will just change the color of button1 (obviously).

So, do I need to use a helper method, new class, or something else? What would that look like?

There are a couple of approaches. One might be to create a common function which the different buttons call:

private void button1_Click(object sender, EventArgs e)
{
    ChangeColor(button1);
}

private void ChangeColor(Button button)
{
    if (button.BackColor != Color.Lime)
        button.BackColor = Color.Lime;
    else
        button.BackColor = Color.White;
}

Then each button handler can use that same function call.

Or, if all of these buttons will always ever do exactly the same thing, then you can use one click handler function for all of them. In this case what you'd need to do is determine which button invoked the handler (whereas you're currently referencing button1 directly) so that you know which one to change. The sender object passed into the handler function is actually a reference to the form element which invoked the handler. All you need to do is cast it:

private void button_Click(object sender, EventArgs e)
{
    var button = (Button)sender;
    if (button.BackColor != Color.Lime)
        button.BackColor = Color.Lime;
    else
        button.BackColor = Color.White;
}

So first the handler grabs a reference to the button which invoked it, then runs the logic on that button. Note also how I made the name of the handler function slightly more generic. Now you'd go to the form designer and set button_Click as the click handler for all of the buttons which should invoke this.

You do this the exact same way you'd do it for any C# class. You derive your own class and customize the base class behavior. Every event has a corresponding OnXxxx() method that you can override.

Add a new class to your project and paste this code:

using System;
using System.Windows.Forms;

class MyButton : Button {
    protected override void OnClick(EventArgs e) {
        // Your code here
        //...
        base.OnClick(e);
    }
}

Change the code in OnClick() to do what you want to do. Compile. You'll now have your own button control on the top of the toolbox. And can drop as many copies of it as you want on a form. They'll all behave the same without having to add any code in the form.

Probably the easiest way would be to have each button invoke the same click handler. Then inside of your handler use the Sender instead of hard coding Button1.

private void buttons_Click(object sender, EventArgs e)
    {
        var theButton = (Button) sender;
        if (theButton.BackColor != Color.Lime)
        {
            theButton.BackColor = Color.Lime;
        }
        else
        {
            theButton.BackColor = Color.White;
        }
    }

You can get the button that raised the Click event by casting sender to Button .

You can then add the same handler to every button.

I'm a VB guy.... in VB.Net you can add multiple handlers for events and connect multiple events to the same handler.

This sub hooks all clicks to color the buttons.

Private Sub ColorButtons(sender As System.Object, e As System.EventArgs) _
Handles Button1.Click, Button2.Click, ..

I do this all the time accidentally because I drag/copy a control to make a new one and the new button gets added to the original's events.

Other Subs can handle the same events to do other work - both will execute.

No idea how to do this in C#.

The proper way to do this really is to associate each button's click event to the function you have coded for that purpose (you want the function to run when the button is clicked, right?), so add the following (or similar) to an appropriate section of your code:

MyButton1.Click += new RoutedEventHandler(buttons_Click);
MyButton2.Click += new RoutedEventHandler(buttons_Click);
etc...

You can associate as many controls to the event handler as you like.

What I usually do before is this:

private void button2_Click(object sender, EventArgs e)
{
    button1.PerformClick();
}

This code will just simply run the codes under button1_Click.

But try not to practice as such and just simply put it in a function/method just like what David suggested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM