hamdi islam
hamdi islam

Reputation: 1521

Change button colour from different form

I'm really new to c# and I'm doing this coursework which is about making a waiter app using visual studio.

The user presses on the seat number which is the button I want to change the colour of and opens a new form to place the order where the user chooses meals and submits the order. Upon submission I want the seat colour on the main form to change to yellow, which indicates that the order has been placed on that seat number.

Here's how the seat button looks in the main form

public void button_clicked(object sender,EventArgs e)
{
    button seatButton = (Button)sender;
    string seat = seatButton.text;
    placeOrder po = new placeOrder(seat);
    po.showDialog();
}

and here's how the submit order button looks in the submit order form

private void submitOrder_clicked(object sender,EventArgs e)
{
    if (listBoxMeals.Items.Count != 0)
       {
          alertbox.text = "You have placed your order successfully";

          // how to change seat button colour ?
       }
    else
       {
          alertbox.text = "Your meals list is empty";
       }
}

here is a picture of the forms

Upvotes: 1

Views: 182

Answers (2)

akkwake
akkwake

Reputation: 31

Assuming you're going to use this for more than one button, you can create a static reference to the button you just clicked.

public static Button CurrentButton;
public void button_clicked(object sender,EventArgs e)
{
    button seatButton = (Button)sender;
    string seat = seatButton.text;
    placeOrder po = new placeOrder(seat);
    CurrentButton = button;
    po.showDialog();
}

Then simply add the following line wherever you want to change the button's color:

    MainForm.CurrentButton.BackColor = Color.Yellow;

Upvotes: 0

Rai Vu
Rai Vu

Reputation: 1635

You can do it this way:

  1. Change the visibility of the button to Public in frmMain.

  2. In the submitOrder_clicked() event of frmChild1 do:

    (this.MdiParent as frmMain).btnChild.BackColor="RequiredColor";
    

Upvotes: 1

Related Questions