Dynamic Dialog Box

Aug 12, 2004

3 min read

.NET1.0

.NET1.1

VS.NET2002

VS.NET2003

C#

Windows

.NET

Visual-Studio

Dev

Intermediate

Author picture

by Donsw

Contributor

82k Views

Sample Image

Introduction

Welcome to my first article in CodeProject! The program that I wrote was to have a user enter a field name, select the data type, and set a field length. Once that was done, they should be able to create a dialog box based on what was entered. I have done this before in MFC but had not done this in .NET. The code provided here is just an example to show the process of what was needed to accomplish this.

Using the code

Since this was more of an example of how this can be done, the classes are not structured to be completely reusable. But hopefully, you can find the concepts useful in your project. At first, I wanted to add data into a ListView so that you can see the actual data that will be used. This was not only easy in VB but there were plenty of examples on the net to show how this was done. The problem that I ran into was that there were less examples using C#. But, through trial and error, I eventually got it to work. The first part was, after the data was entered into the ListView, we need to get that data and add it to the class for the dynamic dialog. This was accomplished by using the foreach statement.

CS
        

The class that actually handles the dynamic dialog can be created using the wizard. Do not create any controls using the wizard since we will create them in the code. The first set of code variables that need to be created are to establish the maximum controls that you want on the page and a count of the actual number.

CS
        

In the dynamic dialog class constructor, add a call to initialize the new controls before the call to InitializeComponent. InitMyComponent creates the array of controls and array elements for the number of controls that were requested.

CS
        

Now comes the actual code to set the location for each object. You not only have to find the initial location but do an increment on each iteration of the set of controls. If you were to polish this a bit more, you could spend more time on the initial location and increment. Since I really have this as an example, I did not spend a lot of time on the polish.

CS
        

Points of Interest

The next set of tasks that can be done for this is to add different control types. Here, I only allowed a TextBox and a Label. Another task would be to have a Panel on the dialog and put the controls on it. This way, you can add a scroll bar when the number of controls exceeds the visible space.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)