Adding C# Controls to a Windows Form Dynamically.

Hi, I'm using Visual Studio 2015 - C#. (I have to admit, I haven't coded in a while, so I'm a bit rusty.)

That said, I have to create a Windows Form Application that will allow the enduser to conduct a survey of several different types of facilities. The application would ask the user questions about each facility and the user would select from a set of predetermined answers (i.e. None, Low, Medium, High, etc) to answer the questions. Each answer would have a predetermined value and all those values would be added up at the end to provide a "total evaluation score." -- That part of the project I think I can accomplish without too much difficulty.

However, my question is this -- since the enduser will have different types of facilities, I wanted to have "one" application and create the controls on the form dynamically depending on what questions and control types are needed for each type of facility. I could use Tab Delimited files to manage the different types.

Therefore, all the questions on the Form will have a Label that makes up the verbiage for the question. And all questions will have a Control of some sort (i.e.. a DropdownBox, or a set of Radio Buttons, or maybe a ComboBox) so, how could I, by way of using Tab Delimited files in conjunction with my Application, add Controls (dynamically) to my Application at Runtime?

More specifically, if I wanted to (dynamical at Runtime) add a Label and a DropdownBox Control to my Form, how would I do that?

Once I figure out the format to do that, I would just have the Application read from a Tab Delimited file - line by line, that would tell the Application which questions to insert into the Form and which Controls go with each specific question.

(Unless you know of a better way to do this, which I would very much like to learn about).

Much like this example below:

                          LABELS                                                                                                           CONTROL                                  CONTROL
   1).  Rate your lighting for your facility.                                                       [DropdownBox - with several choices]         [Score]

   2). Rate your security perimeter (i.e fencing) for your facility.              [DropdownBox - with several choices]          [Score]

   3). Rate your emergency fire suppression for your facility.                   [Radio Buttons - with several choices]          [Score]

In the example above, I would be adding one Label and two Controls to my Form for each question. How to add those Controls dynamically to my Form is my question.

Thank you for your help!

Best Regards,
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Controls are a class like any other class in .NET. So you simply need to new one up. Where people get tripped up is that you also have to add your new instance to a Controls container--be that the form itself, or a panel, or a groupbox, etc. Any control you create must be added to a container control, with the form itself being the ultimate container control.


Label labelInstance = new Label();


Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mr_FulanoAuthor Commented:
Hmmm, very interesting. I see your point and YES, true...Controls are nothing more than a Class and indeed, the container is the "gotcha" in the mix. So, basically what I need to do is create a format for each of my "questions" (i.e. a Label, a couple of other controls, etc) and put that into a ForEach loop, so I can step through the file a line at a time. -- Let me give this a test drive and I'll get back to you ASAP.

Mr_FulanoAuthor Commented:
Thank you Kaufmed, your solution worked very well. Very easy...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.