Lesson 3: Common Tasks Using Controls

Lesson 3: Common Tasks Using Controls

There are a variety of tasks common to each type of control you develop. In this lesson, you will learn about some of these tasks, such as adding your control to the toolbox, displaying it in Microsoft Internet Explorer, debugging it, providing a Toolbox bitmap for your control, and managing licensing.

After this lesson, you will be able to

  • Add your control to the Toolbox

  • Provide a Toolbox bitmap for your control

  • Debug your control

  • Manage control licensing

  • View your control in Internet Explorer

Estimated lesson time: 20 minutes

Adding Your Control to the Toolbox

Once you have created a control, you will want to be able to work with it in your development projects. Adding your control to the Toolbox makes it accessible and easy to work with and allows you to add it to your projects at design time.

You can add a new control to the Toolbox by using the Customize Toolbox option. To access this option, right-click the Toolbox and choose Customize Toolbox, which launches the Customize Toolbox dialog box. From there, you can browse to the file that contains your control.

To add your control to the Toolbox

  1. Right-click the Toolbox, and choose Customize Toolbox. The Customize Toolbox dialog box opens.

  2. Choose the .NET Framework Components tab, and click the Browse button to open the File dialog box.

  3. Browse to the folder with the DLL or EXE file that contains your control. Select the file, and click Open.

  4. Verify that your control has been added to the list displayed in the Customize Toolbox dialog box, and click OK. Your control is added to the Toolbox.

NOTE
In Visual Studio .NET 2003, user controls built as a part of your project are automatically added to the Toolbox under the My UserControls tab. You can still add inherited controls and custom controls using the aforementioned procedure.

Providing a Toolbox Bitmap for Your Control

Visual Studio .NET provides a default icon that appears next to your controls in the Toolbox. You might, however, want to specify your own icon in the form of a bitmap. The following procedure details how to specify a bitmap to be used in the Toolbox with your control.

You specify a Toolbox bitmap by using the ToolboxBitmapAttribute class. This class is an attribute a specialized class that provides metadata about your control. Using the ToolboxBitmapAttribute, you can specify either a 16-by-16-pixel bitmap file to be used as your Toolbox bitmap or you can specify a Type (type). If you specify a Type (type), your control will have the same Toolbox bitmap as that of the Type (type) you specified.

The ToolboxBitmapAttribute is attached to the class declaration of the control. In Visual Basic .NET, it appears in angle brackets (<>) before the class declaration on the same line. In Microsoft Visual C#, it should go within braces ([]) on the line immediately preceding the class declaration for the control.

To provide a Toolbox bitmap for your control by specifying a file

Use the ToolboxBitmapAttribute to specify the file that contains the bitmap, as follows:

Visual Basic .NET

<ToolboxBitmap("C:\Pasta.bmp")> Public Class PastaMaker ' Implementation omitted End Class

Visual C#

[ToolboxBitmap(@"C:\Pasta.bmp")] public class PastaMaker : Control { // Implementation omitted }

To provide a Toolbox bitmap by specifying a type

Use the ToolboxBitmapAttribute to specify the type whose bitmap you want to provide, as follows:

Visual Basic .NET

<ToolboxBitmap(GetType(Button))> Public Class myButton ' Implementation omitted End Class

Visual C#

[ToolboxBitmap(typeof(Button))] public class myButton : Button { // Implementation omitted }

Debugging Your Control

As with any development project, eventually you will have to debug your controls. Because controls are not stand-alone projects, they must be hosted within a Windows Forms project while debugging.

You must build your control before you can test it. Once the project containing your control has been built, you can place it on a form and debug as you would any other project.

If your control is part of an executable project, such as a Windows Forms project, you can add a new form to your project to host your control. If your control is part of a nonexecutable project, such as a class library or a Windows control library project, you must add an additional project to your solution to test your control.

To debug a control in a Windows Forms project

  1. From the Build menu, choose Build Solution to build your solution.

  2. If necessary, add a new form to your project. Set this form as the start-up form.

  3. Add your control to the form, either in code or at design time. Adding your control to the Toolbox in code facilitates adding it at design time.

  4. Press F5 to start your application. Your control is now available for debugging. You can set breakpoints in your control, step through code, and use the other debugging tools to find and correct bugs in your program.

  5. You must rebuild the control before any changes will be apparent in the form. You can rebuild your solution by choosing Rebuild Solution from the Build menu.

To debug a control in a class library or Windows control library project

  1. From the Build menu, choose Build Solution to build your solution.

  2. From the File menu, choose Add Project and then choose New Project to add an additional project to your solution. The Add New Project dialog box opens.

  3. In the Add New Project dialog box, choose Windows Application. Name the project, and click OK.

  4. In Solution Explorer, right-click the References node of the new project and choose Add Reference. The Add Reference dialog box opens.

  5. Choose the Projects tab. If the project containing your control is listed, select it and click OK. If it is not listed, click the Browse button and browse to the folder with the DLL file that contains your control. Select it, and click OK to continue.

  6. Add your control to the form in the new project, either in code or at design time. You might want to add your control to the Toolbox first to facilitate adding it at design time, as discussed earlier in this lesson.

  7. Press F5 to start your application. Your control is now available for debugging. You can set breakpoints in your control, step through code, and use the other debugging tools to find and correct bugs in your program.

Managing Control Licensing

Control licensing allows control developers to protect their intellectual property by ensuring that only authorized users can develop applications with their controls. The .NET Framework has a built-in control-licensing model. By default, it requires that a LicenseProviderAttribute be applied to any control that is to be licensed. This attribute specifies the LicenseProvider to use for validating the license. LicenseProvider is an abstract class that provides a standard interface for validation. In the constructor of the control, you call LicenseManager.Validate to return a reference to a valid license. The control will fail to load if it is not licensed.

The LicenseManager.Validate method validates the license by calling the GetLicense method of the indicated LicenseProvider, which retrieves the license and checks its validity by calling the IsKeyValid method. If the result is true, the license is granted. If the result is false, the control will not load. The particular validation scheme depends on the implementation of LicenseProvider. The .NET Framework includes an implementation of LicenseProvider named LicFileLicenseProvider. When this class is specified as the LicenseProvider, its implementation is used to determine if the license is valid. The GetLicense method of this provider examines the directory that contains the DLL file for this control for a text file named FullName.LIC, where FullName is the fully qualified name of the control. IsKeyValid then examines the first line of this text file for the line myClassName is a licensed component. where myClassName is the fully qualified name of the component. You can override this functionality to provide your own validation logic. You should implement Dispose for every licensed control and include a line in the Dispose method to dispose of the license.

The following code example shows a control named Widget that implements licensing with the LicFileLicenseProvider:

Visual Basic .NET

' This example assumes Imports System.ComponentModel ' The LicenseProvider attribute specifies the kind of ' LicenseProvider to use. <LicenseProvider(GetType(LicFileLicenseProvider))> Public Class _ Widget Inherits Control Private myLicense As License Public Sub New() ' Validates and retrieves a reference to the license myLicense = LicenseManager.Validate(GetType(Widget), Me) ' Additional constructor implementation omitted End Sub ' Implements Dispose to dispose the license Protected Overloads Overrides Sub _ Dispose(ByVal Disposing As Boolean) If Not (myLicense Is Nothing) then myLicense.Dispose() myLicense = Nothing End If End Sub End Class

Visual C#

// This example assumes using System.ComponentModel // The LicenseProvider attribute specifies the kind of // LicenseProvider to use. [LicenseProvider(typeof(LicFileLicenseProvider))] public class Widget : System.Windows.Forms.Control { private License myLicense; public Widget() { // Validates and retrieves a reference to the license myLicense = LicenseManager.Validate(typeof(Widget), this); // Additional constructor implementation omitted } // Implements Dispose to dispose the license protected override void Dispose(bool Disposing) { if (myLicense != null) { myLicense.Dispose(); myLicense = null; } } }

To implement licensing management for your control

  1. Specify the LicenseProvider to use by applying a LicenseProviderAttribute to your control class.

  2. Create the license file in accordance with the implementation of the LicenseProvider that you are using.

  3. In the constructor of your control, call LicenseManager.Validate to validate your license.

  4. Dispose of your license in the control s Dispose method.

Hosting Your Control in Internet Explorer

Every Windows Forms control can be hosted in Internet Explorer. You can take advantage of this feature to create HTML pages with rich Windows Forms control content. In this section, you will learn how to host your control in Internet Explorer.

For a control to be hosted in Internet Explorer, it must be installed to the Global Assembly Cache or it must reside in the same virtual directory as the HTML page in which it is declared.

NOTE
Installing classes to the Global Assembly Cache is discussed in Chapter 9.

You can add a Windows Forms control to an HTML page by using the <OBJECT> tag. The <OBJECT> tag specifies that a compiled code object is to be inserted into the page. The <OBJECT> tag examines its classid property to identify what type of object to load. Thus, you specify your Windows Forms control in the classid property.

The classid for a Windows Forms control consists of two parts. The first part is the path to the file that contains the control. The second part is the fully qualified name of the control. These are separated in the classid by the hash (#). The following code example demonstrates an <OBJECT> tag for a control in the ControlLibrary1.dll file (located in the same virtual directory as the HTML page) with the fully qualified name ControlLibrary1.myControl:

<OBJECT   classdd">

To host your control in Internet Explorer

  1. Save the DLL file that includes your control to the same virtual directory where the HTML page hosting it will reside, or install it to the Global Assembly Cache.

  2. Create an <OBJECT> tag in the HTML page with the appropriate classid property. The classid for a particular control consists of the path to the DLL file that contains the control, followed by the pound sign (#), followed by the control s fully qualified name.

Lesson Summary

  • You can add your controls to the Toolbox by right-clicking the Toolbox and choosing Customize Toolbox. Your controls will appear in the Toolbox with a default icon. If you want to specify a Toolbox bitmap for your control, use the ToolboxBitmapAttribute.

  • To debug controls, they first must be built and hosted in a form. If your control is in an executable project, you can add a form to the project to serve as a test form. If your control is in a nonexecutable project, you must add a test project to host the form. Once hosted, you can use the regular Visual Studio debugging tools to debug your control. You must rebuild the control before changes will be incorporated.

  • The .NET Framework provides a licensing model for licensing your controls. Specify a LicenseProvider by applying the LicFileLicenseProvider attribute to the control. Call LicenseManager.Validate to retrieve a reference to the license. When finished, dispose of the license.

  • You can host your control in Internet Explorer by creating an <OBJECT> tag. The <OBJECT> tag specifies which control to load in the classid property.



MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[.  .. ]0-316
MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[. .. ]0-316
ISBN: 735619263
EAN: N/A
Year: 2003
Pages: 110

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net