Dynamically Adding Controls to a Windows Form
I have a project right now who’s requirements are to dynamically create sections on a form, dynamically create items in those sections (checkboxes, text boxes, etc), and store the values entered in those boxes in a database.
Now this is no small task, but it had me thinking. It’s been a while since I wrote something that used dynamic controls. Since I’m writing the code anyway I thought I would share the technical details as to how you too can create your own controls on the fly, and hook up the events, and read the data. So many web demo’s seem to show the first and miss the next two (which would be the same as missing the point entirely..)
Here is the code (C# this time!):
So that’s how you dynamically add controls to a windows form in C# .Net. Feel free to post your questions / comments below.
Note: In vb the line:
I have a project right now who’s requirements are to dynamically create sections on a form, dynamically create items in those sections (checkboxes, text boxes, etc), and store the values entered in those boxes in a database.
Now this is no small task, but it had me thinking. It’s been a while since I wrote something that used dynamic controls. Since I’m writing the code anyway I thought I would share the technical details as to how you too can create your own controls on the fly, and hook up the events, and read the data. So many web demo’s seem to show the first and miss the next two (which would be the same as missing the point entirely..)
Here is the code (C# this time!):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DynamicControls
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//This block dynamically creates a TextBox and adds it to the form
TextBox tb = new TextBox();
tb.Name = "tbTest";
tb.Location = new Point(10, 10);
tb.Text = "Some text";
//Hook our textbox up to our generic textbox handler
tb.TextChanged += new System.EventHandler(this.tb_TextChanged);
this.Controls.Add(tb);
//This block dynamically creates a Button and adds it to the form
Button b = new Button();
b.Name = "bTest";
b.Location = new Point(10, 40);
b.Text = "My Button";
//Hook our button up to our generic button handler
b.Click += new System.EventHandler(this.b_Click);
this.Controls.Add(b);
}
//Our generic textbox TextChanged() handler:
private void tb_TextChanged(object sender, EventArgs e)
{
TextBox tb;
tb = (TextBox)sender;
//Prove we can get the name of the TextBox that had its text changed
MessageBox.Show(tb.Name + " TextChanged");
}
private void b_Click(object sender, EventArgs e)
{
Button b;
b = (Button)sender;
//Prove we can get the name of the Button that had its click event called
MessageBox.Show(b.Name + " Click");
}
private void bGetValues_Click(object sender, EventArgs e)
{
//Getting the events to fire is all well and good, but how do we collect
//the data from the form when Save is clicked? I am presuming that there
//are dozens of dynamically created controls on this form. Well, this is
//how
TextBox tb;
//Find the TextBox Control named "tbTest"
tb = (TextBox)this.Controls.Find("tbTest", true)[0];
//Prove we can get the Text that was entered in that textbox
MessageBox.Show(tb.Text);
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DynamicControls
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//This block dynamically creates a TextBox and adds it to the form
TextBox tb = new TextBox();
tb.Name = "tbTest";
tb.Location = new Point(10, 10);
tb.Text = "Some text";
//Hook our textbox up to our generic textbox handler
tb.TextChanged += new System.EventHandler(this.tb_TextChanged);
this.Controls.Add(tb);
//This block dynamically creates a Button and adds it to the form
Button b = new Button();
b.Name = "bTest";
b.Location = new Point(10, 40);
b.Text = "My Button";
//Hook our button up to our generic button handler
b.Click += new System.EventHandler(this.b_Click);
this.Controls.Add(b);
}
//Our generic textbox TextChanged() handler:
private void tb_TextChanged(object sender, EventArgs e)
{
TextBox tb;
tb = (TextBox)sender;
//Prove we can get the name of the TextBox that had its text changed
MessageBox.Show(tb.Name + " TextChanged");
}
private void b_Click(object sender, EventArgs e)
{
Button b;
b = (Button)sender;
//Prove we can get the name of the Button that had its click event called
MessageBox.Show(b.Name + " Click");
}
private void bGetValues_Click(object sender, EventArgs e)
{
//Getting the events to fire is all well and good, but how do we collect
//the data from the form when Save is clicked? I am presuming that there
//are dozens of dynamically created controls on this form. Well, this is
//how
TextBox tb;
//Find the TextBox Control named "tbTest"
tb = (TextBox)this.Controls.Find("tbTest", true)[0];
//Prove we can get the Text that was entered in that textbox
MessageBox.Show(tb.Text);
}
}
}
Note: In vb the line:
b.Click = new System.EventHandler(this.b_Click);
AddHandler b.Click, AddressOf b_Click
AddHandler b.Click, AddressOf b_Click
No comments:
Post a Comment