C#-将自定义参数传递给事件
所以我尝试向事件发送自定义参数,但它从未起作用,我尝试了许多不同的方法,但从未起作用, 基本上如此C#-将自定义参数传递给事件,c#,C#,所以我尝试向事件发送自定义参数,但它从未起作用,我尝试了许多不同的方法,但从未起作用, 基本上如此 public void CreateEmojiList() { CreateAllEmojis(); int btnCount = 0; foreach(Emoji emoji in emojiList) { Button btnEmoji = new
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);
}
我想从中得到这两个值
emoji.EmojiText和emoji.EmojiName一种方法是从
按钮
继承并创建一个名为EmojiButton
的类。然后声明一个和事件处理程序的签名匹配的委托。之后,在EmojiButton
类中使用委托声明一个事件,并将类似EmojiText
和EmojiName
的属性也添加到button子类中。最后,您需要将按钮单击事件与自定义事件链接起来。每当单击按钮时,引发事件并传递参数,即this.EmojiText、this.EmojiName
另一种方法是将
表情符号
对象指定给标记
属性。然后,您可以使用正常的EventHandler
签名(对象发送者,EventArgs e
)编写事件处理程序,并查看发送者的标记是什么。然后将标记
转换为表情符号
,并访问其属性。我想到的最快解决方案,并不意味着自定义用户控件
类的定义,按钮
的子类化以及其他类似实践是:
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...
}
您可以利用闭包来“打包”每个按钮的事件处理程序的附加事件数据。一定不要这样做
public void CreateEmojiList()
{
CreateAllEmojis();
int btnCount=0;
foreach(表情列表中的表情符号)
{
按钮btnEmoji=新按钮();
btnEmoji.Size=新尺寸(40,36);
btnEmoji.FlatStyle=FlatStyle.Flat;
btnEmoji.FlatAppearance.MouseDownBackColor=Color.Cyan;
btnEmoji.Cursor=Cursors.Hand;
Font=新字体(“Bahnschrift”,6.75f);
btnEmoji.Text=emoji.EmojiText;
btnEmoji.Top=(panel_main.Controls.OfType().Count()/4)*(1+btnEmoji.Height)+6;
btnEmoji.Left=(btnEmoji.Width+1)*btnCount+6;
面板_main.Controls.Add(btnEmoji);
var emojiCopy=emoji;//不要关闭循环变量!
btnEmoji.Click+=(发送者,参数)=>OnEmojiClick(emojiCopy);
btnCount++;
如果(btnCount==4)
btnCount=0;
}
}
受保护的虚拟void OnEmojiClick(表情表情符号)
{
//做点什么
}
首先需要定义委托,然后创建实例
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);
}
class表情符号
{
//公共委托无效EmojiClickEventHandler(对象发送方、事件args args);
//公共事件EmojiEventHandler EmojiClicked;
//您可以使用以上两行,也可以在代码下面替换它们。
公共事件事件处理程序;
public void createMojiList()
{
CreateAllEmojis();
int btnCount=0;
//代码的其余部分
面板_main.Controls.Add(btnEmoji);
btnEmoji.Click+=OnEmojiClick(btnEmoji);
btnCount++;
}
受保护的虚拟void OnEmojiClick(按钮表情符号)
{
//如果没有事件的订阅者,则在此处执行null检查以处理
如果(EmojiClicked!=null)
{
//表情符号没有名称属性定义,只有文本,因此只传递文本。
EmojiClicked(这是新的EmojiClickEventArgs(emoji.Text,emoji.Text){});
}
}
已单击私有无效表情符号(对象发送者、表情符号ClickEventArgs args)
{
按钮mybutton=发送方为按钮;
Console.WriteLine(“表情文字”+args.text);
}
}您希望传递哪些参数?看起来您使用的是标准按钮,因此无法更改其工作方式。“EmojiClickEvent”与普通按钮单击事件之间有什么区别?当您使用按钮时,eventhandler由button类定义。您可以传递一个派生的类,但事件签名是在类中定义的,因此除非您拥有类代码,否则您不能更改签名。但你仍然可以在活动中随心所欲。您希望对事件参数具体做什么?可能您应该阅读事件如何工作以及如何在自己的类中创建它们,以便获得更深入的理解:线程更新,我意识到我没有提供足够的信息以某种方式将EmojiText和EmojiName编码到按钮的Name属性中。
如果要这样做,不妨使用Tag属性。假设这是Wintech哦,是的!我忘了!我如何准确地访问它的标签?我尝试了sender.Tag,但显然它不起作用。@OussamaEssamadi您需要先转换sender
:((按钮)sender.Tag
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
}
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);
}