开发者

Buttons in a Visual Studios form application

Sorry if this is a dumb question, I'm taking an intro to progr开发者_C百科amming class and need a bit of help with this project I'm working on.

I'm trying to write an application that has about 30 buttons. One common thing I want is for all the buttons to turn yellow when clicked. If they're clicked a second time, they change back to the default color. right now I use the code:

private void btn_1_Click(object sender, EventArgs e)
{
    btn_1.BackColor = Color.Yellow;
}

But that only turns the buttons yellow, I can't turn them "off" by clicking it a second time.

Also, when I'm creating these button events in VS2010, I end up with 30 different event handlers for each button..Is there a way to get them all to do the same thing without having to write all the repetitive code?

I'm guessing that I would have to write my own buttons class? How would I go about doing that? Do i need to create a class library which inherits Buttons?

Sorry for the noob questions. THanks


If every button has a specific action that needs to be performed, then yes, you need to have a click handler for each; however, you can encapsulate the common behavior in a single method.

For example:

private void btn_1_Click(object sender, EventArgs e)
{
    ToggleColor((Button)sender);
    //rest of the code specific to this button
}

private void ToggleColor (Button button)
{
     if(button.Color==Color.Yellow;
         button.Color=Color.Black;
     else 
         button.Color=Color.Yellow;
}

Note that above code is not tested.

Now, if all the buttons do the same thing, you can just set the on click handlers for all of them to be btn_1_Click; for example.


private void btn_1_Click(object sender, EventArgs e)
{
    if (btn_1.BackColor != Color.Yellow)
    {
        btn_1.BackColor = Color.Yellow
    }
    else
    {
        btn_1.BackColor = Color.Control;
    }
}

this is switching default and yellow


If all buttons do the exact same thing you can assign the same event handler to all buttons (instead of btn_1_Click, btn_2_Click etc... you'd have btton_click) - you can select this handler in the properties of each button.


You don't have to write your own class. You can simply assign all your buttons to the same event handler, like this:

button1.Click += new EventHandler(myEventHandler);
button2.Click += new EventHandler(myEventHandler);
button3.Click += new EventHandler(myEventHandler);
button4.Click += new EventHandler(myEventHandler);

Just keep in mind that your event handler has this signature:

private void myEventHandler(object sender, EventArgs e)

By doing that, all your buttons, when clicked, will trigger the same method.

Now to control the color, what you can do is create a simple property on your form which would hold the last color applied. It could be an enum, then you could simply check its value and apply the other one to the buttons, like this:

// Declare your enum:
private enum Colors { Yellow, Default }

private Colors ActualColor = Colors.Default;


// Write your custom event handler:
private void myEventHandler(object sender, EventArgs e)
{
    if (ActualColor == Colors.Default)
    {
        // Apply yellow to buttons
        ActualColor = Colors.Yellow;
    }
    else
    {
        // Apply default
        ActualColor = Colors.Default;
    }            
}


In order to keep track whether it is the 'second time' you press the button, you should declare a variable OUTSIDE the method, which indicates whether you already pressed the button or not.

For example:

public bool IsButtonYellow;
private void btn_1_Click(object sender, EventArgs e) {
    if(!IsButtonYellow) {
    btn.BackColor = Color.Yellow;
    IsButtonYellow = true;
    }
    else {
    btn.BackColor = Control.DefaultBackColor;
    IsButtonYellow = false;
    }
}


Yes:

  1. Create your own button class
  2. Inherit from Button
  3. Implement the handler in your button class and you're done

You can do something simple like this:

public class MyButton : Button
{
    private bool _buttonState;

    protected override void OnClick(EventArgs e)
    {
        base.OnClick(e);

        if (_buttonState)
        {
            BackColor = Color.Yellow;
        }
        else
        {
            BackColor = Color.White;
        }
    }
}

Then in your code you can just create as many of these "MyButton" objects as you need, with no code repetition.


To make all buttons use the same event handler in VS2010:

Click once on a button to select it.

In the “properties” window: click on the “lightning” (=events).

Paste the first button’s event name (btn_1_Click) next to “Click”.

Do the same for every button.

As for changing the color: See answer by killie01.

Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜