CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2002
    Location
    Toronto
    Posts
    167

    Removing controls

    Hi ...

    Here's what I have ... Panel1 which I have used


    me.panel1.controls.add(lbl) to add 10 labels to panel1

    now I am trying to get rid of them using

    for x = 1 to 10

    me.panel1.controls.remove(controls(0))

    Next

    and though it runs the code 10 times, all of the controls are still there, please let me know what I am doing wrong

    Thanks

  2. #2
    Join Date
    Oct 2002
    Location
    Växjö, Sweden
    Posts
    225
    You have to get a reference to the object to be deleted. If you do like this it will work:

    Dim lbl As Label
    lbl.Name = "TestLabel"
    Me.Controls.Add(lbl)

    .....

    Me.Controls.Remove(lbl)

    .... because you still have the variable lbl referenced to the added control.

    So you have to do something like this:

    Dim con As control
    For Each lbl In Me.Controls
    If con.Name = "TestLabel" then Me.Controls.Remove(con)
    Next

    I think you get it. Bottom line: Either way you choose to do this you have to get a reference to the object being deleted.

    /Leyan

  3. #3
    Join Date
    Oct 2002
    Location
    Växjö, Sweden
    Posts
    225
    of course it should be:

    For Each con In Me.Controls

    sorry.

    /Leyan

  4. #4
    Join Date
    May 2002
    Location
    Toronto
    Posts
    167
    ok I have tried this and I'm still not removing labels

    in my on_click for add a line of labels

    it gets all the text, and positions of a label then goto addlabel

    private sub addlabel

    Dim lbl As New Label()
    'for the long box which will equal the control count

    lbl.Width = w
    lbl.Name = "whatever"
    lbl.BackColor = Me.BackColor
    lbl.Text = LabelText
    lbl.Height = 14
    lbl.TextAlign = ta
    lbl.Anchor = Label9.Anchor
    lbl.Left = l
    lbl.Top = distance
    lbl.Visible = True
    Me.Panel1.Controls.Add(lbl)
    end sub

    it does this 8 times to add the 8 new labels of the line

    Then when someone clicks Delete Line I use the code

    dim cntl as Control
    dim counter as integer

    cntl = Me.Panel1.Controls(0)
    For counter = 1 To 8
    If cntl.Name = "whatever" Then Me.Panel1.Controls.Remove(cntl)
    end if

    next
    exit sub

    and the labels are still there. I've tried probably 60 different interprutations of that remove line and still the labels are still there

  5. #5
    Join Date
    May 2002
    Location
    Toronto
    Posts
    167
    ok attempt number 3002 I have done this and it seems to work

    for counter = 1 to 8
    for each cntl in me.panel1.controls
    if cntl is me.panel1.controls(0) then me.panel1.controls.remove(cntl)
    next
    next



    is that good coding practice ? it seems so ... complicated for such an easy task

  6. #6
    Join Date
    Oct 2002
    Location
    Växjö, Sweden
    Posts
    225
    Well, as I see it, it's because cntl does not get a reference to the control. Do you have to use a standard For-loop?

    I had the following code in one button.....

    Dim lbl As New Label()
    lbl.Location = New System.Drawing.Point(20, 25)
    Me.Panel1.Controls.Add(lbl)

    .....and the following code in another button......

    Dim pan As Panel = Me.Panel1
    Dim con As Control
    For Each con In pan.Controls
    If con.GetType.ToString = "System.Windows.Forms.Label" Then
    pan.Controls.Remove(con)
    End if
    Next

    .... and this removes all labels added by the first button.

    /Leyan

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

Featured