Thread: Creating an Array of Labels

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

    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?

    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()
            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;
                    c += 57;

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Hannover, Germany
    You need to set the labels Visible Property to true.

    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
    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.

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

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    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
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    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