Reputation: 1
I am having trouble creating a click event for a dynamically created button on a dynamically created Tab. I keep having an exception thrown. The program creates a Tab for each directory within a directory and then creates a "+" button that is supposed to allow the user to add more text boxes to the page.
Here is the tab creation code:
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);
}
Many thanks for any help!
Upvotes: 0
Views: 1056
Reputation: 1007
Use
....
Button newButton = new Button();
newButton.OnClick += (s, p) => { your onclick code here };
...
Or
....
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
}
...
Upvotes: 1