CPS613

VB .NET Techniques and References

Toronto Metropolitan University University

User-Defined Controls


Table of Contents

Types of User-Defined Controls

Properties

Miscellaneous


Types of User-Defined Controls

Introduction to User-Defined Controls

User-Defined controls are controls that are not provided by .NET but are instead developped by the programmer, usually working with existing controls. Visual Studio provides two types of controls to support this process: In this course, we use composite controls, and extended custom controls that inherit their functionality from an existing control.

When you define your own control, you will need to compile it before you can use it in the designer view of another form or control. Once compiled, it will be available in the toolbox under the "ProjectName" section (where ProjectName is the name of the project where you defined the control)

Other information on user-defined controls can be found at Developing Custom Windows Forms Controls

Composite Controls

Composite controls are controls that are created by putting other controls together into one new control.

In visual Studio, you can create a new composite control by selecting Project > Add User Control. Then you develop it just like a form by dragging and dropping other controls into it in the designer, and modifying its code. More details.

Visual Studio 2022 bug: If the font of the labels in your composite control are not the same as the font of the composite control, this can seriously disrupt the functionality of the composite control because its elements will not necessarily end up being painted where you designed them. In particular, some may end up outside of the control, which renders them invisible. To avoid this, simply pick one font for the entire composite control and define that font in the font property of the composite control. Once you have done this, all the controls dragged into the composite control will use that font by default and the built control will look exactly as it was designed.

As mentioned above, you will need to Build your solution and then the new control will be available in the Toolbox.

Extended Controls

An extended control is a control that is based on another existing control but is more specialised. It is called an "extended" control because it extends the functionality of another control.

Since .NET is an OOP environment, this simply means that an extended control is a subclass of an existing control. By default the extended control inherits all the behaviours of the control it extends. However, you may override some of methods inherited from the superclass. You can also add new methods, properties, behaviours etc. Extending a control is a good way to take advantage of all the builtin functionality of controls provided in .NET while still being able to customize them to your own needs.

In visual Studio, you can create a new extended control by selecting Project > Add New Item > Custom Control. (not user control this time1)

Then, to make it a subclass of another existing control, open its .Designer.vb file from the Solution Exlorer and modify the Inherits statement to specify which control it should inherit from.

Unlike for composite controls, you cannot drag and drop other controls into an extended control in design view, but you can modify some of its properties in the designer. You can get more details at How to inherit from existing Windows forms controls.


Properties

User-Defined Properties

Composite controls are subclasses of the control class and therefore automatically inherit all the properties of the class Control. Extended controls also automatically inherit all the properties of the control being extended. In addition to these properties, you can define your own properties for your new controls, and these properties will work exactly like other properties: you will be able to change them statically in the Properties window of the Visual Studio designer or dynamically in VB code.

General information on properties can be found at: Properties in Windows Forms Controls.
However, this older explanation is more helpful as it provides sample code with good explanations: Properties Overview.

User-Defined Properties that Change a Control's Layout

As you have noticed when you used built-in controls, you can make significant changes to the layout of a control by changing its properties either in the Properties window of the designer or in VB code. You can make your user-defined properties just as powerful as the built-in properties.

If you want to have a property that can be used to change the layout of a control, you can do the following:


Miscellaneous

Dynamic Control Generation and Ownership

When you need to generate a big set of similar controls and set properties for them at the same time it sometimes makes sense to generate them dynamically in your code rather than in the designer. This is because you can use a loop to generate them, and also because you can use a constructor with parameters to generate them, which is something you cannot do from the designer.

If you decide to do this, you can usually create all the controls in a loop in the constructor of the form you are putting them in, and save them in an array so you can access them from the form. However all this does is to make them programmatically accessible from the form; it does not automatically make them visible in the form. To do this, you must specify that they belong to the form, or to a container in the form.

For example, suppose that you have created a Game form for a game like chess or checkers and this Game form will contain a panel called Board which will contain a 2-D array of BoardCells (a user-defined control that you've already created). Here is how you make sure that a cell called mycell shows up in the correct position (x,y) in Board:

    dim mycell as BoardCell = new BoardCell(x,y) 'The constructor for BoardCell would need to set the location of the cell to be x,y
    Board.Controls.add(mycell)                   'Controls is the list of all controls that belong to a panel or a form
    mycell.BringToFront()                        'This is to make sure that mycell is not hidden behind something else

Example of User-Defined Controls and Properties

  • Here is an example of user controls. This example is a VB solution with control library containing two user-defined controls. After you have loaded the solution in VB, open Form1 in the Designer and follow the instructions. Feel free to look at and copy as much of the code in this example as you want.


    This page is maintained by Sophie Quigley (cps613@cs.torontomu.ca)
    Last modified Wednesday, 13-Sep-2023 04:22:48 EDT