CPS613 |
VB .NET Techniques and References | |
User-Defined Controls |
Types of User-Defined Controls |
Properties |
Miscellaneous |
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.
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:
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