Solved

add array of labels to a panel in C# winforms

Posted on 2016-09-21
3
46 Views
Last Modified: 2016-09-21
I want to programmatically create an array of labels and add them to a panel.  None of the labels appear.  

        private Label[] dies = new Label[12];

private void panelAdjDieLevel_Paint(object sender, PaintEventArgs e)
         {
             int height = this.panelAdjDieLevel.Size.Height;
             int length = this.panelAdjDieLevel.Size.Width;
             for (int i = 0; i < 12; i++)
             {
                 dies[i] = new Label();
                 dies[i].Height = 100;// this.Size.Height / 2;
                 dies[i].Width = this.Size.Width / 12;
                 dies[i].Location = new Point(this.panelAdjDieLevel.ClientRectangle.X + (this.panelAdjDieLevel.Size.Height / 2),     this.panelAdjDieLevel.ClientRectangle.Y + (i * dies[i].Width));
                 dies[i].Text = i.ToString();
                 dies[i].BackColor = Color.DarkBlue;
                 dies[i].Visible = true;
                 dies[i].BringToFront();
                 dies[i].BorderStyle = BorderStyle.FixedSingle;

             }
0
Comment
Question by:roujesky
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 41809635
Add them to the Controls property of the Panel.
0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 41809698
First, as käµfm³d pointed out, you need to add labels to a containers control. Have a look how this is done on any form ("Windows Form Designer generated code" in a Designer file). Code is something like:

            this.panelAdjDieLevel.Controls.Add(this.dies[i]);

Open in new window



Second, I wouldn't use Paint event. This event is called every time when control is redrawn. Use a constructor, load event but not Paint:
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.paint(v=vs.110).aspx
0
 

Author Closing Comment

by:roujesky
ID: 41809798
thanks!
that did it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with retrieving partial value from a column using VB.NET 4 44
Syntax error 9 45
Syntax Error 2 46
How would you add MULTITHREADING to the attached C# code? 4 52
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now