Click here to Skip to main content
13,400,012 members (46,809 online)
Click here to Skip to main content
Add your own
alternative version


41 bookmarked
Posted 11 Aug 2004

Dynamic Dialog Box

, 11 Aug 2004
Rate this:
Please Sign up or sign in to vote.
An article on dynamic dialog boxes and ListView control.

Sample Image


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.

private void btnTest_Click(object sender, System.EventArgs e)
    TestDyn tl;
    int lop = 0;
    int numentries;
    // If we have entries then allow the test
    numentries = listView1.Items.Count;
    if ( numentries > 0 )
        tl = new TestDyn();
        // set the number of entries
        // for each entry set its name in the test dialog.
        foreach (ListViewItem item in listView1.Items)
            tl.SetlabelName(lop, item.SubItems[0].Text);

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.

// for now assume the maximum allowable entries at 3.
// this can be changed to allow as much as you want.
const int maxentries = 3;
int    numentries;  // lets keep the count of what we have

// the actual declarations of the controls that we will create.
private System.Windows.Forms.Label [] label1;
private System.Windows.Forms.TextBox [] textBox1;

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.

protected void InitMyComponent()
    int lopcnt;

    // create the array of controls
    this.label1 = new Label[maxentries];
    this.textBox1 = new TextBox[maxentries];

    // now create the entries for each array element
    for ( lopcnt = 0; lopcnt < maxentries; lopcnt++)
        this.label1[lopcnt]  = new System.Windows.Forms.Label();
        this.textBox1[lopcnt]  = new System.Windows.Forms.TextBox();

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.

protected override void OnLoad(EventArgs e)
    int    lop;
    int    x1;
    int    y1;
    int    x2;
    int    y2;
    int    width1;
    int    height1;
    int taborder = 0;
    int    width2;
    int    height2;

    // TODO:  Add TestLic.OnLoad implementation
    base.OnLoad (e);

    // set the initial setting of the controls.
    // for the label
    // although the initial placement
    // of the controls are a bit arbitrary you
    // would want to be more precise for a better look and feel.
    x1 = 16;
    y1 = 24;
    width1 = 88;
    height1 = 16;

    // and the textbox
    x2 = 128;
    y2 = 16;
    width2 = 120;
    height2 = 20;
    // for each entry that was requested.
    for ( lop = 0; lop < numentries; lop++)
        // label n
        label1[lop].Location = new System.Drawing.Point(x1, y1);
        label1[lop].Name = "label" + Convert.ToString(lop);
        label1[lop].Size = new System.Drawing.Size(width1, height1);
        label1[lop].TabIndex = taborder;

        // textBox n
        textBox1[lop].Location = new System.Drawing.Point(x2, y2);
        textBox1[lop].Name = "textBox" + Convert.ToString(lop);
        textBox1[lop].Size = new System.Drawing.Size(width2, height2);
        textBox1[lop].TabIndex = taborder;


        y1 += 50;
        y2 += 50;

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.


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


About the Author

Web Developer
United States United States
I am a Director of Engineering, have an MBA and work in C# forms, Asp.Net and I have been writing Windows program since windows 3.0. I am currently working in the Healthcare industry.

I enjoy reading, music (most types), and learning new technology. I am involved in opensource projects at codeplex.

My linkedin link is

You may also be interested in...

Comments and Discussions

GeneralCac ban oi ! Pin
thuyan05420-Jun-07 4:24
memberthuyan05420-Jun-07 4:24 
GeneralNice ! Pin
Trance Junkie7-Jun-05 2:09
memberTrance Junkie7-Jun-05 2:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180218.2 | Last Updated 12 Aug 2004
Article Copyright 2004 by Donsw
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid