How can I change the properties of a button from another class?

I have four buttons, and each of them can have three possible colors (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 in the MouseUp event on the button designed for right-clicking.

These are the executors for 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 (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 second class code.

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 appears. The code works if I use a message box to see if the switch register can handle the parameter, but changing the color doesn't work.

+3


source to share


2 answers


You can also pass a button to a 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);
        }
    }

      

+1


source


You need to find existing "TableView" forms



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

      

0


source







All Articles