Results 1 to 10 of 10

Thread: [RESOLVED] [2005] Deleting TextBoxes

  1. #1

    Thread Starter
    Hyperactive Member Tarablue's Avatar
    Join Date
    Feb 2007
    Location
    Somewhere West of GMT
    Posts
    398

    Resolved [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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: [2005] Deleting TextBoxes

    its something along these lines

    vb Code:
    1. textbox1.dispose
    2. me.controls.remove(textbox1)

  3. #3
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    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
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  4. #4

    Thread Starter
    Hyperactive Member Tarablue's Avatar
    Join Date
    Feb 2007
    Location
    Somewhere West of GMT
    Posts
    398

    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.

  5. #5
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    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.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  6. #6

    Thread Starter
    Hyperactive Member Tarablue's Avatar
    Join Date
    Feb 2007
    Location
    Somewhere West of GMT
    Posts
    398

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member Tarablue's Avatar
    Join Date
    Feb 2007
    Location
    Somewhere West of GMT
    Posts
    398

    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

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Hyperactive Member Tarablue's Avatar
    Join Date
    Feb 2007
    Location
    Somewhere West of GMT
    Posts
    398

    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
  •  



Click Here to Expand Forum to Full Width