开发者

User control and how to get button events from form

开发者 https://www.devze.com 2023-04-05 04:23 出处:网络
i created user control which looks like this: and code behind looks like: using System; using System.Windows.Forms;

i created user control which looks like this:

User control and how to get button events from form

and code behind looks like:

using System;
using System.Windows.Forms;

namespace Controlls
{
    public partial class Toolbar : UserControl
    {
        public Toolbar()
        {
            InitializeComponent();
        }

        private void pbNaprej_Click(object sender, EventArgs e)
        {

        }

        private void pbNazaj_Click(object sender, EventArgs e)
        {

        }

        private void pbNovo_Click(object sender, EventArgs e)
        {

        }

        private void bpbBrisi_Click(object sender, EventArgs e)
        {

        }

        private void pbPotrdi_Click(object sender, EventArgs e)
        {开发者_JS百科

        }

        private void txtOd_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

now i move this UC to form but there is a problem. How to get this events from UC. I need to implement click events for buttons in form where i use this user control. What i can see is that i can use it like one component and i can not get separate buttons from it.


Add 2 event handlers for yor control, and subscribe to the toobox event at the place you use it:

using System; 
using System.Windows.Forms; 

namespace Controlls 
{ 
    public enum ToolboxButtonType { Potrdi, Brisi, Novo, Nazaj, Naprej } 
    public partial class Toolbar : UserControl 
    { 
        public Toolbar() 
        { 
            InitializeComponent(); 
        } 

        private void pbNaprej_Click(object sender, EventArgs e) 
        { 
            OnToolboxClick(ToolboxButtonType.Naprej);
        } 

        private void pbNazaj_Click(object sender, EventArgs e) 
        { 
            OnToolboxClick(ToolboxButtonType.Nazaj);
        } 

        private void pbNovo_Click(object sender, EventArgs e) 
        { 
            OnToolboxClick(ToolboxButtonType.Novo);
        } 

        private void bpbBrisi_Click(object sender, EventArgs e) 
        { 
            OnToolboxClick(ToolboxButtonType.Brisi);
        } 

        private void pbPotrdi_Click(object sender, EventArgs e) 
        { 
             OnToolboxClick(ToolboxButtonType.Potrdi);
        } 

        private void txtOd_TextChanged(object sender, EventArgs e) 
        { 
             OnTextChanged(txtOd.Text);
        } 
    }
    private OnToolboxClick(ToolboxButtonType button)
    {
        if (ToolboxClick != null)
        {
            ToolboxClick(this, new ToolboxClickEventArgs(button));
        }
    }
    private OnTextChanged(string text)
    {
        if (ToolboxTextChanged!= null)
        {
            ToolboxTextChanged(this, new ToolboxTextChangedEventArgs(text));
        }
    }
    public class ToolboxTextChangedEventArgs: EventArgs
    {
        public string Text { get; private set; }
        public ToolboxClickEventArgs(string text) { Text = text; }
    }
    public class ToolboxClickEventArgs : EventArgs
    {
        public ToolboxButtonType Button { get; private set; }
        public ToolboxClickEventArgs(ToolboxButtonType button) { Button = button; }
    }
    public event EventHandler<ToolboxClickEventArgs> ToolboxClick;
    public event EventHandler<ToolboxTextChangedEventArgs> ToolboxTextChanged;
} 

For example:

//Toolbar toolbar = new Toolbar();
toolbar.ToolboxTextChanged += (s, e) => { btn1.Text = e.Text; };
toolbar.ToolboxClick += (s, e) => 
{
    swith(e.Button)
    {
        case ToolboxButtonType.Brisi: //
            break;
        default:
            break;
    }
};


You need to create event in MainForm class and generarate it in hand mode.

If you also draw your icons on canvas or smth, you need to handle onClick your MainForm, than calculate mouse click position, and generate event that you were create.

If it amount of buttons or smth that has their own onClick events, you can hang on this buttons event the action that will generate your even in main form.

Answer what approach you use, and I past some code.

ADDED:

public partial class Toolbar : UserControl
{
    public Toolbar()
    {
        InitializeComponent();
    }

    public enum ToolBarCommands
    {
        Naprej, Nazaj, Novo
    }

    public Action<object, ToolBarCommands> MenuItemClick;

    private void pbNaprej_Click(object sender, EventArgs e)
    {
        if(MenuItemClick != null)
            MenuItemClick(this, ToolBarCommands.Naprej);             
    }

    private void pbNazaj_Click(object sender, EventArgs e)
    {
        if(MenuItemClick != null)
            MenuItemClick(this, ToolBarCommands.Nazaj);             
    }

    private void pbNovo_Click(object sender, EventArgs e)
    {
        if(MenuItemClick != null)
            MenuItemClick(this, ToolBarCommands.Novo);  
    }
}

How to use it? easy:

// Define section
var toolbar = new Toolbar();
toolbar.MenuItemClick += MenuItemClickHandler;

...

MenuItemClickHandler(object sender, Toolbar.ToolBarCommands item)
{
    switch(item)
    {
        case Toolbar.ToolBarCommands.Naprej:
            // Naperej handler
        break;

        case Toolbar.ToolBarCommands.Nazaj:
            // Nazaj handler
        break;

        case Toolbar.ToolBarCommands.Novo:
            // Novo handler
        break;
    }
}

ADDED:

If names of your buttons same as enum members you can cut this part of code:

    private void pbNaprej_Click(object sender, EventArgs e)
    {
        if(MenuItemClick != null)
            MenuItemClick(this, ToolBarCommands.Naprej);             
    }

    private void pbNazaj_Click(object sender, EventArgs e)
    {
        if(MenuItemClick != null)
            MenuItemClick(this, ToolBarCommands.Nazaj);             
    }

    private void pbNovo_Click(object sender, EventArgs e)
    {
        if(MenuItemClick != null)
            MenuItemClick(this, ToolBarCommands.Novo);  
    }

It will look's like one method (hang this method to all your menu element events)

    private void pbItem_Click(object sender, EventArgs e)
    {
        if(MenuItemClick != null)
        {
            ToolBarCommands command;
            var res = ToolBarCommands.TryParse(((Button)sender).Name, command)
            if(res == true)                
                MenuItemClick(this, command);
        }             
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号