gpt4 book ai didi

c# - 我有事件处理程序 (c#) 的问题

转载 作者:太空宇宙 更新时间:2023-11-03 11:36:04 25 4
gpt4 key购买 nike

这段代码来自一个类

 Button pagebtn = new Button();
pagebtn.Text = "2";

pagebtn.Click +=
listTopicPerPage_Click(this, new btnEventArgs() { btnNumber = 2 });

我在 eventHandler 方法上加了一条下划线,说..

cannot immplicitly convert type 'void' to 'System.EventHandler'.

这是为什么?

我创建的另一个类的代码:

class btnEventArgs : EventArgs
{
public int btnNumber { get; set; }
}

最佳答案

要指定事件参数,您应该在类中创建一个事件字段,并根据需要将其提升:

public partial class WebForm1 : System.Web.UI.Page
{
public event EventHandler<btnEventArgs> SampleEvent;

public void DemoEvent(int val)
{
// Copy to a temporary variable to be thread-safe.
EventHandler<btnEventArgs> temp = SampleEvent;
if (temp != null)
temp(this, new btnEventArgs { btnNumber = val });
}


protected void Page_Load(object sender, EventArgs e)
{
Button1.Click += new EventHandler(Button1_Click);
}

void Button1_Click(object sender, EventArgs e)
{
DemoEvent(2);
}
}

class btnEventArgs : EventArgs
{
public int btnNumber { get; set; }
}

MSDN article

关于c# - 我有事件处理程序 (c#) 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6278711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com