Solved

clear all dynamically created controls

Posted on 2002-07-10
11
141 Views
Last Modified: 2010-05-02
In VBA I was doing something like:

MyForm.Controls.Clear

to clear all the controls on this form.

In VB, if I do that I get an error saying the "Object is not supported". I figured out how to add them in VB but I want to get rid of all of them at once. Can I do this?

To make it more complicated I'm not actually trying to clear controls off a form but clear the controls off a tabstrip (SSTab). Is there another way to do this or am I doing something wrong? Thanks,

guidway
0
Comment
Question by:guidway
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
Try this:
dim c
for each c in MyForm.COntrols
  c.Value = empty
next

CHeers
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
sorry, disregard my comment. You try to remove the control as such, not clear the values.

To clear all the controls, I would load a new instance of the "empty" form, be aware that controls that you added at design time cannot be removed by VB code as such.

CHeers
0
 
LVL 12

Author Comment

by:guidway
Comment Utility
The controls are all created at runtime. I'm not actually using a form, all the controls are on a tabstrip.

guidway
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
>>I'm not actually using a form
you must be using a form to hold the tabstrip?!
so as i suggested, reload your form should work fastest.
CHeers
0
 
LVL 12

Author Comment

by:guidway
Comment Utility
I see your point. I tried this but the problem is that some of the controls on the form and SSTab are created at design time. Only one tab on the SSTab has controls that are created at runtime. If I try reloading (unloading/loading) the form again it does not work.

Here's my code:

dim withevents lab as VB.label
dim withevents chk as VB.checkbox

Private Sub SSTab1_Click(PreviousTab as Integer)

  if SSTab1.Tab = 1 then

     Select Case (MyForm.EntryLabelType)
         Case "Type"
            set lab = Controls.Add("VB.Label", "labSwitch")
            set lab.Container = SSTab1
            SSTab1.Tab = 1
            lab.Caption = "Switches: "
            lab.Move 40,565, 1300
            lab.Visible = true
     end Select
  end if
end sub

Basically what I'm trying to do is when I click on this tab some controls are dynamically created. I want to make sure that the controls are not already there so the best way of doing this is just by clearing them. If the user wants they can pull up the form again but if they selected different options before they load the form then different controls will get loaded into the tab (that's the reason I have the switch statement). I'm not sure I'm following you on reloading the form. If you are talking about doing a Unload/Load on it I tried that and it locked up the project. Please be more specific. Thanks for the help,

guidway
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 12

Author Comment

by:guidway
Comment Utility
Since I add them with a Controls.Add I really don't see why Controls.Clear won't work. It doesn't make sense to me.

guidway
0
 
LVL 12

Author Comment

by:guidway
Comment Utility
I figured it out! All I needed to do was do a test to see if the control existed using this:

if lab is nothing then
   set lab = Controls.Add("VB.Label", "lab1")
end if

That way I don't even have to unload the control. If it exists it won't try to recreate it. I can't believe I didn't think of that earlier. Thanks anyway,

guidway
0
 
LVL 8

Expert Comment

by:DennisBorg
Comment Utility
guidway:

You would use the REMOVE Method of the Controls Collection to unload your dynamically-created controls.

For example:

   Controls.Remove "lab1"

Please note that the Remove method can only be used with dynamically created controls.

The Controls collection, by design, does not support the CLEAR method.

0
 
LVL 8

Expert Comment

by:DennisBorg
Comment Utility
guidway:

One thing you could do to make managing it easier is to add a reference for each control you create dynamically to a collection.

To illustrate:

   Dim colCtrls       As Collection
   Dim WithEvents lab As VB.Label
   Dim WithEvents chk As VB.Checkbox


   Set colCtrls = New Collection
   Set lab = Controls.Add("VB.Label", "labSwitch")
   Set chk = Controls.Add("VB.Checkbox", "MyChkBox")

Then later, to unload those dynamically created controls:

   Dim Index As Long

   For Index = 1 To colCtrls.Count
      Controls.Remove colCtrls(Index).Name
   Next Index
   Set colCtrls = New Collection


0
 
LVL 12

Author Comment

by:guidway
Comment Utility
Thanks guys for the suggestions however I'm on a time limit and don't have time to implement them. Unless anyone has a complaint I'm just going to have a mod delete the question. I ended up answering my own question. ;-) Thanks again though.

guidway
0
 
LVL 6

Accepted Solution

by:
Mindphaser earned 50 total points
Comment Utility
Points refunded and moved to PAQ

** Mindphaser - Community Support Moderator **
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now