添加点击功能生成按钮WinForms

Add _Click funciton to generated button WinForms

我对向生成的按钮添加功能有疑问。我希望能够通过按一个按钮转到另一个 .cs。

确切事件:

PartnerInfo partnerInfo = new PartnerInfo();
partnerInfo.Show();

在每一行的 'TableLayoutPanel' 中生成按钮。我现在为 table:

使用的代码
//get a reference to the previous existent 
RowStyle temp = MyPartnersPanel.RowStyles[MyPartnersPanel.RowCount - 1];
//increase panel rows count by one
MyPartnersPanel.RowCount++;

//add a new RowStyle as a copy of the previous one
MyPartnersPanel.RowStyles.Add(new RowStyle(temp.SizeType, temp.Height));
//add your controls
MyPartnersPanel.Controls.Add(
  new TextBox() {
    Text = PartnerName, ReadOnly=true }, 0, MyPartnersPanel.RowCount - 1);
    MyPartnersPanel.Controls.Add(new TextBox() { Text = PartnerLevel, ReadOnly = true }, 1, MyPartnersPanel.RowCount - 1);
    MyPartnersPanel.Controls.Add(new TextBox() { Text = PartnerGoal, ReadOnly = true }, 2, MyPartnersPanel.RowCount - 1);
    MyPartnersPanel.Controls.Add(new TextBox() { Text = WorkoutPerWeek, ReadOnly = true }, 3, MyPartnersPanel.RowCount - 1);
    MyPartnersPanel.Controls.Add(new Button() { Text = "+", BackColor = Color.Black, ForeColor = Color.White, Height = 29, }, 4, MyPartnersPanel.RowCount - 1);
)};

非常感谢任何帮助。 如果重要的话,我正在使用 .NET Core 3.1

将创建按钮的语句分成两部分,以便您可以访问按钮:

MyPartnersPanel.Controls.Add(new Button() { Text = ...

应该是:

Button btn  new Button() {Text = ...};
MyPartnersPanel.Controls.Add(btn);

// attach an event handler:
btn.Clicked += this.ButtonClickedEventHandler;

private void ButtonClickedEventHandler(object sender, ...)
{
    ...
}