79034

Create labels from array

Question:

I want to create labels based on an array, but i always get only one label.

private void button1_Click(object sender, EventArgs e) { Debug.WriteLine(hardrive.GetHardDriveName.Count); Label[] lblHDDName = new Label[hardrive.GetHardDriveName.Count]; for (int i = 0; i < hardrive.GetHardDriveName.Count; i++) { int x = 10; int y = 10; lblHDDName[i] = new Label(); lblHDDName[i].Location = new System.Drawing.Point(x, y); lblHDDName[i].Text = "Test"; groupBoxHDD.Controls.Add(lblHDDName[i]); y += 10; } }

Debugging

Debug.WriteLine(hardrive.GetHardDriveName.Count);

Shows two items in the array.

The problem is that in the GroupBox there is only one label instead of two.

Answer1:

Your y variable is defined in the for loop, not outside. Therefore, for each execution of the loop, you initialize it to 10 and use it in your System.Drawing.Point. If you want to keep track of the increment done at the end of the loop, you must declare and initialize y before the for loop.

int y = 10; for (int i = 0; i < ...; i++) { // use y ... // increment it y += 10; }

Answer2:

You're resetting y back to 10 at the start of each iteration of the loop.

move the declaration of x and y outside of the loop.

this:

for (int i = 0; i < hardrive.GetHardDriveName.Count; i++) { int x = 10; int y = 10; ....

should be:

int x = 10; int y = 10; for (int i = 0; i < hardrive.GetHardDriveName.Count; i++) { ....

Answer3:

Nope, you're creating all the right labels - but they've all got the same position of (10, 10). If you want to see more than one of them, you'll need to put them in difference places :) Either declare y outside the loop, or simply use:

lblHDDName[i].Location = new Point(10, i * 10 + 10);

Rather than hard-coding the positions, you may want to look into an auto-arranging control of some description.

Also, it looks like you don't really need the array of labels at all - you're not using them afterwards. For example, you could have:

private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < hardrive.GetHardDriveName.Count; i++) { // Assuming you're using C# 3 or higher Label label = new Label { Location = new Point(10, i * 10 + 1), Text = "test" }; groupBoxHDD.Controls.Add(label); } }

or even:

private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < hardrive.GetHardDriveName.Count; i++) { groupBoxHDD.Controls.Add(new Label { Location = new Point(10, i * 10 + 1), Text = "test" }); } }

Answer4:

move the

int x = 10; int y = 10;

out of the for loop

Answer5:

One of the problems you have is that the x and y declarations are inside the loop so y will always be <em>10</em> even though you add 10 to it in the end of the loop. The labels will always be in the same position. To fix this move int y = 10 outside of the for loop. You should move int x = 10 there as well.

Answer6:

move

int x=10; int y=10;

outside the loop

and increment y by 30

if( you want the labels to align themselves to their starting point

int x=10;

can remain inside the loop

int x = 10; for (int i = 0; i < hardrive.GetHardDriveName.Count; i++) { int y = 10; lblHDDName[i] = new Label(); lblHDDName[i].Location = new System.Drawing.Point(x, y); lblHDDName[i].Text = "Test"; groupBoxHDD.Controls.Add(lblHDDName[i]); y += 30; }

Recommend

  • For loop with if condition on multiple R functions
  • How can I tell a form not to dispose a particular control when it closes?
  • WPF version of .ScaleControl?
  • Defined variables not working in javascript files when I use getScript
  • ThreadStatic in asynchronous ASP.NET Web API
  • Scipy Leastsq Optional Output Variable (Mesg)
  • Overlapping controls in Windows XP
  • Zoom in and out of jPanel
  • IE7 and TinyMCE with Plone
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • Scala multiline string placeholder
  • Firefox Extension - Monitor refresh and change of tab
  • Saving Changes After In-App Purchase Has Been Purchased
  • Assign variable to the value in HTML
  • Debug.DrawLine not showing in the GameView
  • Converting a WriteableBitmap image ToArray in UWP
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • How would I use PHP exceptions to define a redirect?
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • MySQL WHERE-condition in procedure ignored
  • JSON with duplicate key names losing information when parsed
  • htaccess rewriting URLs with multiple forward slashes
  • Weird JavaScript statement, what does it mean?
  • To display the title for the current loaction in map in iphone
  • How do you troubleshoot character encoding problems?
  • Web-crawler for facebook in python
  • Jquery - Jquery Wysiwyg return html as a string
  • How to format a variable of double type
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • trying to dynamically update Highchart column chart but series undefined
  • need help with bizarre java.net.HttpURLConnection behavior
  • How does Linux kernel interrupt the application?
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal