guidway
asked on
clear all dynamically created controls
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
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
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
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
ASKER
The controls are all created at runtime. I'm not actually using a form, all the controls are on a tabstrip.
guidway
guidway
>>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
you must be using a form to hold the tabstrip?!
so as i suggested, reload your form should work fastest.
CHeers
ASKER
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
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
ASKER
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
guidway
ASKER
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
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
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.
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.
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
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"
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
ASKER
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
guidway
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
dim c
for each c in MyForm.COntrols
c.Value = empty
next
CHeers