2. Why
Dynamically created GUI controls can be difficult to deal with, but are extremely
useful. At my job there are many different versions of the same hardware, each
with their own specific properties. For example, a generation 3 ARINC module has
6 channels, where as a generation 5 ARINC module has 12 channels.
Dynamically created GUI controls are important for any industry because they are
less time consuming, space consuming and more efficient compared to creating
additional software for each situation you will come across.
3. Example using Windows Forms and C# in VS2015
- Create a project in Visual
Studio.
- You need some parameter
that dictates how many GUI
controls to make.
- You need a data structure to
store the GUI controls.
- You need some kind of table
that can generate rows
dynamically. In this example
I’ll be using a table layout
panel.
4. - It’s cleaner to use methods to create controls because it helps reduce bloat in your code. This
method is pretty basic. It creates a check box control, sets if it’s checked, sets the width and
height, and then returns it. More complicated controls will have more complicated create
methods. If you wanted to take it a step further you can add some way of mapping delegates to
events in this method. At work, I usually pass a list of tuples that contain a key and a delegate.
This allows you to map multiple delegates to multiple events.
5. This is our checked changed event handler for when any of the check boxes are clicked. I
added a Text Box to the form to show how linking event handlers to dynamically created
GUI controls work. This method receives the control which triggered the event as a
parameter. It goes through the list of controls and looks for what index the check box holds
and then prints a message in the text box.
6. This is the rest of the
program. The constructor
calls the init method, which
creates the List<CheckBox>,
sets the amount of rows in
the table layout panel, and
creates the check boxes.