简体   繁体   English

C# Windows 窗体帮助! 面板未显示

[英]C# Windows Form help! A panel is not displaying

I have to do a chessboard in C# windows form with panels.我必须在带有面板的 C# windows 窗体中做一个棋盘。 I have a button, two textboxs (to set the number of columns and rows) and one big panel where I will place the small panles.我有一个按钮、两个文本框(用于设置列数和行数)和一个大面板,我将在其中放置小面板。

This is the Button Click event:这是按钮单击事件:

private void button1_Click(object sender, EventArgs e)
        {
            int x, y, width, height, colors=0;
            Panel paneln;

            rows = Convert.ToInt32(textBox1.Text);
            columns = Convert.ToInt32(textBox2.Text);

            height = panel1.Size.Height / rows;
            width = panel1.Size.Width / columns;
            x = panel1.Location.X;
            y = panel1.Location.Y;

            for (int i=0;i<rows;i++)
            {
                for(int k = 0;k<columns;k++)
                {
                    paneln = new Panel();
                    paneln.Location = new Point(x, y);
                    paneln.Size = new Size(width, height);

                    if (colors % 2 == 0)
                        paneln.BackColor = Color.Black;
                    else
                        paneln.BackColor = Color.White;
                   
                    paneln.BringToFront();
                    panel1.SendToBack();
                    this.Controls.Add(paneln);

                    x += width;
                    colors++;
                }
                x= panel1.Location.X;
                y += height;
                if (columns % 2 == 0) colors++;
            }
            button1.Enabled = false;
        }

It works fine but if the columns value is even, the last panel (the bottom right one) is not displayed.它工作正常,但如果列值是偶数,则不会显示最后一个面板(右下角)。

For example with rows=4 and columns=4 i get this:例如,行 = 4 和列 = 4 我得到这个:

BWBW体重

WBWB白银

BWBW体重

WBW WBW

I tried to force the internal for to perform one more loop in the last row, but then it places two panels,which actually makes sense, and makes me think it actually creates the panel object but somehow it isn't shown.我试图强制内部 for 在最后一行再执行一个循环,但随后它放置了两个面板,这实际上是有道理的,并使我认为它实际上创建了面板对象,但不知何故它没有显示。

Any suggestion?有什么建议吗? thanks!谢谢!

Ok I figured it out!好吧,我想通了! I was doing the我在做

paneln.BringToFront();
panel1.SendToBack();

before adding paneln to the form, so it didn't work.在将面板添加到表单之前,它不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM