WinForms动态矩阵带有按钮的新表单

时间:2018-10-18 15:52:39

标签: c# winforms

因此,我设法以一种新的形式创建了一个动态的文本框数组,并在其下面有一个按钮,但是该按钮与某些文本框重叠,如屏幕截图所示:

http://prntscr.com/l7ln4q enter image description here

我想向数组添加边距和填充,以便按钮位于数组下方 这是生成数组的代码:

    int LinhaText = frmDim.linhas;
    int ColunText = frmDim.colunas;

    const int TEXTBOX_WIDTH = 30;
    const int TEXTBOX_HEIGHT = 30;
    const int SPACING = 0;
    List<List<TextBox>> textboxes = new List<List<TextBox>>();

    private void Form2_Load(object sender, EventArgs e)
    {
        for (int row = 0; row < LinhaText; row++)
        {
            List<TextBox> newRow = new List<TextBox>();
            textboxes.Add(newRow);
            for (int col = 0; col < ColunText; col++)
            {

                TextBox newbox = new TextBox();
                newbox.Width = TEXTBOX_WIDTH;
                newbox.Height = TEXTBOX_HEIGHT;
                newbox.Top = (row * (TEXTBOX_HEIGHT + SPACING)) + SPACING;
                newbox.Left = (col * (TEXTBOX_WIDTH + SPACING)) + SPACING;
                newRow.Add(newbox);
                this.Controls.Add(newbox);

            }
        }

    }

数组的生成工作正常。

1 个答案:

答案 0 :(得分:0)

如果您只想要一个简单的解决方案,请尝试以下操作:

    private void Form1_Load(object sender, EventArgs e)
    {
        Panel p = new Panel();        // added code    

        for (int row = 0; row < LinhaText; row++)
        {
            List<TextBox> newRow = new List<TextBox>();
            textboxes.Add(newRow);
            for (int col = 0; col < ColunText; col++)
            {

                TextBox newbox = new TextBox();
                newbox.Width = TEXTBOX_WIDTH;
                newbox.Height = TEXTBOX_HEIGHT;
                newbox.Top = (row * (TEXTBOX_HEIGHT + SPACING)) + SPACING;
                newbox.Left = (col * (TEXTBOX_WIDTH + SPACING)) + SPACING;
                newRow.Add(newbox);
                p.Controls.Add(newbox);    // modified code (added textboxes to panel rather than form)

            }
        }

        // added code
        p.Dock = DockStyle.Fill;
        this.Controls.Add(p);
        this.Controls.SetChildIndex(p, 0);
        Button b1 = new Button();
        b1.Text = "hi";
        b1.Dock = DockStyle.Bottom;
        this.Controls.Add(b1);
        this.Controls.SetChildIndex(b1, 1);
    }