Click here to Skip to main content
15,885,027 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi dear,
I am using c sharp to create a small application by using which so that i am able to create windows forms dynamically...I am really stuck in that application...Can somebody do help me out that how we can create c sharp forms dynamically and also add controls on that forms....

Please do help me out i will be very thankful for your this kind act...

Regards,
Hassan Tariq

XML
Thanks alot for your help and time... but dear actually i have a wizard that takes all the requirements from the user that how much buttons he want, how much text-boxes he wants etc...and when user completes that wizard the end result is that my project create the windows form for my user...but <b>the problem is that when user again run the same project executable file it again runs the wizard but instead that i just want that when user again runs the project it shows its own created form instead of my wizard</b>....

Thanks for your support.....
Waiting for a suitable reply......

Best Regards,
Hassan Tariq
Posted
Updated 12-Aug-11 19:45pm
v2

Here's a simple example for creating key/value pair editors:

Form form = new Form();
form.Location = new Point(x, y);

form.Width = 200;
form.SuspendLayout();
int usedHeight = 0;

foreach(KeyValuePair<string,> kvp in someDictionary){
 Label label = new Label();
 label.Text = kvp.Key;
 label.Top = usedHeight + 7; label.Left = 5;
 form.Controls.Add(label);
 
 TextBox textBox = new TextBox();
 textBox.Text = kvp.Value;
 textBox.Top = usedHeight + 5; textBox.Left = 80;
 textBox.Width = 115;
 textBox.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
 form.Controls.Add(textBox);

 usedHeight += textBox.Height + 5;
}

form.ResumeLayout();
form.Height = usedHeight + 10;


What you might want to do is dynamically fill a panel, and have other panels around the edges of your form with their Dock property set. That way you can use the designer to make an OK/Cancel button bar, menus, toolbar etc, and just have a panel in the middle with dynamic content (and set the size of the form appropriately). Dynamically creating entire forms is probably less useful.

I used something similar to this to bring up a selection dialog where the number of options to check wasn't known at design time (it was a list of data sets to operate on, which can be added by the user). In that case I had a GroupBox which was dynamically sized and several other controls in a panel docked to the top or the bottom made in the designer.
 
Share this answer
 
Comments
Member 7995415 13-Aug-11 1:44am    
Thanks alot for your help and time... but dear actually i have a wizard that takes all the requirements from the user that how much buttons he want, how much text-boxes he wants etc...and when user completes that wizard the end result is that my project create the windows form for my user...but the problem is that when user again run the same project executable file it again runs the wizard but instead that i just want that when user again runs the project it shows its own created form instead of my wizard....

Thanks for your support.....
Waiting for a suitable reply......

Best Regards,
Hassan Tariq
BobJanova 13-Aug-11 7:14am    
So your question is actually how to -store- a form? Run through the control tree creating some sort of definition file (XML would make sense) with the properties you care about (control type, caption, position, size, anchor, perhaps some styling information). When you start the app, see if that file exists, and if so, use it to create the form again instead of going through the wizard to create one.
Member 7995415 13-Aug-11 9:53am    
yes dear...this is the actual thing which i am going to ask..if u have some ideas about it do share with me with some coding example...

thanks...
Try this Codeproject article:

Creating Dynamic form and making the content fit[^]

If that isn't applicable, try learning how to use google. When I googled with your question as the search phrase, I got back 3.9 MILLION results.
 
Share this answer
 
Comments
hserrano 15-May-20 11:09am    
"When I googled with your question as the search phrase, I got back 3.9 MILLION results."
Then Why you respond?
#realJSOP 15-May-20 12:54pm    
You are aware that this question if almost 9 years old right?
You can also create a form in the designer and then look at the generate code by the designer.

For example, if you compare the code of a form without any control with the code of a form with one TextBox, you would essentially know how to add that control to your form.

The main difference with dynamically generated forms is that you would have to provide a way to access controls and hook events (and eventually unhook them depending how your code is done).
 
Share this answer
 
v2
Comments
Member 7995415 13-Aug-11 1:44am    
Thanks alot for your help and time... but dear actually i have a wizard that takes all the requirements from the user that how much buttons he want, how much text-boxes he wants etc...and when user completes that wizard the end result is that my project create the windows form for my user...but the problem is that when user again run the same project executable file it again runs the wizard but instead that i just want that when user again runs the project it shows its own created form instead of my wizard....

Thanks for your support.....
Waiting for a suitable reply......

Best Regards,
Hassan Tariq
Philippe Mori 13-Aug-11 8:24am    
You have written the same text at 3 different places! and worst in the question, it is displayed as code...
TamilRselvan 17-Aug-15 2:58am    
how to create a dynamic question in c# windos form

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900