Thread: Creating an Array of Labels

  1. #1
    Registered User Mcdom34's Avatar
    Join Date
    Jun 2012
    Location
    North Royalton, Ohio, United States
    Posts
    22

    Creating an Array of Labels

    I'm attempting to write a program that displays an array of labels; however, I can't seen to make the labels appear in the form. I've set some of the properties, but still not luck. Any suggestions?

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    
    namespace HangMan
    {
        public partial class Hangman : Form
        {
            Random r = new Random();
            string[] a = {"Cleveland", "Oakland", "Detroit", "Tampa", "Seattle", "Portland",
                "Denver", "Phoenix", "Kent", "Hollywood", "Buffalo"};
            string q = "";
            Label[] label;
    
    
            int guessRight = 0;
            int guessWrong = 0; 
            
            public Hangman()
            {
                InitializeComponent();
            }
            private void Hangman_Load(object sender, EventArgs e)
            {
                int n = 0;
                q = a[n];
                Guess.Visible = false;
    
    
                label = new Label[q.Length];
    
    
                int c = 0;
                for (int i = 0; i < label.Length; i++)
                {
                    label[i] = new Label();
                    label[i].AutoSize = true;
                    label[i].Location = new Point(200 + c, 150);
                    label[i].Font = Control.DefaultFont; 
                    label[i].ForeColor = Control.DefaultForeColor;
                    label[i].BackColor = Control.DefaultBackColor;
                    this.Controls.Add(label[i]); 
                    c += 57;
                }
            }

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    You need to set the labels Visible Property to true.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User sirama's Avatar
    Join Date
    Jun 2012
    Location
    Bangalore
    Posts
    7
    In addition to nvoigt:

    You set autosize property of the label to true and you does't set any display text to the label. Possibly you does't use the array you defined for display text of the label.


    Code:
       string[] a = {"Cleveland", "Oakland", "Detroit", "Tampa", "Seattle", "Portland",
                "Denver", "Phoenix", "Kent", "Hollywood", "Buffalo"};

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    If you're manipulating controls, you might also need to call Invalidate() (on the form works) to cause a repaint.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I would also recommend using a list of labels instead of an array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two-Dimensional Array of Labels
    By BobSlingblade in forum C++ Programming
    Replies: 1
    Last Post: 04-13-2011, 05:59 AM
  2. Array of Runtime Labels
    By wbeasl in forum C# Programming
    Replies: 3
    Last Post: 12-20-2008, 11:53 PM
  3. Dynamic Labels again!!
    By SuperNewbie in forum C# Programming
    Replies: 3
    Last Post: 07-11-2002, 12:36 AM
  4. Dynamic labels??
    By SuperNewbie in forum C# Programming
    Replies: 2
    Last Post: 07-09-2002, 01:31 AM
  5. Labels in C++?!!?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2001, 04:27 AM