How to change the background color of several buttons at once in windows forms?

1

The thing is like this: I have two forms, Form1 and Form2 .

Form1 is the main one and has 20 buttons which each represents a different table of my restaurant. Clicking on any of them opens a Form2 in which you can register orders, etc ... Each form2 also contains a Title button Abrir Mesa which I need that when clicked I change the color of button background MESA 1 in Form1 . If the button Abrir mesa of MESA 2 is clicked, then I need it to be THIS and not the MESA 1 that changes color, and so on.

I have created this that works for me only for the first button, that is to say the MESA1 but I do not manage to achieve something that is for everyone. I leave some code to see if someone can help me. Thank you very much!

   // FORM 1

  public void btnMesa1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2(this);
        f2.Text = "Mesa 1";
        f2.Show();
    }

    private void btnMesa2_MouseClick(object sender, MouseEventArgs e)
    {
        Form2 f2 = new Form2(this);
        f2.Text = "Mesa 2";
        f2.Show();
    }

    public void cambiarFondo()
    {
        btnMesa1.BackColor = Color.Green;
    }


       // FORM 2



    public partial class Form2 : Form
{
    double precio = 0;
    double cantidad = 0;
    double subtotal = 0;
    double total = 0;
    Form1 f1 = new Form1();

    public Form2()
    {

    }

    public Form2(Form1 parametro)
    {
        InitializeComponent();
        f1 = parametro;
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        DataGridViewButtonColumn colBotones = new DataGridViewButtonColumn();
        colBotones.Name = "colBotones";
        colBotones.HeaderText = "BORRAR";
        this.Pagos.Columns.Add(colBotones);
    }

    private void txtCantidad_TextChanged(object sender, EventArgs e)
    {
        precio = Convert.ToDouble(txtPrecio.Text);
        cantidad = Convert.ToDouble(txtCantidad.Text);
        subtotal = precio * cantidad;
        etiquetaSubTotalNum.Text = subtotal.ToString();
    }

    // AGREGA UN PRODUCTO
    private void btnAgregarItem_Click(object sender, EventArgs e)
    {
        Pagos.Rows.Add(txtID.Text, txtNombre.Text, txtPrecio.Text, txtCantidad.Text, etiquetaSubTotalNum.Text);

    }

    // CALCULA EL VALOR TOTAL DE LA MESA E IMPRIME EL TICKET
    private void btnTicket_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Pagos.Rows.Count; i++)
        {
            total += double.Parse(Pagos.Rows[i].Cells["celdaSubtotal"].Value.ToString());
        }

        etiquetaTotalNum.Text = total.ToString();
        total = 0;
    }

    // CIERRA LA MESA
    private void btnCerrarMesa_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    // DIBUJA "BORRAR" SOBRE LOS BOTONES DE ELIMINAR
    private void Pagos_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        if (e.ColumnIndex >= 0 && this.Pagos.Columns[e.ColumnIndex].Name == "colBotones" && e.RowIndex >= 0)
        {
            e.Paint(e.CellBounds, DataGridViewPaintParts.All);

            DataGridViewButtonCell celBoton = this.Pagos.Rows[e.RowIndex].Cells["colBotones"] as DataGridViewButtonCell;

            e.Graphics.DrawString("BORRAR", new Font("Verdana", 8), new SolidBrush(Color.Red),
            e.CellBounds.Left + 3, e.CellBounds.Top + 3);

            e.Handled = true;
        }
    }

    // ELIMINA LA FILA DE ESE PRODUCTO AL HACER CLICK EN EL BOTON "BORRAR"
    private void Pagos_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (this.Pagos.Columns[e.ColumnIndex].Name == "colBotones")
        {
            Pagos.Rows.RemoveAt(Pagos.CurrentRow.Index);
        }
    }

    // CAMBIA EL BOTON DE LA MESA A COLOR VERDE/ABIERTA
    private void btnAbrirMesa_Click(object sender, EventArgs e)
    {
        this.btnAbrirMesa.BackColor = Color.Green;
        f1.cambiarFondo();
    }
    
asked by Nacho Zve De La Torre 28.05.2018 в 03:05
source

1 answer

2

A very simple solution is to change your cambiarFondo method so that it receives the name of the button that should change color, and look for that button by that name. Something like this:

public void cambiarFondo(string boton)
{
    var btn = this.Controls.OfType<Button>().Where(x=> x.Name == boton).FirstOrDefault();
    if (btn != null)
    {
         btn.BackColor = Color.Green;
    }
}

Then, in your Form2 , change the call to send the name of the button to change:

private void btnAbrirMesa_Click(object sender, EventArgs e)
{
    this.btnAbrirMesa.BackColor = Color.Green;
    //no pones suficientes datos sobre como sabes que botón se ha pulsado
    //imagino que debes comprobar en el 'sender' para saber que mesa es
    //y obtener "indicemesa"
    f1.cambiarFondo("btnMesa" + indiceMesa);
}

It is not the best solution, I would probably choose to use custom events and each button will listen to the event to know that it should change color, but with this solution I think it can help you.

An important note. In your Form2 you have the following line:

Form1 f1 = new Form1();

This is creating a new instance of Form1 that is not used at all. Change it simply by Form f1; , since then you store the reference to the instance.

    
answered by 28.05.2018 / 09:56
source