Fire MouseClick on TableLayoutPanel?

255 views Asked by At

I have a TableLayoutPanel with some controls added as Label and PictureBox. Now I am trying add a Handler to MouseClick to this TableLayoutPanel. If I do remove these controls Label and PictureBox the Handler is fired right, but with controls doesn't. I tried send these controls go to back, example: Label.SendToBack() but it doesn't works too. I think the problem is with the controls Label and PictureBox because without them works fine.

How could I solve this ?

void initComponents(){
            IList<Mesa> lista = mDAO.findAll();
            if (lista.Count > 0){
                foreach (Mesa mesa in lista){
                    customPanel = new TableLayoutPanel();
                    customPanel.MouseClick += cms_MouseClick; //mouseclick of customPanel
                    customPanel.BorderStyle = BorderStyle.None;
                    customPanel.BackColor = Color.FromArgb(255, 255, 128);
                    customPanel.Size = new Size(100, 100);
                    customPanel.Name = Convert.ToString(mesa.id);

                    //label mesa
                    numMesa = new Label();
                    numMesa.Font = new Font(numMesa.Font.Name, 12, FontStyle.Bold);
                    numMesa.Text = Convert.ToString(mesa.id);
                    numMesa.SendToBack();

                    //picturebox
                    picture = new PictureBox();
                    picture.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
                    picture.Image = IguanaBar.Properties.Resources.mesabar;
                    picture.SizeMode = PictureBoxSizeMode.StretchImage;
                    picture.SendToBack();

                    //label aviso
                    aviso = new Label();
                    aviso.Anchor = (AnchorStyles.Right);
                    aviso.Font = new Font(aviso.Font.Name, 6, FontStyle.Bold);
                    aviso.Text = "LIVRE";
                    aviso.ForeColor = Color.Green;
                    aviso.TextAlign = ContentAlignment.MiddleCenter;
                    picture.SendToBack();

                    //add components
                    customPanel.Controls.Add(numMesa);
                    customPanel.Controls.Add(picture);
                    customPanel.Controls.Add(aviso);

                    //add panel a panel principal                    
                    panelMesas.Controls.Add(customPanel);
                }                
            }            

            //popup menu
            cms = new ContextMenuStrip();
            cms.Items.Add("Vender");
            cms.Items.Add("Finalizar");
            cms.Items.Add("Trocar");
        }
1

There are 1 answers

0
D T On BEST ANSWER

You can change

 numMesa.SendToBack();
 picture.SendToBack();

to

  numMesa.MouseClick += cms_MouseClick; //mouseclick of customPanel
  picture.MouseClick += cms_MouseClick; //mouseclick of customPanel