Adding controls at runtime

  • Thread starter Thread starter Michael S. Montoya
  • Start date Start date
M

Michael S. Montoya

I have to add Button controls to a form on runtime. Basically the user
enters the number of rows and number of columns and the buttons will be
created. The code I have seen so far creates a new form each time and I
don't see how this would work as the name of the form would change.

Any ideas.

Thanks in advance.
Michael S. Montoya
 
Michael said:
I have to add Button controls to a form on runtime. Basically the user
enters the number of rows and number of columns and the buttons will be
created. The code I have seen so far creates a new form each time and I
don't see how this would work as the name of the form would change.

That is not a good approach. First, the form must be open
in design view to use CreateControl, then, since forms are
large objects, repeatedly modifying a form will cause no end
of bloat, and worst of all, it greatly increases the
probability of corruption.

The usual way to deal with a situation like this is to have
a large number of invisible controls on the form. Then make
them visible as needed. With this approach, it's a good
idea to name the controls with a common prefix and numeric
suffix such as btn0101 where the numbers are the row and
column indexex. This allow you to reference the controls
using this kind of syntax:

Me("btn" & Format(row,"00") & Format(col,"00")).Visible=True
 
Back
Top