Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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);
      this.Controls.Add(tnew);
      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.




Reply With Quote
  #2 (permalink)  
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
Default

Hi,

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;
    this.Controls.Add(newTextBox);

    _lastLocation.Offset(50, 50);
    _dynamicControls.Push(newTextBox);
}

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

        this.Controls.Remove(controlToBeRemoved);
        controlToBeRemoved.Dispose();
    }
}

Let me know if this isn't clear.

Greetz,

Geert

http://geertverhoeven.blogspot.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:24 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.