Removing controls from a form programatically

G

Guest

Hi,

I would modify a form from VB code by deleting some controls and add new ones.
But I cannot find an appropriet method.
For example:

forms!form1.controls("textbox1").delete

does not work because there is no delete method.

Thank's for your help

Gabor
 
A

Allen Browne

You can use the DeleteControl() method, but you will need the form in design
view.

In case you are not aware, you can change a control into a different type of
control by altering its ControlType.
 
G

Guest

Thank's!
It is working, though it's a bit strange that if I use a for cycle to delete
all the controls:
For Each ctlText In frm.Controls
Call DeleteControl(frm.name, ctlText.Name)
Next
then it deletes only the "first column" of controls. That is the cycle does
not go through all of the controls somehow.
By the way I may put the For cycle into a loop:
Do While frm.Controls.Count > 0
Loop


„Allen Browne†ezt írta:
 
M

MacDermott

Here's what I'd guess is happening:
Your For loop is equivalent to this one:
For i=0 to frm.Controls.Count-1

Now execution hits this for the first time, and deletes frm.Controls(0).
The item which had been Controls(1) becomes Controls(0).
So on the next pass through the loop (with i=1), the current Controls(1) is
deleted.
But the original Controls(1), now Controls(0), isn't touched.
The same logic continues to apply, so you end up with only half of your
controls deleted.

One alternative approach is to frame your For statement like this:
For i=frm.Controls.Count-1 to 0 Step -1

HTH
- Turtle
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top