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;
numentries = listView1.Items.Count;
if ( numentries > 0 )
{
tl = new TestDyn();
tl.SetNumEntries(listView1.Items.Count);
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.
const int maxentries = 3;
int numentries;
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;
this.label1 = new Label[maxentries];
this.textBox1 = new TextBox[maxentries];
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;
base.OnLoad (e);
x1 = 16;
y1 = 24;
width1 = 88;
height1 = 16;
x2 = 128;
y2 = 16;
width2 = 120;
height2 = 20;
this.SuspendLayout();
for ( lop = 0; lop < numentries; lop++)
{
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++;
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.