我想使用for循环创建带有文本框的面板行。 面板将在另一个面板(MajorPanel)中创建。
for循环的当前值用于为文本框赋值。
行数将由一个表单(form2)确定,该表单具有一个文本框(RowNum)来输入主窗体(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
然而,当我执行时,面板会生成另一个,如图所示:
这是我想要的结果:
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
答案 0 :(得分:0)
快速浏览一下,当您使用YAxis = +32
时,看起来您正在使用YAxis += 32
。
第一个是将YAxis
设置为值32,第二个是将它递增32,这是你想要的(我假设)
答案 1 :(得分:0)
有两个问题:
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