[英]How to create a Custom Image Button, made of three individual buttons and add event handler
I want to make a Custom Image Button
which should be consist of three buttons with attached click event
. 我要制作一个“ Custom Image Button
,该Custom Image Button
应由三个带有附加click event
按钮组成。
I tried to make a Customize button
but I couldn't able attached a separate event handler
. 我试图制作一个“ Customize button
但无法附加单独的event handler
。
for each button: 对于每个按钮:
class MulitButtons : UserControl
{
public Color bckColor1 = Color.Blue;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics graphics = e.Graphics;
RectangleF recF1 = new RectangleF(0, 0, 100, 40);
RectangleF recF2 = new RectangleF(100, 0, 100, 40);
RectangleF recF3 = new RectangleF(200, 0, 100, 40);
RectangleF[] arrRecF = { recF1, recF2, recF3 };
Pen pen = new Pen(Color.Black, 2);
int fontHeight = 10;
Font font = new Font("Arial", fontHeight);
SolidBrush brush = new SolidBrush(bckColor1);
SolidBrush textBrush = new SolidBrush(Color.Black);
graphics.DrawRectangles(pen, arrRecF);
graphics.DrawString(Text, font, textBrush, 10, 10);
}
}
Adding event handler: 添加事件处理程序:
MulitButtons objMltBtn = new MulitButtons();
EventHandler handler= new EventHandler(but1_Click);
objMltBtn.Click += handler;
Please help me out. 请帮帮我。 Thanks. 谢谢。
If I understand well, you want to publish the Click
events of your inner buttons. 据我了解,您想发布内部按钮的Click
事件。
Solution 1 解决方案1
This simply publishes the inner click event. 这只是发布内部点击事件。
public event EventHandler Button1Click
{
add { button1.Click += value; }
remove { button1.Click -= value; }
}
Solution 2 解决方案2
If you want some control over the invoking, do it like this: 如果要控制调用,请执行以下操作:
public MultiButtons()
{
InitializeComponents();
button1.Click += ButtonClick;
button2.Click += ButtonClick;
button3.Click += ButtonClick;
}
// this handles all of your clicks
private void ButtonClick(object sender, EventArgs e)
{
if (sender == button1)
OnButton1Click(EventArgs.Empty);
// TODO: the other buttons...
}
public event EventHandler Button1Click;
protected virtual void OnButton1Click(EventArgs e)
{
var handler = Button1Click;
if (handler != null)
handler(this, e);
else
// you can call some default action if there is no event subscription
DefaultButton1Click();
}
Update: I have just seen your comment: 更新:我刚刚看到您的评论:
I need a custom Image button with Three sections and on each section click i want to invoke an event which will perform some operation. 我需要一个具有三个部分的自定义图像按钮,然后在每个部分上单击以调用一个将执行某些操作的事件。
Just create three separate buttons next to each other and draw them in the Paint
as if they were a single button. 只需创建三个彼此相邻的单独按钮,然后在“ Paint
绘制它们,就好像它们是单个按钮一样。 Then you can use one of the solutions above. 然后,您可以使用上述解决方案之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.