Adding Controls Dynamically

Adding controls dynamically allows you complete flexibility over your user interface. VB6 supported dynamic control addition, but the addition of controls was inconsistent with the model of declaring an object, creating a new instance of the object, and adding that object to a parent container.

VB6 supported adding controls to a form using a syntax that is more consistent with late-binding COM syntax than an object creation syntax (see Listing 16.7 for a VB6 example). Dynamic control creation in Visual Basic .NET is close enough that you should be able to make the transition easily enough (see Listing 16.8).

Listing 16.7. Dynamic control creation in VB6
						1:  Option Explicit
2:
						3: Private WithEvents Button As CommandButton ...

Get Visual Basic® .NET Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.