H
hunanwarrior
I added textbox controls to a form when user selects amount to create
from a combobox as follows:
'Load up the combobox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim i As Integer
For i = 1 To 10
cbxItemCnt.Items.Add(i)
Next
End Sub
'Add textbox controls based upon selection
Private Sub cbxItemCnt_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cbxItemCnt.SelectedIndexChanged
Dim i As Integer
Dim tmpTextBox As TextBox
For i = 1 To cbxItemCnt.SelectedItem
tmpTextBox = New TextBox
tmpTextBox.Name = "txtBox" & i
tmpTextBox.Top = cbxItemCnt.Top + (25 * i)
tmpTextBox.Left = cbxItemCnt.Left
Me.Controls.Add(tmpTextBox)
Next
End Sub
When I try to remove the controls, I get undesirable results (i.e. If
4 textboxes were created, only textbox 1 & 3 are removed. Debugging
returns c = Nothing on these iterations)
'Remove programmatically created textboxes
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnClear.Click
For Each c As Control In Me.Controls
If TypeOf (c) Is TextBox Then
Me.Controls.Remove(c)
c.Dispose()
End If
Next
End Sub
from a combobox as follows:
'Load up the combobox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim i As Integer
For i = 1 To 10
cbxItemCnt.Items.Add(i)
Next
End Sub
'Add textbox controls based upon selection
Private Sub cbxItemCnt_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cbxItemCnt.SelectedIndexChanged
Dim i As Integer
Dim tmpTextBox As TextBox
For i = 1 To cbxItemCnt.SelectedItem
tmpTextBox = New TextBox
tmpTextBox.Name = "txtBox" & i
tmpTextBox.Top = cbxItemCnt.Top + (25 * i)
tmpTextBox.Left = cbxItemCnt.Left
Me.Controls.Add(tmpTextBox)
Next
End Sub
When I try to remove the controls, I get undesirable results (i.e. If
4 textboxes were created, only textbox 1 & 3 are removed. Debugging
returns c = Nothing on these iterations)
'Remove programmatically created textboxes
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnClear.Click
For Each c As Control In Me.Controls
If TypeOf (c) Is TextBox Then
Me.Controls.Remove(c)
c.Dispose()
End If
Next
End Sub