我正在尝试使用flowlayoutpanel以编程方式从上到下组织一对标签文本框。我想要得到的是类似于下图:
所以我已经实现了下面的代码(我需要创建254个标签 - 文本框对):
Dim lbl As Label
Dim txt As TextBox
Dim flowLayout As FlowLayoutPanel
For i As Integer = 0 To 253
lbl = New Label
lbl.Text = i.ToString("000") + ":"
lbl.Padding = New Padding(0)
lbl.Margin = New Padding(0)
txt = New TextBox
txt.Text = "<" + i.ToString.PadLeft(3, " ") + ">"
txt.MaxLength = 5
txt.Margin = New Padding(0)
txt.Size = New Size(39, 20)
flowLayout = New FlowLayoutPanel
flowLayout.FlowDirection = FlowDirection.LeftToRight
flowLayout.Controls.Add(lbl)
flowLayout.Controls.Add(txt)
flowLayout.Padding = New Padding(0)
flowLayout.Margin = New Padding(0)
Me.FlowLayoutPnl.Controls.Add(flowLayout)
Next
但是使用上面的代码我得到以下内容:
有什么想法吗?
答案 0 :(得分:0)
如果我了解您正在寻找的内容,此代码应该会为您提供预期的结果。
Dim flowLayout As New FlowLayoutPanel
flowLayout.AutoScroll = True
For i = 0 To 253
Dim label As New Label
label.AutoSize = True
label.Padding = New Padding(10, 5, 5, 10)
label.Text = i.ToString("000 ") + ":"
Dim txt As New TextBox
txt.Text = "Input " + i.ToString
txt.MaxLength = 5
flowLayout.Controls.Add(label)
flowLayout.Controls.Add(txt)
Next
Controls.Add(flowLayout)
flowLayout.Dock= DockStyle.Fill
顺便说一下,您发布的所需图片显示了一对标签,而不是标签/ TextBox。关于代码,您必须首先创建主容器(FlowLayoutPanel),然后在进行itterationm时,您必须将每个元素添加到容器中。最后,您需要将FlowLayoutPanel添加到表单控件中以显示在表单上。
答案 1 :(得分:0)
我已使用以下代码解决了这个问题:
Private Sub PopupForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load
Dim lbl As Label
Dim txt As TextBox
Dim flowLayout As FlowLayoutPanel
Dim g As Graphics
For i As Integer = 0 To 253
lbl = New Label
lbl.Text = i.ToString("000") + ":"
lbl.Anchor = AnchorStyles.None
lbl.AutoSize = True
txt = New TextBox
txt.Text = "<" + i.ToString.PadLeft(3, " ") + ">"
txt.MaxLength = 5
txt.Anchor = AnchorStyles.None
txt.ReadOnly = True
g = txt.CreateGraphics
txt.Width = g.MeasureString(txt.Text, txt.Font).Width + 5
g.Dispose()
flowLayout = New FlowLayoutPanel
flowLayout.FlowDirection = FlowDirection.LeftToRight
flowLayout.AutoSize = True
flowLayout.Anchor = AnchorStyles.None
flowLayout.Margin = New Padding(0)
flowLayout.Padding = New Padding(0)
flowLayout.Controls.Add(lbl)
flowLayout.Controls.Add(txt)
Me.FlowLayoutPnl.Controls.Add(flowLayout)
Next
End Sub
,结果为this
注意:
我的FlowLayoutPnl是在设计时创建的,具有以下属性(其他属于默认值):