Reputation: 45
Im working on adding a group of textbox each time a button is clicked, but the position of the next group of textbox should refer on the previously created textbox.
here is the screencap.
this is the first image before clicking the 'more' button
next is after clicking the more button once.
but after clicking again the more button. the space between each textbox is too far from the other.
I know something is wrong in my location code. can someone help me check this.
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
any help will be much appreciated. thanks in advance.
Upvotes: 0
Views: 84
Reputation: 32445
Use right tool for the job - DataGridView Control
Instead of calculating positions and adding textboxes you can just add a row in DataGridView
.
You can use two DataGridView
controls for "Requisition" and for "Issuance" groups.
In the designer create two DataGridView
and add columns you need. Then in
button click eventhandler you will add new row for both
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
If you go further you can use DataSource
property.
Create a class which represent one row of your data
Public Class Requisition
Public Property ItemNo As Integer
Public Property Unit As String
Public ItemDescription As String
Public Quantity As Decimal
End Class
Then create a BindingList(Of Requsition)
and add it as data source in the 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
Upvotes: 1
Reputation: 3003
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
Upvotes: 1