Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 30024 |  Welcome, Guest!   Register  Login
Home > Articles > Windows Forms > Runtime Adding and Removing of Controls in Winforms

Runtime Adding and Removing of Controls in Winforms

1 vote(s)
Rating: 5 out of 5
Article posted by Initiotech on 9/20/2009 | Views: 8420 | Category: Windows Forms | Level: Intermediate red flag


The Article will explain how to Add and remove controls from the Windows Forms during the Runtime.

Introduction

This Article will demontrates how to Add Controls on Runtime and how to remove them.
And also how to get values out of an Runtime Created Control.
It Also demonstrates how to write event handlers for runtime created controls.

To Add the Controls in the Runtime Use the Following Code


private void button1_Click(object sender, EventArgs e)
{
j = j + 1;
TextBox cntrl;
LinkLabel labl;
cntrl = new TextBox();
cntrl.Text = "Text " + i.ToString();
labl = new LinkLabel();
labl.Name = "lbl" + j.ToString();
labl.Text = "Remove Text Box";

labl.Tag = cntrl;

labl.Click += new EventHandler(RemoveControl);
if (i < this.Height)
{
cntrl.Location = new Point(0, i);
labl.Location = new Point(cntrl.Width + 5 , i);
i = i + cntrl.Height +5;
Controls.Add(cntrl);
Controls.Add(labl);
}
else
{
MessageBox.Show("Cannot Add more Controls");
}
}

Note : The "Tag" Property of the LinkLabel Control is used to Store the information about the control next to it(TextBox) for Removing purpose only.

To Remove the Controls use the Following Code


public void RemoveControl(object sender, System.EventArgs e)
{
LinkLabel lbls=(LinkLabel)sender;
this.Controls.Remove((LinkLabel)sender);
this.Controls.Remove((TextBox)lbls.Tag);

}


To retrive Values from the Added Runtime Control use the Following code


private void button2_Click(object sender, EventArgs e)
{
foreach (Control ctrl in Controls)
{
if (ctrl is TextBox)
{
MessageBox.Show(((TextBox)ctrl).Text);
}
}
}

Regards
Hefin Dsouza

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Hefin Dsouza

Experience:8 year(s)
Home page:http://hefin.in
Member since:Wednesday, September 16, 2009
Level:Starter
Status: [Member]
Biography:My Name is Hefin Dsouza.
I am a faculty at Computer Education Institute.I am also an Freelance Software Developer and Software Trainner.I am working with DotNet Technologies from the past 8 years.I am an Microsoft Certified Windows Application Developer for .NET 3.5.
And also a Blogger at http://blog.hefin.in
>> Write Response - Respond to this post and get points
Related Posts

In this article you will learn about the hash algorithm and types are available in .Net.

Display the images in the rdlc reports, where the images are retrived from the database.

The Article explains the usage of FileSystemWatcher class in winforms for logging Deleting,Renaming,Modifications of files.

In This article you will know how to start a mobile application on windows.

In this article explains you how to create Stopwatch Application using C#.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/28/2012 11:58:43 AM