The thing is like this: I have two forms, Form1
and Form2
.
Form1
It is the main one and it has 20 buttons, each one representing a different table in my restaurant. Clicking on any of them opens a page Form2
in which orders can be registered, etc... Each form2
also contains a Title button Abrir Mesa
which I need, when clicked, to change the background color of the button MESA 1
in the Form1
. If the button Abrir mesa
is MESA 2
clicked, then I need it to be THIS and not the MESA 1
one to change color, and so on.
I've created this that works for me only for the first button, that is, MESA1
but I can't find something that works for everyone. I leave some code to see if someone can help me. From already 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();
}
A very simple solution is that you change your method
cambiarFondo
so that it receives the name of the button that should change color, and looks for that button by said name. Something like that:Then, in your
Form2
, change the call so that it sends the name of the button to change:It is not the best solution, I would probably choose to use custom events and have each button listen to the event to know that it should change color, but with this solution I think it can work for you.
An important note. In your
Form2
you have the following line:This is creating a new instance of
Form1
that is not used for anything. Simply change it toForm f1;
, since you then store the reference to the instance.