Solved

add array of labels to a panel in C# winforms

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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