We help IT Professionals succeed at work.
Get Started

Adding C# Controls to a Windows Form Dynamically.

Mr_Fulano
Mr_Fulano asked
on
170 Views
Last Modified: 2017-11-17
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
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE