简体   繁体   English

如何创建由三个单独的按钮组成的自定义图像按钮并添加事件处理程序

[英]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.

相关问题 如何使用按钮创建自定义控件以及如何在 silverlight 中为 windows mobile 7 添加事件按钮单击事件 - How to create custom control with buttons and how to add event button click event in silverlight for windows mobile 7 如何动态创建按钮并添加不同的事件处理程序? - How to dynamically create buttons and to add different event handler? 如何为多个按钮创建事件处理程序? - How to create an event handler for multiple buttons? 如何使用for循环为多个按钮创建事件处理程序? - How to create an event handler for multiple buttons using a for loop? 如何使5x5按钮数组中的每个按钮/复选框具有不同的事件处理程序? - How do i make each individual button/checkbox in a 5x5 button array have a different event handler? 不使用动态创建的按钮触发按钮处理程序事件 - Button handler event not firing with dynamically created buttons 如何将Button_Click事件处理程序实现到自定义控件? - How to implement Button_Click event handler to a custom control? 如何为事件处理程序创建事件处理程序? - How to create a event handler for event handler? 如何使用AddHandler在WPF中的按钮上为LostKeyBoardFocusEvent添加事件处理程序 - How to use AddHandler to add event handler for a LostKeyBoardFocusEvent on a button in WPF 如何创建asp.net自定义Web控件事件处理程序 - How to create asp.net Custom web controls event handler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM