Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing Dynamically Created Controls

Status
Not open for further replies.

Hackster

Programmer
Mar 28, 2001
173
US
I have a form in my app which allows the user to dynamically create up to 10 command buttons. This is working spendidly, including the coding for the dynamic buttons. The possible choices are listed in a checked Listbox, and they simply select which command button(s) they want at the moment, and they get created. What I need to do now, is to provide them a method of removing the button if they accidentially selected one they are going to be using. To create the buttons, I'm using this
Code:
Option Explicit
Private WithEvents btnButtonOne As CommandButton

Private Sub CreateButtonOne()
Set btnButtonOne = Controls.Add("VB.CommandButton", "btnButtonOne")
   With btnButtonOne
      .Visible = True
      .Width = 1000
      .Caption = "First Button"
      .Top = 1000
      .Left = 1000
   End With
End Sub
I've listed only one of the routines as an example and changed the caption for clarity sakes. What I can't seem to do, however, is to get the syntax correct for the Controls.Remove Blah Blah. Does anyone know how that works?
 
Unload <objectName>

Andy
--
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
 
Private Sub CleanIt()
Dim Box As TextBox
Dim I As Integer

bReady = False
I = 1
For Each Box In Locations
Set Box = Nothing
Call frmTest.Controls.Remove("txtBox" & I)
I = I + 1
Next
Set Locations = Nothing

Just change the reference from text boxes and this will rid you of all of the buttons. You can fiddle with this so that it only removes the ones you wish. Hope this helps...

LF
 
beat me too it...

However...

if you use the other method to dynamically create a control...
Such as you have an invisible CommandButton with Index of 0...
You can do this to create another:
Code:
N = Command1.Count
Load Command1(N)
With Command1(N)
  .Visible = True
  .Caption = "Button " & N
  .Move 1000 * N, 1000, 1000
End With

Then you can remove all but the original invisible control in the control array like this...
Code:
While Command1.Count > 1
  Unload Command1(Command1.Count - 1)
Wend

*You are not limited to any number (such as 10) with this method

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Homersim2600: Controls.Remove btnButtonOne
Set btnButtonOne = Nothing

works like a champ (I went all over the place with the Remove thing and it turned out to be so simple. LOL) Thanks!

AndyWatt: Unload returns an error when you attempt to use it on a dynamic control.
 
I have to say that I didn't write that code--I'm not that good--rather, a coworker did it for me on a project a few months back and I just happend to have it handy for ya. So Thanks Daryl If you are out there!

LOL!

LF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top