我每次点击按钮时都会添加一组文本框,但下一组文本框的位置应该参考之前创建的文本框。
这是screencap。
但再次点击更多按钮后。每个文本框之间的空间距离另一个文本框太远。
我知道我的位置代码有问题。有人可以帮我查一下吗。
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addbox.Click
Dim Count = gbox2.Controls.OfType(Of TextBox)().ToList().Count
For i As Integer = 1 To 6
Dim txtbox As New TextBox()
Select Case i
Case 1
txtbox.Location = New System.Drawing.Point(17, 30 * Count)
txtbox.Size = New System.Drawing.Size(43, 26)
txtbox.Name = "itemno2" & (Count + 1)
gbox2.Controls.Add(txtbox)
Case 2
txtbox.Location = New System.Drawing.Point(81, 30 * Count)
txtbox.Size = New System.Drawing.Size(43, 26)
txtbox.Name = "unit2" & (Count + 1)
gbox2.Controls.Add(txtbox)
Case 3
txtbox.Location = New System.Drawing.Point(142, 30 * Count)
txtbox.Size = New System.Drawing.Size(254, 26)
txtbox.Name = "itemdesc2" & (Count + 1)
gbox2.Controls.Add(txtbox)
Case 4
txtbox.Location = New System.Drawing.Point(417, 30 * Count)
txtbox.Size = New System.Drawing.Size(56, 26)
txtbox.Name = "requan2" & (Count + 1)
gbox2.Controls.Add(txtbox)
Case 5
txtbox.Location = New System.Drawing.Point(21, 30 * Count)
txtbox.Size = New System.Drawing.Size(56, 26)
txtbox.Name = "issuequan2" & (Count + 1)
gbox3.Controls.Add(txtbox)
Case 6
txtbox.Location = New System.Drawing.Point(94, 30 * Count)
txtbox.Size = New System.Drawing.Size(144, 26)
txtbox.Name = "remarks2" & (Count + 1)
gbox3.Controls.Add(txtbox)
End Select
Next
'addbox.Visible = False
save.Visible = False
End Sub
任何帮助将不胜感激。提前谢谢。
答案 0 :(得分:1)
Dim currentTop As Integer = 5
Dim defaultHeight As Integer = 26
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Count = gbox2.Controls.OfType(Of TextBox)().ToList().Count
currentTop += defaultHeight + 5
For i As Integer = 1 To 6
Dim txtbox As New TextBox()
Select Case i
Case 1
txtbox.Location = New System.Drawing.Point(17, currentTop)
txtbox.Size = New System.Drawing.Size(43, 26)
txtbox.Name = "itemno2" & (Count + 1)
gbox2.Controls.Add(txtbox)
Case 2
txtbox.Location = New System.Drawing.Point(81, currentTop)
txtbox.Size = New System.Drawing.Size(43, 26)
txtbox.Name = "unit2" & (Count + 1)
gbox2.Controls.Add(txtbox)
Case 3
txtbox.Location = New System.Drawing.Point(142, currentTop)
txtbox.Size = New System.Drawing.Size(254, 26)
txtbox.Name = "itemdesc2" & (Count + 1)
gbox2.Controls.Add(txtbox)
Case 4
txtbox.Location = New System.Drawing.Point(417, currentTop)
txtbox.Size = New System.Drawing.Size(56, 26)
txtbox.Name = "requan2" & (Count + 1)
gbox2.Controls.Add(txtbox)
Case 5
txtbox.Location = New System.Drawing.Point(21, currentTop)
txtbox.Size = New System.Drawing.Size(56, 26)
txtbox.Name = "issuequan2" & (Count + 1)
gbox3.Controls.Add(txtbox)
Case 6
txtbox.Location = New System.Drawing.Point(94, currentTop)
txtbox.Size = New System.Drawing.Size(144, 26)
txtbox.Name = "remarks2" & (Count + 1)
gbox3.Controls.Add(txtbox)
End Select
Next
End Sub
答案 1 :(得分:1)
使用正确的工具进行工作 - DataGridView Control
您可以在DataGridView
中添加一行,而不是计算位置和添加文本框
您可以使用两个DataGridView
控件来执行"申请"并且发行"发行"基团。
在设计器中创建两个DataGridView
并添加所需的列。然后进去
按钮单击事件处理程序,您将为两个
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles addbox.Click
RequisitionDataGridView.Rows.Add(new object[] {value1, value2, value2 })
IssuanceDataGridView.Rows.Add(new object[] {value6, value7})
End Sub
如果你走得更远,你可以使用DataSource
财产
创建一个代表一行数据的类
Public Class Requisition
Public Property ItemNo As Integer
Public Property Unit As String
Public ItemDescription As String
Public Quantity As Decimal
End Class
然后创建一个BindingList(Of Requsition)
并将其作为数据源添加到DataGridiView
Public Class YourForm Inherits Form
Private _requsitionData As New BindingList(Of Requsition)
Private _issuanceData As New BindingList(Of Issuance)
Public Sub New()
InitializeComponents()
RequisitionDataGridView.DataSource = _requsitionData
IssuanceDataGridView.DataSource = _issuanceData
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles addbox.Click
_requsitionData.Add(new Requisition())
_issuanceData.Add(new Issuance())
End Sub
End Class