带有类似按钮的项目的面板 [英] Panels with items acting like a button
问题描述
我有一些 Panel
,每个 Panel
都有一个 PictureBox
和一个 Label
.
public static void redPanel(Panel panel1){panel1.MouseEnter += new EventHandler((o, a) => panel1.BackColor = Color.Brown);panel1.MouseDown += new MouseEventHandler((o, a) => panel1.BackColor = Color.DarkRed);panel1.MouseUp += new MouseEventHandler((o, a) => panel1.BackColor = Color.Firebrick);panel1.MouseLeave += new EventHandler((o, a) => panel1.BackColor = Color.Firebrick);}
我编写了此代码,以便面板对鼠标悬停和单击做出反应...但是如果我将 Label
或 PictureBox
悬停在 Panel
> 恢复到原来的颜色.我希望 Label
和 PictureBox
的行为与 Panel
完全一样(例如:如果我单击 Label
我希望将其计为对 Panel
的点击).
我想要一些代码......比如 SourceControl
或者从顶部的函数来管理它自己的 Label
和 PicureBox
.>
PS:我试图为 Label
和 PictureBox
制作另一个函数,但每个 Panel
都有不同的颜色,所以这意味着我需要有大约 23 行代码仅用于标签......
快速回答:只需将相同的处理程序附加到所有这些.
public static void redPanel(Panel panel1){var mouseEnter = new EventHandler((o, a) => panel1.BackColor = Color.Brown);panel1.MouseEnter += mouseEnter;pictureBox1.MouseEnter += mouseEnter;label1.MouseEnter += mouseEnter;//等等}
更好的答案:改变你的代码,如果它是一个可重用的组件,你最好创建一个带有 PictureBox
和 UserControl
的 UserControl
代码>标签代码>.然后从设计器将相同的事件处理程序分配给从它们生成的事件(加上 UserControl
本身).
也就是说一个按钮远不止这些(它必须对键盘事件做出反应,可以聚焦,可以是默认按钮等等).我更愿意从按钮派生出来只添加绘图功能(标签和图像是很少的代码).
I have some Panel
's and each Panel
has a PictureBox
and a Label
in it.
public static void redPanel(Panel panel1)
{
panel1.MouseEnter += new EventHandler((o, a) => panel1.BackColor = Color.Brown);
panel1.MouseDown += new MouseEventHandler((o, a) => panel1.BackColor = Color.DarkRed);
panel1.MouseUp += new MouseEventHandler((o, a) => panel1.BackColor = Color.Firebrick);
panel1.MouseLeave += new EventHandler((o, a) => panel1.BackColor = Color.Firebrick);
}
I made this code so the panel reacts to mouse hover and click ... but if I hover the Label
or the PictureBox
the Panel
reverts to its original color. I want that the Label
and the PictureBox
to act exactly like the Panel
(ex: If I click the Label
I want that to count as a click on the Panel
).
I want some code ... like SourceControl
or something for the function from top to manage its own Label
and PicureBox
.
P.S: I tried to make another functions for Label
and PictureBox
but each Panel
has different color so It means that I need to have about 23 lines of code just for labels ...
Quick answer: just attach the same handler to all of them.
public static void redPanel(Panel panel1)
{
var mouseEnter = new EventHandler((o, a) => panel1.BackColor = Color.Brown);
panel1.MouseEnter += mouseEnter;
pictureBox1.MouseEnter += mouseEnter;
label1.MouseEnter += mouseEnter;
// And so on
}
Better answer: change your code, if it's a reusable component you'd better to create a UserControl
with a PictureBox
and a Label
. From Designer then assign the same event handler to events generated from both of them (plus UserControl
itself).
That said a button is much more than that (it has to react to keyboard events, can be focused, can be default button and so on). I'd prefer to derive from button to just add drawing features (a label and an image are very little code).
这篇关于带有类似按钮的项目的面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!