Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
| Search | Today's Posts | Mark Forums Read
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 16th, 2007, 05:33 AM
Authorized User
Join Date: Mar 2007
Location: Pune, Maharashtra, India.
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default Delete Dynamic Controls

I want to add/delete control to/from the form dynamically.
for e.g I add textbox to form on button Click event, here's the code

TextBox tnew;
public int x1=200; y1=200;

  public void btn_Add_Click(object sender, EventArgs e)
      tnew = new TextBox();
      tnew.Location = new Point(x1,y1);
      y1 = y1 + 20;

The above code adds textbox one below the other on the form.

I want the code that deletes the textbox from the bottom, one by one
What I tried tnew.Dispose() doesnot work.

Old July 22nd, 2007, 04:23 AM
Authorized User
Join Date: Sep 2004
Location: Edegem, Antwerpen, Belgium.
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts


It is best to keep track of what control you've added to the form. This will make it easier to remove them back afterwards.
Here is an example that uses a stack (last in first out principle) to store a reference to the controls you've added.

By using the Pop method, you can retrieve the latest added control. You need to remove the control from the Controls collection of the form before disposing it.

NOTE: The sample is written in 2.0, let me know if you are using 1.1 so I'll rewrite it without using Generics.

private Stack<Control> _dynamicControls = new Stack<Control>();
private Point _lastLocation = new Point(50, 50);

private void btnAdd_Click(object sender, EventArgs e)
    TextBox newTextBox = new TextBox();
    newTextBox.Location = _lastLocation;

    _lastLocation.Offset(50, 50);

private void btnDelete_Click(object sender, EventArgs e)
    if (_dynamicControls.Count > 0)
        Control controlToBeRemoved = _dynamicControls.Pop();


Let me know if this isn't clear.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic User Controls.... Jayahar ASP.NET 2.0 Professional 8 October 12th, 2007 01:27 AM
Create dynamic controls saulodet C# 2 November 21st, 2003 12:36 PM
Dynamic Controls [xx(] koneruvijay VS.NET 2002/2003 0 November 12th, 2003 12:02 AM
Dynamic ActiveX Controls gabloom Excel VBA 0 September 17th, 2003 02:06 PM
Dynamic Controls tommyuk VB How-To 2 June 26th, 2003 12:54 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.