473,511 Members | 15,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Remove multiple programmatically created controls

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

Feb 1 '07 #1
8 3721
Sorry, forgot the question ;>)

Anyone have any ideas on how to resolve this issue?

Thanks so much.

Feb 1 '07 #2
On Feb 1, 1:04 pm, hunanwarr...@gmail.com wrote:
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

Feb 1 '07 #3
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

<hu**********@gmail.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
>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

Feb 1 '07 #4
Sorry, wrong thread :(

"Charles May" <nu***@bidniz.comwrote in message
news:7Tqwh.13834$yB5.9970@trndny03...
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

<hu**********@gmail.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
>>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


Feb 1 '07 #5
On Feb 1, 2:06 pm, "Charles May" <n...@bidniz.comwrote:
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

<hunanwarr...@gmail.comwrote in message

news:11**********************@a75g2000cwd.googlegr oups.com...
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
Huh?

Thanks,

Seth Rowe

Feb 1 '07 #6
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

Feb 1 '07 #7
On Feb 1, 3:05 pm, hunanwarr...@gmail.com wrote:
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

Feb 1 '07 #8
hu**********@gmail.com wrote:
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.
Feb 2 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4579
by: Tzvika | last post by:
Hi, Is there a way to programmatically add/remove windows components in c# or in any other language What is the API fro that?
9
2329
by: james.e.coleman | last post by:
Hello, I have created a custom dropdownlist that is used multiple times within a single page. When trying to set the values of the controls with the page in which they are being used, they all...
6
1663
by: David Tilman | last post by:
I need to create some tables on a web page at run time rather than design time. There could be 1 - 5 tables on the page depending on options. I've created the tables by creating the cells, rows,...
5
15269
by: Brian McClellan | last post by:
Just wondering if anyone has a simple example of creating a gridview completely programmatically, i'm not doing anything terribly sophisticated. When creating the gridview declaratively evertying...
0
1166
by: TB | last post by:
Hi All: I have this page where a rows / cells are programmatically added to to table by pushing a button. The rows contain a textbox and a associated button. What I want to is to be able to...
0
1310
by: mark.norgate | last post by:
Hi I'm having a problem in adding controls to a page programmatically in response to a button click. Composite user controls added programmatically in the CreateChildControls() method work...
4
8444
by: Bob | last post by:
Hi, I'm working with VWD and i defined programmatically a button (in code-behind) with an ID. So I expect to see beside "Page Events" and "Form1" my button "b1" in order to use the Click event....
1
3488
by: =?Utf-8?B?U3RlcGhhbmllIERvaGVydHk=?= | last post by:
Can anyone tell me how to programmatically remove a control from a tab page from VB .Net? The control was added using: Me.TabPage1.Controls.Add(pb) but I don't know what its index number is to...
4
1484
by: Siv | last post by:
Hi, I have an application that reads information from a database and depending on that information creates controls on a form. Depending on where the user is at in a process there may be one item...
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.