Hi everyone,

I am thinking of doing something like this but I am not sure it can be done or not. Normally, if i write a program (Windows Form) I drag and drop what button and textboxes are necessary on the form then do necessary procedures and actions. What I am thinking now is, is there anyway to build a window form dynamically using some setting from a file ? I have no idea how to do it. Let's say if I have something like "button 3" then on my window form, it has 3 buttons on it. So, if i change that text file to "button 4" then on my window form, it has 4 buttons on it. If possible, I would like to know how to assign event to those button. I am not sure whether it's feasible or not but please I am looking forward to hearing other experts ideas here. Thanks all.

7 Years
Discussion Span
Last Post by htetnaing

Yes, it is possible. You'll need to learn all the fields you need to set and the easiest way to do that is to see how Visual Studio does it.

Open a new Windows form project and add a textbox to the form. Add an event handler also. Now open the file Form1.Designer.cs. You'll see a collapsed code section called "Windows Form Designer generated code". Expand this and look for the comment that says "textBox1". That is all the code used to configure the textBox (just above the comment you should see the "new System.Windows.Forms.TextBox()" line where they create the object.

Just bellow that code you'll see the "Form1" comment, and in there the line where they add the control to the form this.Controls.Add(this.textBox1) .

That's everything you need to add a control of the form.


Thanks. I didn't think of how Visual Studio does. I guess it's the best way to learn about how to make a dynamic window forms. I still have more one more question. If I want to assign event to those button, how can i do so ? I mean Visual Studio get all the code before the compilation is done and in my case, it's already in runtime. I hope you dun mind shedding some light about it on me. Thanks.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.