如何在 VB.net 中的 TableLayoutPanel 内组织 Groupbox 中的控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17995456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to organize controls in a Groupbox inside a TableLayoutPanel in VB.net
提问by Arch
I've run into an interesting problem with Windows form application design that I'm having some difficulty resolving. I am currently working on a program that is supposed to be a digital replacement for a certain checklist that my company uses. This should be straightforward, but it's been mandated that the program should use the contents of an SQL database to determine what items should be on the checklist in the program (making it easy to change the items in the checklist). This means that I have to dynamically design and create much of my application using code instead of my Visual Studio 2010 designer.
我在 Windows 窗体应用程序设计中遇到了一个有趣的问题,我在解决这个问题时遇到了一些困难。我目前正在开发一个程序,该程序应该是我公司使用的某个清单的数字替代品。这应该很简单,但是程序必须使用 SQL 数据库的内容来确定程序中的检查清单上应该包含哪些项目(从而可以轻松更改检查清单中的项目)。这意味着我必须使用代码而不是 Visual Studio 2010 设计器来动态设计和创建我的大部分应用程序。
I've been able to figure out the SQL stuff and even most of the dynamic design stuff, but there is one complex issue I can't seem to defeat. To create the appearance of a checklist, I dynamically create a TableLayoutPanel with two columns and one row. I then grab my data from the database and create a groupbox that is captioned with the instruction that the person using the program needs to do. I also add a textbox with a label, a checkbox, or both to the groupbox depending on the database entry. Finally, I add the textbox, label, and/or checkbox to the groupbox, then add a row to my table and add the groupbox to the table in that row. However, all of the controls in the groupbox are mashed into the top left corner of the row, so I try to adjust their organization using their .location property. Unfortunately, the moment I alter that property, the affected control disappears from the form completely. I have tried using the .bringtofront() method with no success; the control is still missing.
我已经能够弄清楚 SQL 的内容,甚至大多数动态设计的内容,但是有一个我似乎无法克服的复杂问题。为了创建清单的外观,我动态创建了一个两列一行的 TableLayoutPanel。然后我从数据库中获取我的数据并创建一个分组框,该分组框带有使用该程序的人需要执行的指令的标题。我还根据数据库条目向分组框添加了一个带有标签、复选框或两者的文本框。最后,我将文本框、标签和/或复选框添加到 groupbox,然后向我的表中添加一行并将 groupbox 添加到该行中的表中。但是,groupbox 中的所有控件都混杂在行的左上角,因此我尝试使用它们的 .location 属性调整它们的组织。很遗憾,在我更改该属性的那一刻,受影响的控件将从表单中完全消失。我尝试使用 .bringtofront() 方法但没有成功;控件仍然缺失。
What I am looking for is guidance on how to organize dynamically created controls in a dynamically created groupbox (or any other grouping control) that is itself contained in another dynamically created grouping control. I would really appreciate any assistance that I can get; this problem is preventing me from getting to work on the functionality of the program. The applicable code is posted below. Thanks in advance!
我正在寻找的是关于如何在动态创建的 groupbox(或任何其他分组控件)中组织动态创建的控件的指导,该 groupbox 本身包含在另一个动态创建的分组控件中。我真的很感激我能得到的任何帮助;这个问题阻止我开始处理程序的功能。适用的代码发布在下面。提前致谢!
Private Sub addItem(ByVal count As Integer, ByVal itemList As List(Of checklistField))
If itemList(count).hasTextbox Or itemList(count).hasCheckbox Then
Dim newGroupbox As New GroupBox
With newGroupbox
.Dock = DockStyle.Fill
.Text = itemList(count).instruction
End With
If ((itemList(count).hasTextbox = True) And (itemList(count).hasCheckbox = False)) Then
Dim newTextboxLabel As New Label
With newTextboxLabel
.Text = itemList(count).textboxLabel
'.Location = New Point(20, (475))
.AutoSize = True
End With
Dim newTextbox As New TextBox
With newTextbox
.Name = "Textbox" & count.ToString
.Size = New Size(100, 20)
'.Location = New Point(100, (470 + (10 * count)))
End With
textboxList.Add(newTextbox)
newGroupbox.Controls.Add(newTextboxLabel)
newGroupbox.Controls.Add(newTextbox)
tblFields.RowCount += 1
tblFields.RowStyles.Add(New RowStyle(SizeType.AutoSize))
tblFields.Controls.Add(newGroupbox, 0, tblFields.RowCount - 1)
采纳答案by LarsTech
Your commented out Location properties have the Y value set too high, so the controls are not visible on the form:
您注释掉的 Location 属性的 Y 值设置得太高,因此控件在表单上不可见:
'.Location = New Point(20, (475))
'.Location = New Point(100, (470 + (10 * count)))
The Location of the control is in relation to the parent's client space, so use smaller numbers:
控件的位置与父级的客户端空间有关,因此请使用较小的数字:
With newTextboxLabel
.Text = itemList(count).textboxLabel
.Location = New Point(20, 20)
.AutoSize = True
End With
Dim newTextbox As New TextBox
With newTextbox
.Name = "Textbox" & count.ToString
.Size = New Size(100, 20)
.Location = New Point(20, 40)
End With

