views:

67

answers:

2

my panel in my windows form application doesn't include all the buttons i asked it to. It shows only 1 button, Here is the code

 private void AddAlphaButtons()
        {
            char alphaStart = Char.Parse("A");
            char alphaEnd = Char.Parse("Z");

        for (char i = alphaStart; i <= alphaEnd; i++)
        {
            string anchorLetter = i.ToString();
            Button Buttonx = new Button();
            Buttonx.Name = "button " + anchorLetter;
            Buttonx.Text = anchorLetter;
            Buttonx.BackColor = Color.DarkSlateBlue;
            Buttonx.ForeColor = Color.GreenYellow;
            Buttonx.Width = 30;
            Buttonx.Height = 30;

            this.panelButtons.Controls.Add(Buttonx);

            //Buttonx.Click += new System.EventHandler(this.MyButton_Click);
        }
    }
+6  A: 

Aren't they all going to be on the same position?

Try setting Buttonx.Location = new Point(100, 200);

(but with different points for different buttons)

Paul
A: 

You could use a FlowLayoutPanel, which would take care of the layout for you, or you need to track the locations yourself, or which could look something like this:

private void AddAlphaButtons()
{
    char alphaStart = Char.Parse("A");
    char alphaEnd = Char.Parse("Z");   

    int x = 0;  // used for location info
    int y = 0;  // used for location info

    for (char i = alphaStart; i <= alphaEnd; i++)
    {
        string anchorLetter = i.ToString();
        Button Buttonx = new Button();
        Buttonx.Name = "button " + anchorLetter;
        Buttonx.Text = anchorLetter;
        Buttonx.BackColor = Color.DarkSlateBlue;
        Buttonx.ForeColor = Color.GreenYellow;
        Buttonx.Width = 30;
        Buttonx.Height = 30;

        // set button location
        Buttonx.Location = new Point(x, y);

        x+=30;
        if(x > panel1.Width - 30)
        { 
            x = 30;
            y+=30;
        }

        this.panelButtons.Controls.Add(Buttonx);

        //Buttonx.Click += new System.EventHandler(this.MyButton_Click);
     }
}
JohnForDummies
That did it!!Thanks a lot, u r truly a programmer
Fortubeks