|
-
Jun 6th, 2008, 07:17 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2005] Deleting TextBoxes
Hello All,
OK, with help I'm now able to add additional TextBoxes during 'Run-Time' now I need to, depending on a certain condition, remove some or all of those created. I did a search and found code from Hack that clears them but I cannot find anything on deleting them, can anyone offer such coding?
Rgds,
Tarablue
-
Jun 6th, 2008, 08:06 PM
#2
Re: [2005] Deleting TextBoxes
its something along these lines
vb Code:
textbox1.dispose
me.controls.remove(textbox1)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 6th, 2008, 08:09 PM
#3
Re: [2005] Deleting TextBoxes
The parent container (ie the form) has a Controls Property that is a collection of controls. This has a remove method that accepts a control as an argument or a RemoveAt Method that removes the control at a specified index
Example
Code:
'Remove a control object
Me.Controls.Remove(Me.TextBox1) 'Removes TextBox1
'remove a control by it's index
Me.Controls.RemoveAt(0) 'Removes the first control in the collection
-
Jun 6th, 2008, 09:25 PM
#4
Thread Starter
Hyperactive Member
Re: [2005] Deleting TextBoxes
Hello .paul & bmahler,
Unfortunately both methods are looking at TextBoxes that are on the form to start with, ie,BEFORE 'Run-Time' and not those that are created during 'Run-Time'.
If you create TextBox7 at 'Run-Time' then by placing in your code
Code:
Me.Controls.Remove(Me.TextBox1) 'Removes TextBox7
will only causes an error because TextBox7 is not a member of Form1, or is not Declared.
I'm trying to clear the extra TextBoxes from;
Code:
Private Sub TextBox1_enter(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles TextBox1.Enter
Me.Controls.Remove(Me.TextBox7)
End Sub
Best Rgds,
Tarablue
Last edited by Tarablue; Jun 6th, 2008 at 09:29 PM.
-
Jun 6th, 2008, 09:42 PM
#5
Re: [2005] Deleting TextBoxes
How are you adding in these controls at run time? are you adding them to the forms control collection? if not then they will not be displayed so not really sure why you need to remove them? if you are just looking to destroy the instance of them you can call the dispose method on the textBox object. If you post your code for how you are adding them it will be much easier to help you.
-
Jun 6th, 2008, 10:02 PM
#6
Thread Starter
Hyperactive Member
Re: [2005] Deleting TextBoxes
Hello bmahler,
Here's my code so far and is for just for getting around the problem, it will be incorporated into a much larger program.
Code:
Public Class Form1
Public TextLeft As Integer = 6
Public TextRight As Integer = 7
Private Sub TextBox2_enter(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles TextBox2.Enter
Dim M, N, A, TextNumberLeft, TextNumberRight As Integer
TextLeft = 6
TextRight = 7
M = Val(TextBox1.Text)
N = M + 1
A = 26
N = Val(TextBox1.Text) + 1
If N > 3 Then
For i As Integer = 1 To M - 2
Dim tb1 As New TextBox
TextNumberLeft = TextLeft + i
With tb1
.Name = "NumericalTextBox" & TextNumberLeft
.Text = TextNumberLeft
.TabStop = True
.TabIndex = TextNumberLeft
.Size = New Size(34, 20)
.Location = New Point(58, (30 + (i * A)))
End With
Me.Controls.Add(tb1)
TextLeft = TextLeft + 1
Dim tb2 As New TextBox
TextNumberRight = TextRight + i
With tb2
.Name = "NumericalTextBox" & TextNumberRight
.Text = TextNumberRight
.TabStop = True
.TabIndex = TextNumberRight
.Size = New Size(34, 20)
.Location = New Point(115, (30 + (i * A)))
End With
Me.Controls.Add(tb2)
TextRight = TextRight + 1
Next
Else : Exit Sub
End If
End Sub
End Class
On my form I have 2 TextBoxes towards the bottom of the form - TextBox1 & TextBox2. Enter 2 into TextBox1 and then click or tab to TextBox2, nothing happens. Enter 3, 4 or 5 and additional TextBoxes appear.
It is these that I want to remove and replace with new ones that are dependant on the value of TextBox1. For instance, if you enter 4 in TextBox1 boxes 7,8,9,10 appear. If you enter 3 in TextBox1 only 7 & 8 should appear.
I hope that's clear.
Best Rgds,
Tarablue
-
Jun 7th, 2008, 03:44 AM
#7
Re: [2005] Deleting TextBoxes
bmahler has already told you what to do: call the Remove or RemoveAt method fo the form's Controls collection. You're adding the new TextBoxes to the form's Controls collection with its Add method so you can certainly remove them with its Remove method.
Now, if you call Remove then you need to pass a reference to the actual TextBox you want removed. If you call RemoveAt you have to pass its index in the forms Controls collection, which corresponds to its z-order index.
If you want a reference to the TextBox itself then you can index the Controls collection by name. You might also keep a separate List or Dictionary of TextBoxes you've added. If you want to get the index of a particular control then you know that (Me.Controls.Count - 1) is the index of the last control you added.
-
Jun 7th, 2008, 08:23 AM
#8
Thread Starter
Hyperactive Member
Re: [2005] Deleting TextBoxes
Hello jmcilhinney,
Yes, I tried both the methods that bmahler offered and the only items that were removed were those from the original form layout, none of the added TextBoxes were affected. As bmahler says;
Code:
Me.Controls.RemoveAt(0)
Removes the first control in the collection and if I increase (0) past the index value of those items on the original form I get the error Index is out of range. That is why I offered my code up to give a clearer picture of how the TextBoxes are created.
Best Rgds,
Tarablue
-
Jun 7th, 2008, 12:34 PM
#9
Re: [2005] Deleting TextBoxes
I'm sorry but you are mistaken. How can you get an IndexOutOfRangeException if you specify a valid index? If you've added the TextBox to the Controls collection then it can't possibly not have an index. Here's the proof:
1. Create a new WinForms project.
2. Add two Buttons to the form.
3. Add this code:
Code:
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles Button1.Click
Dim tb As New TextBox
tb.Location = New Point(100, 100)
Me.Controls.Add(tb)
End Sub
Private Sub Button2_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles Button2.Click
Dim tb As Control = Me.Controls(Me.Controls.Count - 1)
tb.Dispose()
Me.Controls.Remove(tb)
End Sub
4. Run the project.
5. Click Button1 and see the TextBox appear.
6. Click Button2 and see the TextBox disappear.
If it's not working for you then you're simply using the wrong index. As I said, Count-1 is the index of the last control you added. Once that's removed then Count-1 will be the index of the control you added before that. If you add N TextBoxes then you can remove the control at index Count-1 N times and they will all be removed.
-
Jun 7th, 2008, 05:45 PM
#10
Thread Starter
Hyperactive Member
Re: [2005] Deleting TextBoxes
Hello jmcilhinney,
OK, your code makes it much clearer for me and does work, thank you. You have added 2 lines that are crucial to it;
Dim tb As Control = Me.Controls(Me.Controls.Count - 1)
tb.Dispose()
that I didn't have before.
I have incorporated it into a TextBox1_Leave sub function.
Thank you.
Best Rgds,
Tarablue
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|