CDamsinghe
CDamsinghe

Reputation: 39

How to change a button's properties from another class?

I have a four buttons, and each of them can be of three possible colours(backgorund):yellow, green, blue.

I have a method in another class from which I want to change the color of these buttons. This method will be called on a MouseUp event on the button, engineered for a right click.

These are the actionlisteners for the buttons:

private void RightSeat_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            TableModel.ColorChange(2);
        }
    }

    private void BottomSeat_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            TableModel.ColorChange(3);
        }
    }

    private void LeftSeat_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            TableModel.ColorChange(4);
        }
    }

    private void TopSeat_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            TableModel.ColorChange(1);
        }
    }

This is where the buttons are created(the designer code):

public void InitializeComponent()
    {
        this.LeftSeat = new System.Windows.Forms.Button();
        this.RightSeat = new System.Windows.Forms.Button();
        this.BottomSeat = new System.Windows.Forms.Button();
        this.TopSeat = new System.Windows.Forms.Button();
//other generated code
}

public System.Windows.Forms.Button LeftSeat;
public System.Windows.Forms.Button RightSeat;
public System.Windows.Forms.Button BottomSeat;
public System.Windows.Forms.Button TopSeat;

This is the code in the second class.

public static void ColorChange(int btn)
    {
        TableView tw = new TableView();
        switch (btn)
        {
            case 1:
                tw.TopSeat.BackColor = Color.Yellow;
                break;
            case 2:
                tw.RightSeat.BackColor = Color.Yellow;
                break;
            case 3: 
                tw.BottomSeat.BackColor = Color.Yellow;
                break;
            case 4:
                tw.LeftSeat.BackColor = Color.Yellow;
                break;
            default:
                break;
        }
    }

However, when I use this method, nothing happens in the application. No error of any type appears. The code works if I use a message box to see if the switch case can handle the parameter, but the color changing doesn't work.

Upvotes: 3

Views: 1728

Answers (2)

uowzd01
uowzd01

Reputation: 1000

You need to find the existing forms "TableView"

Button btn = Application.OpenForms["TableView "].Controls["LeftSeat"] as Button;
btn.BackColor = Color.Yellow

Upvotes: 0

User2012384
User2012384

Reputation: 4919

You can also pass the button to the method using "ref"

public static void ColorChange(int btn, ref System.Windows.Forms.Button buttonToChange)
    {
        switch (btn)
        {
            case 1:
                buttonToChange.BackColor = Color.Yellow;
                break;
            case 2:
                buttonToChange.BackColor = Color.Yellow;
                break;
            case 3: 
                buttonToChange.BackColor = Color.Yellow;
                break;
            case 4:
                buttonToChange.BackColor = Color.Yellow;
                break;
            default:
                break;
        }
    }

Then you can call this method using:

 private void TopSeat_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            TableModel.ColorChange(1, ref TopSeat);
        }
    }

Upvotes: 1

Related Questions