Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Dynamic Dialog Box

0.00/5 (No votes)
11 Aug 2004 1  
An article on dynamic dialog boxes and ListView control.

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.

        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

                tl.SetNumEntries(listView1.Items.Count);
                // for each entry set its name in the test dialog. 

                foreach (ListViewItem item in listView1.Items) 
                {
                    tl.SetlabelName(lop, item.SubItems[0].Text);
                    lop++;
                }
                tl.ShowDialog();
            }
        }

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;
            this.SuspendLayout();
            // 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;
                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;
                taborder++;

                this.Controls.Add(textBox1[lop]);
                this.Controls.Add(label1[lop]);

                y1 += 50;
                y2 += 50;
            }
            this.ResumeLayout(false);
        }

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here