Programming III
Visual Inheritance
and User-Defined
Controls
Visual Inheritance and User
Controls
Upon completion of this work, the learner shall as a
means of designing and building a user-defined
control
The student shall be capable of creating .NET
components with Visual Studio .NET using a Class
Library project, and using the DLL files in C#
applications
Visual Inheritance(1/12)
Visual
inheritance enables you to achieve
visual consistency.
For example, you could define a base Form
that contains a products logo and a specific
background color.
You then could use the base Form throughout
an app for uniformity and branding.
Visual Inheritance(2/12)
Class VisualInheritanceBaseForm (Fig. 15.45)
derives from Form.
We use the public class VisualInheritanceBaseForm.
Use the namespace declaration that was created for us by
the IDE.
Right click the project name in the Solution Explorer and
select Properties, then choose the Application tab.
In the Output type drop-down list, change Windows
Application to Class Library.
Building the project produces the .dll.
Visual Inheritance(3/12)
Visual Inheritance(4/12)
Visual Inheritance(5/12)
To visually
inherit from
VisualInheritanceBaseForm, create a new
Windows Forms app.
In this app, add a reference to the .dll you
just created.
Modify the line that defines the class:
public partial class VisualInheritanceTestForm :
[Link]
Visual Inheritance(6/12)
Design view, the new apps Form should
now display the controls inherited from the
base Form (Fig. 15.46).
Class VisualInheritanceTestForm
(Fig. 15.47) is a derived class of
VisualInheritanceBaseForm.
In
Visual Inheritance(7/12)
Visual Inheritance(8/12)
10
Visual Inheritance(9/12)
11
Visual Inheritance(10/12)
12
Visual Inheritance(11/12)
13
Visual Inheritance(12/12)
14
User-Defined Controls
The
.NET Framework allows you to create
custom controls.
Custom controls appear in the users
Toolbox.
There are multiple ways to create a custom
control, depending on the level of
customization that you want (Fig. 15.49).
15
User-Defined Controls
16
User-Defined Controls
17
Example of User-Defined Control
In
[Link], there are many
places use user-defined control
18
Implement a User-Defined Control
19
Timer Control
20
User-Defined Controls
21
Timers are non-visual components that generate Tick events at
a set interval.
The Timers Interval property defines the number of
milliseconds between events.
Create a UserControl class for the project by selecting Project
> Add User Control .
We name the file (and the class) ClockUserControl.
Add a Label and a Timer to the UserControl.
Set the Timer interval to 1000 milliseconds.
clockTimer must be enabled by setting Enabled to true
User-Defined Controls
15.50 shows the output of Clock,
which contains our ClockUserControl.
Figure
22
User-Defined Controls
23
Consume the User Defined Control(1/7)
24
Consume the User Defined Control(2/7)
Right click
On General
25
Consume the User Defined Control(3/7)
26
Consume the User Defined Control(4/7)
27
Consume the User Defined Control(5/7)
28
Consume the User Defined Control(6/7)
29
Consume the User Defined Control(7/7)
30
Create a Windows Form Toolbox Control(1/7)
The
Windows Forms Toolbox Control template
creates a user defined control and provides all
of the functionality that is required to add the
control to the Toolbox.
31
Create a Windows Form Toolbox Control(2/7)
32
Create a Windows Form Toolbox Control(3/7)
33
Create a Windows Form Toolbox Control(4/7)
34
Create a Windows Form Toolbox Control(5/7)
35
Create a Windows Form Toolbox Control(6/7)
Press
F5 to build the project, and opens a
second Experimental instance of Visual Studio
that has the control installed.
In the Experimental instance of Visual Studio,
create a Windows Forms Application
project.
36
Create a Windows Form Toolbox Control(7/7)
37
References
.NET
Documentation
Textbook, Chap 15
https://
[Link]/en-us/library/[Link]
x
https://
[Link]/en-us/library/[Link]
x
[Link]
38