Solved

add array of labels to a panel in C# winforms

Posted on 2016-09-21
3
36 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 74

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

13 Experts available now in Live!

Get 1:1 Help Now