如何创建一个自定义图像按钮,由三个单独的按钮组成并添加事件处理程序
How to create a Custom Image Button, made of three individual buttons and add event handler
我想制作一个 Custom Image Button
,它应该由三个按钮组成,并附有 click event
。
我试图制作一个 Customize button
,但无法附加一个单独的 event handler
。
对于每个按钮:
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);
}
}
添加事件处理程序:
MulitButtons objMltBtn = new MulitButtons();
EventHandler handler= new EventHandler(but1_Click);
objMltBtn.Click += handler;
请帮帮我。
谢谢。
如果我理解得很好,你想发布你内部按钮的 Click
事件。
解决方案 1
这只是发布内部点击事件。
public event EventHandler Button1Click
{
add { button1.Click += value; }
remove { button1.Click -= value; }
}
解决方案 2
如果您想控制调用,请这样做:
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();
}
更新:刚刚看到你的评论:
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.
只需创建三个彼此相邻的单独按钮,然后将它们绘制在 Paint
中,就好像它们是一个按钮一样。然后你可以使用上面的解决方案之一。
我想制作一个 Custom Image Button
,它应该由三个按钮组成,并附有 click event
。
我试图制作一个 Customize button
,但无法附加一个单独的 event handler
。
对于每个按钮:
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);
}
}
添加事件处理程序:
MulitButtons objMltBtn = new MulitButtons();
EventHandler handler= new EventHandler(but1_Click);
objMltBtn.Click += handler;
请帮帮我。 谢谢。
如果我理解得很好,你想发布你内部按钮的 Click
事件。
解决方案 1
这只是发布内部点击事件。
public event EventHandler Button1Click
{
add { button1.Click += value; }
remove { button1.Click -= value; }
}
解决方案 2
如果您想控制调用,请这样做:
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();
}
更新:刚刚看到你的评论:
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.
只需创建三个彼此相邻的单独按钮,然后将它们绘制在 Paint
中,就好像它们是一个按钮一样。然后你可以使用上面的解决方案之一。