Adding C# Controls to a Windows Form Dynamically.

Mr_Fulano
Mr_Fulano used Ask the Experts™
on
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,
Fulano
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
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.

e.g.

Label labelInstance = new Label();

formInstance.Controls.Add(labelInstance);

Open in new window

Author

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.

Thanks,
Fulano

Author

Commented:
Thank you Kaufmed, your solution worked very well. Very easy...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial