【问题标题】:Generating rows of panels with for loop in VB.NET在 VB.NET 中使用 for 循环生成面板行
【发布时间】:2017-06-16 06:24:42
【问题描述】:

我想使用 for 循环创建带有文本框的面板行。 面板将在另一个面板 (MajorPanel) 内创建。

for 循环的当前值用于为文本框赋值。

行数将由具有文本框 (RowNum) 的表单 (form2) 确定,用于输入主表单 (form1) 中所需的行数,并将该信息用于 for 循环的计数器,如下所示:

Public Class Form2

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Ok.Click

    Dim Rows As Integer
    Rows = RowNum.Text 'RowNum is where to input number of rows needed in form1

    Dim TxtBoxPanel As New Panel
    Dim LeftBox As New TextBox
    Dim CenterBox As New TextBox
    Dim RightBox As New TextBox
    Dim YAxis As Integer ' for adding TxtBoxPanel in new row

    For index = 1 To Rows

        'adding the textbox panel

        Form1.MajorPanel.Controls.Add(TxtBoxPanel)  'referring to form1 as panel needed in form1
        TxtBoxPanel.Name = ("txtBoxPanel" & index)
        TxtBoxPanel.Size = New Size(610, 32)
        YAxis = +32
        TxtBoxPanel.Location = New Point(3, YAxis)

        'adding left box
        TxtBoxPanel.Controls.Add(LeftBox)
        LeftBox.Name = ("LeftBox" & index)
        LeftBox.Text = (index)
        LeftBox.Size = New Size(100, 20)
        LeftBox.Location = New Point(3, 3)

        'adding center box
        TxtBoxPanel.Controls.Add(CenterBox)
        CenterBox.Name = ("CenterBox" & index)
        CenterBox.Text = (index)
        CenterBox.Size = New Size(100, 20)
        CenterBox.Location = New Point(258, 3)

        'adding right box
        TxtBoxPanel.Controls.Add(RightBox)
        RightBox.Name = ("RightBox" & index)
        RightBox.Size = New Size(100, 20)
        RightBox.Text = (index)
        RightBox.Location = New Point(495, 3)

    Next index

    Close()
End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles RowNum.TextChanged

End Sub
End Class

但是,当我执行时,面板会在另一个之上生成一个,如下所示:

After execution for 23 rows

这是我想要的结果:

Rows of panels within MajorPanel

form1 只有一行代码:

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Form2.Show()
End Sub
End Class

【问题讨论】:

    标签: vb.net winforms for-loop


    【解决方案1】:

    快速浏览一下,您似乎在使用 YAxis = +32,而您应该使用 YAxis += 32

    第一个只是将 YAxis 设置为值 32,第二个是将它增加 32,这就是你想要的(我假设)

    【讨论】:

      【解决方案2】:

      有两个问题:

      1- 您正在向 MajorPanel 添加相同的 TxtBoxPanel 实例,这意味着它将覆盖先前的位置,并且您最终将在 MajorPanel 的底部只有一个 TxtBoxPanel,因为您没有创建新的,而是更新获得的同一实例的坐标在开始循环之前创建。

      2- "YAxis = +32" 应该是 "YAxis +=32

      这里是更新的代码。它应该会给你想要的结果。

      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Ok.Click
      
              Dim Rows As Integer
              Rows = RowNum.Text 'RowNum is where to input number of rows needed in form1
      
      
              Dim YAxis As Integer ' for adding TxtBoxPanel in new row
      
              For index = 1 To Rows
      
                  Dim TxtBoxPanel As New Panel
                  Dim LeftBox As New TextBox
                  Dim CenterBox As New TextBox
                  Dim RightBox As New TextBox
      
                  'adding the textbox panel
      
                  Form2.MajorPanel.Controls.Add(TxtBoxPanel)  'referring to form1 as panel needed in form1
                  TxtBoxPanel.Name = ("txtBoxPanel" & index)
                  TxtBoxPanel.Size = New Size(610, 32)
                  YAxis +=32
                  TxtBoxPanel.Location = New Point(3, YAxis)
      
                  'adding left box
                  TxtBoxPanel.Controls.Add(LeftBox)
                  LeftBox.Name = ("LeftBox" & index)
                  LeftBox.Text = (index)
                  LeftBox.Size = New Size(100, 20)
                  LeftBox.Location = New Point(3, 3)
      
                  'adding center box
                  TxtBoxPanel.Controls.Add(CenterBox)
                  CenterBox.Name = ("CenterBox" & index)
                  CenterBox.Text = (index)
                  CenterBox.Size = New Size(100, 20)
                  CenterBox.Location = New Point(258, 3)
      
                  'adding right box
                  TxtBoxPanel.Controls.Add(RightBox)
                  RightBox.Name = ("RightBox" & index)
                  RightBox.Size = New Size(100, 20)
                  RightBox.Text = (index)
                  RightBox.Location = New Point(495, 3)
      
              Next index
      
              Close()
          End Sub
      

      【讨论】:

      • 您是对的,因为按照您和@Magnus 的建议将 ''YAxis=+32'' 更改为 ''YAxis +=32'' 后,只会生成最后一个面板。那么如何在不覆盖以前的情况下创建“TxtBoxPanel”的所有其他实例和文本框?
      • 我已与错误修复共享代码。你能用吗?我已将控件的声明移到循环中,因此它将为每次迭代创建所有必需控件的新实例。
      • @Amin84,由于该解决方案对您有效,因此应将其标记为答案。
      猜你喜欢
      • 2020-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-21
      • 2013-12-16
      • 1970-01-01
      • 2020-03-30
      • 2016-09-11
      相关资源
      最近更新 更多