In this article I will explain how to add dynamic controls like
Label, TextBox, Button, etc. to Windows Forms Application on click of a Button.
I will here also explain how we can attach event handler to
various dynamically added controls like TextBox, Button, etc.
Form Layout
The form layout
consists of a Button and a Panel control to add dynamic controls within it.
Namespaces
You will need to
import the following namespaces.
C#
using System.Linq;
using System.Drawing;
VB.Net
Imports System.Linq
Imports System.Drawing
Dynamically adding Windows Label Control
Firstly we need to
create a new object of the Label control and then count of the Label controls
is determined, Count is useful to give number incremented unique Ids to the
controls as well as for setting location of the controls on the Windows Form so
that controls don’t overlap each other. After the location, the remaining
properties like Size, Name and Text are set. Finally the Label control is added
to the Windows Forms Panel control.
C#
Label label = new Label();
int count = panel1.Controls.OfType<Label>().ToList().Count;
label.Location = new Point(10, (25 * count) +
2);
label.Size = new Size(40, 20);
label.Name = "label_" + (count + 1);
label.Text = "label
" + (count + 1);
panel1.Controls.Add(label);
VB.Net
Dim label As New Label()
Dim count As Integer = panel1.Controls.OfType(Of Label)().ToList().Count
label.Location = New Point(10, (25 * count) + 2)
label.Size = New Size(40, 20)
label.Name =
"label_" & (count + 1)
label.Text =
"label " & (count + 1)
panel1.Controls.Add(label)
Dynamically adding Windows TextBox Control
Similar to Label
the TextBox control is dynamically added, the only difference here is the Text
property and the dynamic TextChaned event handler.
C#
TextBox textbox = new TextBox();
count =
panel1.Controls.OfType<TextBox>().ToList().Count;
textbox.Location = new System.Drawing.Point(60, 25 * count);
textbox.Size = new System.Drawing.Size(80, 20);
textbox.Name = "textbox_" + (count + 1);
textbox.TextChanged += new System.EventHandler(this.TextBox_Changed);
panel1.Controls.Add(textbox);
VB.Net
Dim textbox As New TextBox()
count =
panel1.Controls.OfType(Of TextBox)().ToList().Count
textbox.Location = New System.Drawing.Point(60, 25 * count)
textbox.Size = New System.Drawing.Size(80, 20)
textbox.Name =
"textbox_" & (count + 1)
AddHandler textbox.TextChanged, AddressOf TextBox_Changed
panel1.Controls.Add(textbox)
The following
dynamic TextChanged event handler is raised when the text is changed inside the
dynamic TextBox control.
C#
private void TextBox_Changed(object sender, EventArgs e)
{
TextBox textbox = (sender as TextBox);
MessageBox.Show(textbox.Name + " text changed. Value
" + textbox.Text);
}
VB.Net
Private Sub TextBox_Changed(sender As Object, e As EventArgs)
Dim textbox As TextBox = TryCast(sender, TextBox)
MessageBox.Show(textbox.Name + " text
changed. Value " + textbox.Text)
End Sub
Dynamically adding Windows Button Control
Similar to the
TextBox control, here a Button control is dynamically added. The only
difference is that here we have a dynamic Click event handler.
C#
Button button = new Button();
count =
panel1.Controls.OfType<Button>().ToList().Count;
button.Location = new System.Drawing.Point(150, 25 * count);
button.Size = new System.Drawing.Size(60, 20);
button.Name = "button_" + (count + 1);
button.Text = "Button
" + (count + 1);
button.Click += new System.EventHandler(this.Button_Click);
panel1.Controls.Add(button);
VB.Net
Dim button As New Button()
count =
panel1.Controls.OfType(Of Button)().ToList().Count
button.Location = New System.Drawing.Point(150, 25 * count)
button.Size = New System.Drawing.Size(60, 20)
button.Name =
"button_" & (count + 1)
button.Text =
"Button " & (count + 1)
AddHandler button.Click, AddressOf Button_Click
panel1.Controls.Add(button)
The following
dynamic Click event handler is raised when the dynamic TextBox control is
clicked.
C#
private void Button_Click(object sender, EventArgs e)
{
Button button = (sender as Button);
MessageBox.Show(button.Name + " clicked");
}
VB.Net
Private Sub Button_Click(sender As Object, e As EventArgs)
Dim button As Button = TryCast(sender, Button)
MessageBox.Show(button.Name + "
clicked")
End Sub
No comments:
Post a Comment