开发者

why doesnt my panel show all my buttons in my c# application?

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 alpha开发者_运维问答End = 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);
        }
    }


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)


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);
     }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜