Adding Controls at Runtime

It is possible to add controls to a userform at runtime. This is very convenient if you are not sure how many items you will be adding to the form.

Figure 21.8 shows a very plain form. It has only one button. This plain form is used to display any number of pictures from a product catalog. The pictures and accompanying labels appear at runtime, as the form is being displayed.

Figure 21.8

Flexible forms can be created if you add most controls at runtime.

Figure 21.8 shows a very plain form. It has only one button. This plain form is used to display any number of pictures from a product catalog. The pictures and accompanying labels appear at runtime, as the form is being displayed.

A sales rep making a sales presentation uses this form to display a product catalog. He can select any number of SKUs from an Excel worksheet and hit a hot key to display the form. If he selects 20 items on the worksheet, the form displays with each picture fairly small, as shown in Figure 21.9.

Figure 21.9

Here,the sales rep has asked to see photos of 20 SKUs.The UserForm_ Initialize procedure adds each picture and label on the fly.

Figure 21.9

Here,the sales rep has asked to see photos of 20 SKUs.The UserForm_ Initialize procedure adds each picture and label on the fly.

If the sales rep selects fewer items, then the images are displayed larger, as shown in Figure 21.10.

Figure 21.10

The logic in Userform_ Initialize decides how many pictures are being displayed and adds the appropriate size controls.

Figure 21.10

The logic in Userform_ Initialize decides how many pictures are being displayed and adds the appropriate size controls.

A number of techniques are used to create this userform on the fly. The initial form contains only one button, called cbClose. Everything else is added on the fly.

0 0

Responses

Post a comment