Hi,

I need to have all the letters of the alphabet displayed across my tabpage, with each letter clickable. Can someone pls help me; I can't get this right, I only have the letter A displayed:

Label[] lblalpha = null;
            lblalpha = new Label[26];
            string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            char[] array = alphabet.ToCharArray();

            for(int i=0; i < array.Length; i++)
            {
                char letter = array[i];
                lblalpha[i] = new Label();
                lblalpha[i].Visible = true;
                lblalpha[i].Text = letter.ToString();
                lblalpha[i].Location = new Point(74 + 2*i, 51);
                lblalpha[i].Cursor = Cursors.Hand;
                lblalpha[i].Enabled = true;                
                this.tabPage2.Controls.Add(lblalpha[i]);
                lblalpha[i].Click += new System.EventHandler(lblalpha_Click);
            }

Appreciate so much any help. Thanks in advance!

Recommended Answers

All 5 Replies

This will do it:

public partial class Form1 : Form
    {
        Label[] lbls;
        public Form1()
        {
            InitializeComponent();

            char[] letters = Enumerable.Range('A', 'Z' - 'A' + 1).Select(i => (Char)i).ToArray();
            lbls = new Label[letters.Length];
            int x = 20;
            int y = 20;
            for (int i = 0; i < letters.Length; i++)
            {
                lbls[i] = new Label();
                lbls[i].Name = "label" + letters[i];
                lbls[i].Text = letters[i].ToString();
                lbls[i].Location = new Point(x, y);
                lbls[i].AutoSize = false;
                lbls[i].Size = new Size(15, 13);
                lbls[i].Cursor = Cursors.Hand;
                lbls[i].Click += new EventHandler(labels_Click);
                this.tabPage1.Controls.Add(lbls[i]);
                x += 16;
            }
        }

        private void labels_Click(object sender, EventArgs e)
        {
            Label lb = sender as Label;
            MessageBox.Show("You have clicked " + lb.Text);
        }
    }

Wow, thanks thanks so much Mitja! It does help. I'm really grateful. I'm tweaking my code; I want to understand what's the difference between your code and mine. And I found out that I lack this line:

lbls.Size = new Size(15, 13);

I added that, and just retained my code inside my form_load, and it now works. Why do you think is that? :=)

You put create labels one beside another (lets say a few inches apart), and because you didnt disable AutoSize property, it means that the 1st label has Width size of 35 (35 is defualt), so your code only shows 1st letter A, other were hidden. Am I correct?

That is becuase your 2nd label (B letter) was hidden behind label of letter A.
My code has set the AutoSize to false, and set new width Size to 15. 15 is the width of a letter (and a bit more, so you dont have letters just one beside another).

Yup! That's exactly what happened, only letter A showed. Thanks so much for this! :=)

And I thought that was taken cared of by the .Location, spaced wide apart :=)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.