Thread: Control array

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    3

    Control array

    hello every1

    i seem to be having problems adding Control arrays to a Panel. this is how i am doing it,

    Code:
    Label[] la = new Label[5];
    la[0].Text = "label text";
    Panel1.Controls.Add(la[0]);
    now this compiles fine, but i get an error on runtime. any1 know what im doing wrong?

    thanks for your help.

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    You've allocated space for a new array, but you haven't actually allocated space for each item in the array. Try this:

    Code:
    Label[] labels = new Label[5];
    labels[0] = new Label();
    labels[0].Text = "Label 1!";
    labels[1] = new Label();
    labels[1].Text = "Label 2!";
    labels[2] = new Label();
    labels[2].Text = "Label 3!";
    
    Panel1.Controls.AddRange(labels);

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    3
    thanks for your reply, got it to work perfect

  4. #4
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    You're welcome

















    My bill will be in the post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM