Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6353121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:21:17+00:00 2026-05-24T22:21:17+00:00

private ArrayList label= new ArrayList(30); Label label_class = new Label(); Random r = new

  • 0
private ArrayList label= new ArrayList(30);

Label label_class = new Label();
Random r = new Random();

for (int i = 0; i < label.Count; i++) {
    ((Label)label[i]).Location = new Point(r.Next(ClientRectangle.Right -10),
                                           r.Next(ClientRectangle.Bottom - 10));
    ((Label)label[i]).Text = "o";
    ((Label)label[i]).Click += new EventHandler(Form1_Load);

    this.Controls.Add((Label)label[i]);

    ((Label)label[i]).Show();
}

This for loop is inside the Form1_Load so that it will run when the form is loading..
the problem is that, when I break point, I see that the code inside the forloop is not noticed by the break point/does not run. why is that?? and how can i create many labels randomly placed on form1(window form)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:21:18+00:00Added an answer on May 24, 2026 at 10:21 pm

    The issue is in

    private ArrayList label= new ArrayList(30);
    

    This creates an ArrayList of size 30, not one with 30 elements.

    If you do something like

    List<Label> labels = new List<Label>();
    
    for (int i = 0; i < 30; i++) {
        var temp = new Label();
    
        temp.Location = new Point(r.Next(ClientRectangle.Right -10),
                                           r.Next(ClientRectangle.Bottom - 10));
        temp.Text = "o";
        temp.Click += new EventHandler(Form1_Load);
    
        temp.BackColor = System.Drawing.Color.White;
    
        this.Controls.Add(temp);
    
        temp.Show();
        labels.Add(temp) 
    }
    

    It should work.

    In addition, notice my use of List<Label> instead of ArrayList there are cases where you will want to have the ability to specify the objects you are taking out but generally ( and in this case ) the generic forms where you specify the type will sure you much better. You will not need to do all of the boxing that you are doing and you will write fewer lines of code all of which will be more readable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a array list like this: private ArrayList<Locations> Artist_Result = new ArrayList<Location>(); This
I have an array list of objects in my application. private static ArrayList<Player> userList=new
private List<String> subList; private List<List<String>> records = new ArrayList<List<String>>(); for(....){ subList = new ArrayList<String>();
private ArrayList<String> colors = new ArrayList<String>(); Looking at the example above, it seems the
private Arraylist split(byte[] filebytes, int range) { } how to split this filebytes by
Let's say I have this: public class Whatever { private ArrayList<String> myList = new
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
private const int THE_ANSWER = 42; or private const int theAnswer = 42; Personally
. private void Form1_Load(object sender, EventArgs e) { List<CaclulatedData> tests = new List<CaclulatedData> {
I have a problem with ArrayList. I'm using ArrayList like this: private ArrayList<Playlist> mPlaylists;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.