C# - 将自定义参数传递给事件

时间:2017-12-29 23:21:48

标签: c#

所以我试图向一个事件发送自定义参数,但它从未奏效,我尝试了很多不同的方法,但我从来没有让它工作, 所以基本上!

public void CreateEmojiList()
        {
            CreateAllEmojis();
            int btnCount = 0;

            foreach(Emoji emoji in emojiList)
            {
                Button btnEmoji = new Button();
                btnEmoji.Size = new Size(40, 36);
                btnEmoji.FlatStyle = FlatStyle.Flat;
                btnEmoji.FlatAppearance.MouseDownBackColor = Color.Cyan;
                btnEmoji.Cursor = Cursors.Hand;
                btnEmoji.Font = new Font("Bahnschrift", 6.75f);
                btnEmoji.Text = emoji.EmojiText;
                btnEmoji.Top = (panel_main.Controls.OfType<Button>().Count<Button>() / 4) * (1 + btnEmoji.Height) + 6;
                btnEmoji.Left = (btnEmoji.Width + 1) * btnCount + 6;
                panel_main.Controls.Add(btnEmoji);
                btnEmoji.Click += //What do I do here?
;                btnCount++;

                if (btnCount == 4)
                    btnCount = 0;
            }
        }

        protected virtual void OnEmojiClick(EmojiClickEventArgs e)
        {
            if (this.EmojiClick != null)
                EmojiClick(e);
        }

这是我想用来传递我的论点的类:

public class EmojiClickEventArgs : EventArgs
    {
        private string emojiText;
        private string emojiName;

        public EmojiClickEventArgs(string EmojiText, string EmojiName)
        {
            this.EmojiText = EmojiText;
            this.EmojiName = EmojiName;
        }

        public string EmojiText { get { return emojiText; } set { emojiText = value; } }
        public string EmojiName { get { return emojiName; } set { emojiName = value; } }
    }

我想从中获取这两个值 emoji.EmojiText和emoji.EmojiName

4 个答案:

答案 0 :(得分:1)

一种方法是从Button继承并创建一个名为EmojiButton的类。然后,您声明一个与您的事件处理程序的签名匹配的委托。之后,使用EmojiButton类中的委托声明一个事件,同时向按钮子类添加EmojiTextEmojiName等属性。最后,您需要将按钮单击事件与自定义事件相关联。只要点击按钮,就举起你的活动并传递你的论据,this.EmojiText, this.EmojiName

另一种方法是将Emoji对象分配给Tag属性。然后,您可以使用正常的EventHandler签名(object sender, EventArgs e)编写事件处理程序,并查看sender的{​​{1}}是什么。然后,您将Tag转换为Tag并访问其属性。

答案 1 :(得分:1)

您可以利用闭包来“打包”每个按钮的事件处理程序的附加事件数据。请确保不要close over the loop variable.

    public void CreateEmojiList()
    {
        CreateAllEmojis();
        int btnCount = 0;

        foreach(Emoji emoji in emojiList)
        {
            Button btnEmoji = new Button();
            btnEmoji.Size = new Size(40, 36);
            btnEmoji.FlatStyle = FlatStyle.Flat;
            btnEmoji.FlatAppearance.MouseDownBackColor = Color.Cyan;
            btnEmoji.Cursor = Cursors.Hand;
            btnEmoji.Font = new Font("Bahnschrift", 6.75f);
            btnEmoji.Text = emoji.EmojiText;
            btnEmoji.Top = (panel_main.Controls.OfType<Button>().Count<Button>() / 4) * (1 + btnEmoji.Height) + 6;
            btnEmoji.Left = (btnEmoji.Width + 1) * btnCount + 6;
            panel_main.Controls.Add(btnEmoji);
            var emojiCopy = emoji; //don't close on the loop variable!
            btnEmoji.Click += (sender,args) => OnEmojiClick(emojiCopy);
            btnCount++;

            if (btnCount == 4)
                btnCount = 0;
        }
    }

    protected virtual void OnEmojiClick(Emoji emoji)
    {
        //do something
    }

答案 2 :(得分:0)

我想到的最快的解决方案并不意味着自定义UserControl类的定义,Button的子类化以及其他类似的实践:

btnEmoji.Click += (sender, e) =>
{
    Button b = (Button)sender;

    // let's suppose that the button name corresponds to the emoji name
    String emojiName = b.Name; 
    // let's suppose that the button tag contains the emoji text
    String emojiText = (String)b.Tag;

    Emoji_Clicked(sender, e, (new EmojiClickEventArgs(emojiText, emojiName)));
};

private void Emoji_Clicked(Object sender, EventArgs e, EmojiClickEventArgs ee)
{
    // Your code...
}

答案 3 :(得分:0)

首先需要定义委托,然后创建一个实例。

class Emojis
{
       // public delegate void EmojiClickEventHandler(object sender,EventArgs args);
       //public event EmojiEventHandler EmojiClicked;
       //you can use above two lines or replace them instead below code.
        public event EventHandler<EmojiClickEventArgs> EmojiClicked;
        public void CreateEmojiList()
         {
             CreateAllEmojis();
             int btnCount = 0;
             //rest of the code
            panel_main.Controls.Add(btnEmoji);
            btnEmoji.Click += OnEmojiClick(btnEmoji);
            btnCount++;
        }

   protected virtual void OnEmojiClick(Button emoji)
   {
    //Here null check to handle if no subscribers for the event
    if(EmojiClicked!=null)
       {
           //there is no name property define for emoji but only text hence passing only text.
          EmojiClicked(this ,new  EmojiClickEventArgs(emoji.Text,emoji.Text){ });
       }
   }
   private void Emoji_Clicked(Object sender, EmojiClickEventArgs args)
        {
            Button mybutton = sender as Button;
            Console.WriteLine("emoji text "+ args.Text);
        }

}