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 August 12th, 2007, 08:04 AM
Authorized User
 
Join Date: Mar 2007
Location: Pune, Maharashtra, India.
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?













Reply With Quote
  #2 (permalink)  
Old August 12th, 2007, 08:10 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 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
Reply With Quote
  #3 (permalink)  
Old August 12th, 2007, 12:29 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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
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
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



All times are GMT -4. The time now is 10:25 PM.


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