Remove multiple programmatically created controls

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
 
H

hunanwarrior

Sorry, forgot the question ;>)

Anyone have any ideas on how to resolve this issue?

Thanks so much.
 
R

rowe_newsgroups

Sorry, forgot the question ;>)

Anyone have any ideas on how to resolve this issue?

Thanks so much.

Why not use a List(Of Textbox) variable and add each created text box
to it. Then when you want to remove those textboxes do some like this:

<pseudocode>

for each tb as TextBox in TempTextBoxesList
if me.controls.contains(tb) then
me.controls.remove(tb)
end if
next

</pseudocode>

Thanks,

Seth Rowe
 
C

Charles May

what about something like ...
Select Case TextBox1.Text

Case "X", "Y", "Z"

MsgBox("XYZ")

Case "A", "B", "C"

MsgBox("ABC")

Case Else

MsgBox("None of the options were entered")

End Select



CM
 
C

Charles May

Sorry, wrong thread :(

Charles May said:
what about something like ...
Select Case TextBox1.Text

Case "X", "Y", "Z"

MsgBox("XYZ")

Case "A", "B", "C"

MsgBox("ABC")

Case Else

MsgBox("None of the options were entered")

End Select



CM
 
R

rowe_newsgroups

what about something like ...
Select Case TextBox1.Text

Case "X", "Y", "Z"

MsgBox("XYZ")

Case "A", "B", "C"

MsgBox("ABC")

Case Else

MsgBox("None of the options were entered")

End Select

CM

Huh?

Thanks,

Seth Rowe
 
H

hunanwarrior

Thanks for the response, Seth.

<<Why not use a List(Of Textbox) variable and add each created text
box
to it. >>

Do you have an example of how to do this?

Thanks
 
R

rowe_newsgroups

Thanks for the response, Seth.

<<Why not use a List(Of Textbox) variable and add each created text
box
to it. >>

Do you have an example of how to do this?

Thanks

Simple just change the code that adds the textboxes to this:

' Here's your variable for holding the list of textboxes
Private MyTempTextboxes as new List(Of Textbox)()

'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
MyTempTextboxes.Add(tmpTextBox.Left)
Me.Controls.Add(tmpTextBox)
Next
End Sub

That should be it!

Thanks,

Seth Rowe
 
P

Phill W.

For Each c As Control In Me.Controls
If TypeOf(c) Is TextBox Then
Me.Controls.Remove(c)
c.Dispose()
End If

This is an Age-old problem. You're looping through a Collection and
removing items /from/ that Collection as you go. The iterator - that's
doing the looping for you - is /ignoring/ your removals and following
the /original/ list of controls (from when it started looping), which is
why it's skipping some.

Solution 1:
Loop /backwards/ through the control (yes, you have to muck about with a
subscript) removing the Controls as you go.

Solution 2: Create your own "collection" of textboxes as you add them
(independent of the Controls property), then loop through that and
remove them from the Form, something like

Private m_DynamicTextBoxes as ArrayList

' Add to above as you load the TextBoxes, then

For Each tb As Textbox In m_DynamicTextBoxes
Me.Controls.Remove(tb)
Next

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top