Create Button at runtime!
-
I have to create buttons,forms at runtime after reading XML file. XML file will contain numbers of buttons and froms which i have to create run time. Please let me know right way.
-
Where do you want to place these buttons? On the dynamic forms?
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
-
You are in the wrong forum then. You can create a user control which exposes a property where you can set the button count. That user control will in turn have a TableLayoutPanel or a FlowLayoutPanel which will ensure the layout comes out good. Now, when you set the button count, you can add cells to the TableLayoutPanel and add buttons to it. You can also have a delegate exposed from the user control which will act as a common click event for the button and which can be handled anywhere. Then, create a form and add this user control to it.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
-
You are in the wrong forum then. You can create a user control which exposes a property where you can set the button count. That user control will in turn have a TableLayoutPanel or a FlowLayoutPanel which will ensure the layout comes out good. Now, when you set the button count, you can add cells to the TableLayoutPanel and add buttons to it. You can also have a delegate exposed from the user control which will act as a common click event for the button and which can be handled anywhere. Then, create a form and add this user control to it.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
-
I don't have a sample code for this for now. You can try this: 1. Create a user control with a tablelayoutpanel in it. 2. Add a method to it that takes in one integer parameter. In that method, add rows and columns to the panel and add buttons in it. 3. Search for "delegates and events" or "c# expose events from user control" to understand how to expose the click event for the buttons and handle it in the creating form.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
-
I have to create buttons,forms at runtime after reading XML file. XML file will contain numbers of buttons and froms which i have to create run time. Please let me know right way.
Do like this :
Button newButton = new Button();
newButton.Location = new Point(30,50); // Put your calculated point here.
newButton.Text = "Click Me";
newButton.Size=new Size(50,25);
newButton.Click += new EventHandler(newButton_Click);
this.Controls.Add(newButton);Place your
Eventhandler
asnewButton_Click
.public void newButton_Click(object sender, EventArgs e)
{
// Do all you need.
}Please do post in right forum next time... :doh:
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using Javascript