Link to home
Start Free TrialLog in
Avatar of broadbent
broadbentFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Removing a control

I have a form with an array of controls that I created.

Dim ik as Integer '  is the count of these controls

I want to remove the last control and I use this code to do so

    Controls.Remove(ListVItems(ik))       ' remove the control
      ListVItems(ik) = Nothing                 ' I'm not sure this is necessary
      ik -= 1                                          ' reduce the total
      ReDim Preserve ListVItems(ik)        '  reduce the array
      Refresh

but nothing happens.
What have I done wrong?
ASKER CERTIFIED SOLUTION
Avatar of Kinger247
Kinger247

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kinger247
Kinger247

Use RemoveByKey like:

Me.Controls.RemoveByKey("Button1")

or

Dim ControlToRemove as string = "Button1"
Me.Controls.RemoveByKey(ControlToRemove )
Avatar of Fernando Soto
Hi broadbent;

Arrays are zero base in .Net so if ik is the total number of controls in the array then the last control in the array is ( ik - 1 ).

Fernando
Avatar of broadbent

ASKER

You're quite right, I forgot to say that ik is set =-1

But the answer is quite simple. Just asking the question normally helps.
These controls are NOT on the form, but on a panel (so that I can use the AutoScroll)
MainPanel.Controls does work.

I'm an idiot, and I will happily give the points to Kinger247
RemoveKey was not the answer by the way.
Remove(index) was just as good