Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 向groupbox添加控件时出现问题_Vb.net - Fatal编程技术网

Vb.net 向groupbox添加控件时出现问题

Vb.net 向groupbox添加控件时出现问题,vb.net,Vb.net,我正在vb.net中的groupbox中创建一个文本框网格。它们的宽度、高度、字体等都是统一的。。我目前拥有的代码如下: Dim new_cell As New TextBox With new_cell .Multiline = True .Width = cell_width .Height = cell_height .Font = ("arial", 12) End With For j = 0 To

我正在vb.net中的groupbox中创建一个文本框网格。它们的宽度、高度、字体等都是统一的。。我目前拥有的代码如下:

 Dim new_cell As New TextBox
 With new_cell
        .Multiline = True
        .Width = cell_width
        .Height = cell_height
        .Font = ("arial", 12)
    End With

    For j = 0 To N
        For i = 0 To M
                new_cell.Left = (i * cell_width) + i
                new_cell.Top = (j * cell_width) + j
                space.Controls.Add(new_cell)
        Next
    Next
其中M,N是网格大小,空格是groupbox。 本应创建整个网格,但它仅在下角创建一个文本框。这是因为在循环的下一次迭代中对新的_单元所做的更改会影响上一个控件,因此不会添加新控件。我可以用数组替换单个textbox,然后循环并将textbox数组的每个元素添加到groupbox。然而,这会产生难看的代码,而且效率似乎很低(每个单元格都有重复的属性),因此我想知道是否有办法将控件添加到groupbox,然后解除textbox变量与添加到groupbox的textbox之间的关联(或其他什么)。

您只创建了一个
textbox
。您需要在
i
循环中移动
Dim new_单元格作为新文本框

For j = 0 To N
    For i = 0 To M

        Dim new_cell As New TextBox

        With new_cell
            .Multiline = True
            .Width = cell_width
            .Height = cell_height
            .Font = ("arial", 12)
        End With

        new_cell.Left = (i * cell_width) + i
        new_cell.Top = (j * cell_width) + j
        space.Controls.Add(new_cell)

    Next
Next
或者更好:

Dim cell_x As Integer = space.DisplayRectangle.Left
Dim cell_y As Integer = space.DisplayRectangle.Top
Dim cell_i As Integer = 0 ': Cell `i` spacing
Dim cell_j As Integer = 0 ': Cell `j` spacing

For j = 0 To N
    For i = 0 To M
        space.Controls.Add(New TextBox() With {
            .Name = String.Format("N{0}M{1}", j, i),
            .Multiline = True,
            .Width = cell_width,
            .Height = cell_height,
            .Font = New Font("arial", 12),
            .Left = (cell_x + (i * (cell_width + cell_i))),
            .Top = (cell_y + (j * (cell_height + cell_j)))
        })
    Next
Next