我在动态创建的Tab上为动态创建的按钮创建点击事件时遇到问题。我一直抛出异常。该程序为目录中的每个目录创建一个Tab,然后创建一个“+”按钮,该按钮应允许用户向页面添加更多文本框。
以下是标签创建代码:
private void addTabs(int tab_Number, string assetName)
{
TabPage newTab = new TabPage(assetName);
prjct_Directory_Setup_Tab.TabPages.Add(newTab);
// tabs need a unique id to maintain state information
newTab.Name = "Tab_" + tab_Number;
// add text to the tabs
newTab.Text = assetName;
//Add tab Labels
Tab_labelPositions(assetName, newTab);
//Create a new flow panel
FlowLayoutPanel mainTabFlowPanel = new FlowLayoutPanel();
//Add the control to the tab page
mainTabFlowPanel.AutoScroll = true;
mainTabFlowPanel.AutoSize = true;
mainTabFlowPanel.FlowDirection = FlowDirection.TopDown;
mainTabFlowPanel.Location = new Point(13, 134);
//Add the control to the tab page
newTab.Controls.Add(mainTabFlowPanel);
//Add the picture folder labels to the flow panel
findAllFolders_inAssetFolder(assetName, newTab, mainTabFlowPanel);
Point buttonLocale = new Point(156, 22);
String addFolders = "addFolders" + tab_Number;
//createButton(buttonLocale, "+", addFolders, newTab);
Button newButton = new Button();
//create a new size
Size buttonSize = new System.Drawing.Size(75, 33);
//setup the button
newButton.Name = addFolders;
newButton.Text = "+";
newButton.Location = buttonLocale;
newButton.Size = buttonSize;
newButton.Click += new EventHandler(this.newButton_Clicked);
}
非常感谢您的帮助!
答案 0 :(得分:1)
使用
....
Button newButton = new Button();
newButton.OnClick += (s, p) => { your onclick code here };
...
或者
....
Button newButton = new Button();
newButton.OnClick += newButtonOnClick; //where newButtonOnClick is a method you define in your class
private void newButtonOnClick(object sender, EventArgs e)
{
//insert code here
}
...