Results 1 to 12 of 12

Thread: Removing Controls At Run Time

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2011
    Posts
    19

    Removing Controls At Run Time

    I Put together this bit of code. What the problem im having is Removing a Label after i place the code its in a select end slecte code . The Problem is in red.

    the code

    Code:
    
    
            Dim Animals As String = (TextBox1.Text)
            Select Case Animals
    
                Case "Apples"
    
    
          Dim userinput1 As Integer = InputBox("Enter The Farm X Location")
                 Dim userinput2 As Integer = InputBox("Enter The Farm Y Location")
                 Dim userinput3 As Integer = InputBox("Enter The Farm Widgth Size")
                 Dim userinput4 As Integer = InputBox("Enter The Farm Hidgth Size")
                    Dim userinput5 As String = InputBox("Please Choose Farm Name")
                    Dim LB As New Label()
                    LB.Name = userinput5
                    Me.Controls.Add(LB)
                    LB.Location = New Point(userinput1, userinput2)
                    LB.Size = New Size(userinput3, userinput4)
                    LB.BackColor = Color.Red
                    LB.Text = "Apples"
    
    
    
                Case "Sell"
    
           Dim userinput1 As String = InputBox("Please Choose A Orchard To Sell")
           Dim LB As New Label()
            Me.Controls.Remove(LB)
           LB.Name = userinput1
     
       
    End Select

    Everything works Accept the remove selection ... I think it has something to do with Dim LB As New Label() ... And ... Me.Controls.Remove(LB)
    Last edited by JAC81FTW; Jun 12th, 2011 at 05:49 PM. Reason: Typo

  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    39

    Re: Removing Controls At Run Time

    It seems your creating a new label, then removing it. Do you mean to remove a label that's already on your form?

    Try:
    lblName.Dispose()

    or perhaps

    lblName.Visible = False

    From what I can tell, you need to refer to the label that's on your form- not a new one. The new label has not been assigned a parent or location, so it's not going to be in Me.Controls

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Removing Controls At Run Time

    First off I would highly suggest turning Option Strict On under Project properties. You have a variable userinput1 declared as Integer then later using it as a string.

    The following code under Case Apples reflects how you might use InputBox but you would be better to use TextBoxes as it can be frustrating to keep having to press OK and go from dialog to dialog with no chance to change something in a prior dialog if a mistake was made.

    I have to ask, what is an Apple animal and what about sell?

    Code:
            Select Case Animals
                Case "Apples"
                    Dim userinput1 As Integer = CInt(InputBox("Enter The Farm X Location"))
                    Dim userinput2 As Integer = CInt(InputBox("Enter The Farm Y Location"))
                    Dim userinput3 As Integer = CInt(InputBox("Enter The Farm Widgth Size"))
                    Dim userinput4 As Integer = CInt(InputBox("Enter The Farm Hidgth Size"))
                    Dim userinput5 As String = InputBox("Please Choose Farm Name")
                    Dim LB As New Label()
                    LB.Name = userinput5
                    Me.Controls.Add(LB)
                    LB.Location = New Point(userinput1, userinput2)
                    LB.Size = New Size(userinput3, userinput4)
                    LB.BackColor = Color.Red
                    LB.Text = "Apples"
                Case "Sell"
                    Dim LB As New Label()
                    Me.Controls.Remove(LB)
                    LB.Name = userinput1
            End Select
    In the following userinput1 is out of scope and as mention above is declared Integer but used below to set a String property. Secondly you just created the label, it has not been added to the form's control collection so it can not be removed.

    Code:
                Case "Sell"
                    Dim LB As New Label()
                    Me.Controls.Remove(LB)
                    LB.Name = userinput1
    If you want to remove a label here is one method which looks into the current form's control collection for a label named Label1.
    Code:
    Dim SomeLabel = Me.Controls.Find("Label1", True)
    If SomeLabel IsNot Nothing Then
        Me.Controls.Remove(SomeLabel(0))
    End If

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2011
    Posts
    19

    Re: Removing Controls At Run Time

    I can add and edit the lb that being placed i just dont no how to delte it after i set it up hmmm gona take some thinking



    ok i tryed the dispose that didnt work i tride the visable that didnt work hmm

    i may have to add in a label thats a control in stead of using a new lable

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Removing Controls At Run Time

    Quote Originally Posted by JAC81FTW View Post
    I can add and edit the lb that being placed i just dont no how to delte it after i set it up hmmm gona take some thinking



    ok i tryed the dispose that didnt work i tride the visable that didnt work hmm

    i may have to add in a label thats a control in stead of using a new lable
    If you look at my reply it shows how to remove a control as in your original question along with how to fix several issues with your code.

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Removing Controls At Run Time

    Quote Originally Posted by Zombie666 View Post
    It seems your creating a new label, then removing it. Do you mean to remove a label that's already on your form?

    Try:
    lblName.Dispose()

    or perhaps

    lblName.Visible = False

    From what I can tell, you need to refer to the label that's on your form- not a new one. The new label has not been assigned a parent or location, so it's not going to be in Me.Controls
    That is not possible as the label was declared using a localized variable and the name is not available for the same reason. If the name is known that the label can be removed using code in my original reply.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jun 2011
    Posts
    19

    Re: Removing Controls At Run Time

    Sorry about that we posted at the same time lol i read it but did a little resarch on this in these post and it workes

    what you did works but only on one label im going to need 8 different labels with out having 8 labels which is why i did the lb as new label i can add as many lb = labels as i want but with only using a few my brother "said codeing ant fun lol im actully having fun lol anyways" it works but how would i add in 8 different lbls i can now have lbl 1 move and dispoes but i need 7 more ill take a look at your code


    ps tyos for the others

  8. #8
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Removing Controls At Run Time

    Quote Originally Posted by JAC81FTW View Post
    Sorry about that we posted at the same time lol i read it but did a little resarch on this in these post and it workes

    what you did works but only on one label im going to need 8 different labels with out having 8 labels which is why i did the lb as new label i can add as many lb = labels as i want but with only using a few my brother "said codeing ant fun lol im actully having fun lol anyways" it works but how would i add in 8 different lbls i can now have lbl 1 move and dispoes but i need 7 more ill take a look at your code


    ps tyos for the others
    Sorry but adding controls as you are will get you nowhere. Again what I gave you will work with one or 100 labels, the difference is how you search for the labels you want to remove.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jun 2011
    Posts
    19

    Re: Removing Controls At Run Time

    i could use a list box that stores the the options in farming could i rename the labels in a listbox

    like i select a option "apples" and it goes to the textbox from there i think i can rename the label one thats in a cotrol to lblapples

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jun 2011
    Posts
    19

    Re: Removing Controls At Run Time

    lol give me time to replay lol your fast i like that

  11. #11
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Removing Controls At Run Time

    Attached working example form in VS2008, Framework 3.5

    Extract from attachement
    Code:
    Public Class Demo
        Dim ListOfLabels As New List(Of Label)
        Private Sub cmdAdd_Click( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles cmdAdd.Click
    
            If (From C In ListOfLabels Where C.Name = txtName.Text).Count = 0 Then
                Dim LB As New Label()
                '
                ' No need to set width which is why the form controls for width are disabled
                ' but if you want to set the width then so be it, enable those controls.
                '
                LB.AutoSize = True
                LB.Name = txtName.Text
                LB.Location = New Point(CInt(LeftNumericUpDown.Value), CInt(TopNumericUpDown.Value))
                LB.BackColor = Color.Red
                LB.Text = txtLabelText.Text
                Me.Controls.Add(LB)
                ListOfLabels.Add(LB)
                ListBox1.Items.Add(LB.Name)
                ListBox1.SelectedIndex = ListBox1.Items.Count - 1
            Else
                MsgBox(String.Concat(txtName.Text, " already exists"))
            End If
    
            cmdRemove.Enabled = ListBox1.Items.Count > 0
    
        End Sub
        Private Sub cmdRemove_Click( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles cmdRemove.Click
    
            If ListBox1.Items.Count > 0 Then
                If ListBox1.SelectedIndex <> -1 Then
                    Dim Item = CType(Me.Controls.Find(ListBox1.Text, True)(0), Label)
                    Me.Controls.Remove(Item)
                    ListOfLabels.Remove(Item)
                    ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
                    If ListBox1.Items.Count > 0 Then
                        ListBox1.SelectedIndex = 0
                    End If
                End If
            End If
    
            cmdRemove.Enabled = ListBox1.Items.Count > 0
    
        End Sub
    End Class

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jun 2011
    Posts
    19

    Re: Removing Controls At Run Time

    Thanks didnt have to wite the hole thing but thanks you did a good job thanks

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