【发布时间】:2021-02-05 18:44:04
【问题描述】:
所以我在 WinForms 应用程序上有一个表单。 在那个表单上是一个 FlowLayoutPanel。 FlowLayout 面板上有一堆用户控件,每个控件代表数据库表中的行。 每个控件上都有一个按钮。
如何让表单订阅一个按钮,单击其中一个控件传递回该行数据库信息?
这是控制代码:
public partial class ctrlLeague : UserControl
{
public League activeLeague = new League();
public event EventHandler<MyEventArgs> ViewLeagueClicked;
public ctrlLeague(League lg)
{
InitializeComponent();
lblLeagueName.Text = lg.leagueName;
activeLeague = lg;
}
private void btnViewLeague_Click(object sender, EventArgs e)
{
ViewLeagueClicked(this, new MyEventArgs(activeLeague));
}
public class MyEventArgs : EventArgs
{
public MyEventArgs(League activeLeague)
{
ActiveLeague = activeLeague;
}
public League ActiveLeague { get; }
}
}
如果我将以下内容放入表单构造函数中,它会告诉我“
【问题讨论】:
-
那么按钮在 ctrLeague 用户控件上?然后像往常一样订阅 evant (btn.Click += btnViewLeague_Click)。
-
但是我的表单不能像其他事件一样看到这个事件。我认为这是因为它在一个控件内,在一个 FlowLayoutPanel 内,并且该控件有多个实例。
-
我不知道您想在用户控件中订阅事件,而不是在表单中。
-
如果不清楚,我深表歉意。
标签: c# winforms event-handling