Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
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 August 12th, 2007, 08:04 AM
Authorized User
 
Join Date: Mar 2007
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default ArrayList with UserControl

I have a UserControl that has textbox, labels etc in it.
This control can be added to the form one below the other via
Form Load_Event
{
     for (int i = 0; i <5; i++)
     {
          UserControl1 u1 = new UserControl1();
          u1.Location = new Point(70, (125 + (i * 23)));
          this.Controls.Add(u1);
     }
}



Same thing I want to use a ArrayList so I tried this,

List<UserControl1> array_list = new List<UserControl1>();
UserControl1 uc1 = new UserControl1();

To add the UserControl1 to form I again used a for loop
for (int i = 0; i <5; i++)
{
     array_list[i].Add(uc1);
     array_list[i].Location = new Point(70, (125 + (i * 23)));
}

But this gives me an ERROR
UserControl1' does not contain a definition for 'Add'

How can I add UserControl to form, one below the other using
ArrayList?













 
Old August 12th, 2007, 08:10 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Take a look at this:

   array_list[i].Add(uc1);

You're calling Add in the i-th item in the list. Instead, you want to call Add on the list itself:

   array_list.Add(uc1);

Not sure if this solves your original problem, but it should at least solve the "UserControl1' does not contain a definition for 'Add'" error.

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old August 12th, 2007, 12:29 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You are going to encounter another problem: You will find that this code will only add a single user control. This is because you are adding the same control to the list over and over. You need to create a new instance of the control on each pass of the loop. I also simplified your code a bit further, including Imar's suggestion. I find it easier to work with the new instance of the class is questions then to clutter the code by using the list indexer property.

List<UserControl1> array_list = new List<UserControl1>();
UserControl1 uc1;
for (int i = 0; i <5; i++)
{
     uc1 = new UserControl1();
     uc1.Location = new Point(70, (125 + (i * 23)));
     array_list.Add(uc1);
}

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
ArrayList Mogg-Way C# 2005 5 February 12th, 2008 04:05 PM
ArrayList sort collie C# 1 August 28th, 2007 08:08 AM
Help with an arraylist crazy-nun General .NET 4 July 14th, 2005 03:32 AM
a usercontrol problem csc820203 C# 2 July 10th, 2004 10:55 AM
ArrayList kobystud C# 4 May 25th, 2004 02:05 PM





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