Solved

add array of labels to a panel in C# winforms

Posted on 2016-09-21
3
57 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 75

Expert Comment

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

689 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