为什么我的面板不显示 C# 应用程序中的所有按钮?

发布于 2024-09-03 21:23:15 字数 785 浏览 8 评论 0原文

我的 Windows 窗体应用程序中的面板不包含我要求的所有按钮。它只显示 1 个按钮,这是代码

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说好的呢 2024-09-10 21:23:16

难道他们不都会站在同一个位置吗?

尝试设置 Buttonx.Location = new Point(100, 200);

(但不同的按钮有不同的点)

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)

傲娇萝莉攻 2024-09-10 21:23:16

您可以使用 FlowLayoutPanel,它会为您处理布局,或者您需要自己跟踪位置,或者可能看起来像这样:

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

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);
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文