Avatar of Mr_Fulano
Mr_Fulano
Flag for United States of America asked on

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,
Fulano
C#Microsoft Visual Studio

Avatar of undefined
Last Comment
Mr_Fulano

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
kaufmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Mr_Fulano

ASKER
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
Mr_Fulano

ASKER
Thank you Kaufmed, your solution worked very well. Very easy...
Your help has saved me hundreds of hours of internet surfing.
fblack61