简体   繁体   English

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

[英]C# - Passing custom arguments to events

So I was trying to send custom arguments to an event, but it never worked, I tried so many different methods, but I never got it to work, So basically! 因此,我试图向事件发送自定义参数,但是它从未起作用,我尝试了许多不同的方法,但是我却从未使其起作用,所以基本上!

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);
        }

this is the class I want to use to pass my arguments: 这是我想用来传递参数的类:

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; } }
    }

I want to get those two values from emoji.EmojiText and emoji.EmojiName 我想从emoji.EmojiText和emoji.EmojiName中获得这两个值

One way is to inherit from Button and create a class called EmojiButton . 一种方法是从Button继承并创建一个名为EmojiButton的类。 You then declare a delegate that matches the signature of the your event handler. 然后,您声明一个与您的事件处理程序的签名匹配的委托。 After that, declare an event using the delegate in the EmojiButton class, add property like EmojiText and EmojiName to the button subclass as well. 之后,使用EmojiButton类中的委托声明一个事件,并将EmojiTextEmojiName之类的属性也添加到按钮子类中。 Finally you need to link the button click event with your custom event. 最后,您需要将按钮单击事件与自定义事件链接。 Whenever the button is clicked, raise your event and pass your arguments ie this.EmojiText, this.EmojiName . 每当单击按钮时,引发事件并传递参数,即this.EmojiText, this.EmojiName

Another way is to assign your Emoji objects to the Tag property. 另一种方法是将Emoji对象分配给Tag属性。 You can then write the event handler with the normal EventHandler signature ( object sender, EventArgs e ), and look at what the sender 's Tag is. 然后,您可以使用普通的EventHandler签名( object sender, EventArgs e )编写事件处理程序,并查看senderTag是什么。 You then cast the Tag to an Emoji and access its properties. 然后,您可以将Tag投放到Emoji并访问其属性。

You can take advantage of closures to "package up" the additional event data for each button's event handler. 您可以利用闭包为每个按钮的事件处理程序“打包”其他事件数据。 Just make sure not to 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
    }

The fastest solution that comes to my mind and that doesn't imply the definition of custom UserControl classes, the subclassing of Button and other similar practices is: 我想到的最快的解决方案是,它不暗示自定义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...
}

First and foremost thing you need to define delegate ,then create an instance. 首先需要定义委托,然后创建一个实例。

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);
        }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM