在 TableLayoutPanel 上触发 MouseClick?
Fire MouseClick on TableLayoutPanel?
我有一个 TableLayoutPanel,其中添加了一些控件作为 Label 和 PictureBox。现在我正在尝试将一个处理程序添加到此 TableLayoutPanel 的 MouseClick。如果我确实删除了这些控件 Label 和 PictureBox,则 Handler 会被正确触发,但控件不会。我尝试将这些控件发送到后面,例如:Label.SendToBack()
但它也不起作用。我认为问题出在控件 Label 和 PictureBox 上,因为没有它们工作正常。
我该如何解决这个问题?
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");
}
你可以改变
numMesa.SendToBack();
picture.SendToBack();
到
numMesa.MouseClick += cms_MouseClick; //mouseclick of customPanel
picture.MouseClick += cms_MouseClick; //mouseclick of customPanel
我有一个 TableLayoutPanel,其中添加了一些控件作为 Label 和 PictureBox。现在我正在尝试将一个处理程序添加到此 TableLayoutPanel 的 MouseClick。如果我确实删除了这些控件 Label 和 PictureBox,则 Handler 会被正确触发,但控件不会。我尝试将这些控件发送到后面,例如:Label.SendToBack()
但它也不起作用。我认为问题出在控件 Label 和 PictureBox 上,因为没有它们工作正常。
我该如何解决这个问题?
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");
}
你可以改变
numMesa.SendToBack();
picture.SendToBack();
到
numMesa.MouseClick += cms_MouseClick; //mouseclick of customPanel
picture.MouseClick += cms_MouseClick; //mouseclick of customPanel