Common Tasks with controls

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. You can make your controls accessible and easy to work with by adding them to the Toolbox. This allows you to add them to your projects at design time.

You can add new controls to the Toolbox by using the Customize Toolbox option. You can access this option by right-clicking on the Toolbox and choosing Customize Toolbox, which launches the Customize Toolbox dialog box. From there, you can browse to the file that contains your control and add it.

To add your control to the Toolbox

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

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

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

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.

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 Toolbox 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 specify.

The ToolboxBitmapAttribute is attached to the class declaration of the control. In Microsoft Visual Basic .NET, it appears in angle brackets (<>) before the class declaration on the same line.

Debugging Your Control

As with any development project, you will eventually 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 Windows 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

From the Build menu, choose Build Solution to build your solution.
If necessary, add a new form to your project. Set this form as the startup form.
Add your control to the form, either in code or at design time.

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.

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
From the Build menu, choose Build Solution to build your solution.

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.

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

In Solution Explorer, right-click the References node of the new project and choose Add Reference.

The Add Reference dialog box opens.

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 containing the DLL file for the project that contains your control. Select it and click OK to continue.

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.

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

The .NET Framework provides built-in management for 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 default control licensing model requires that any control to be licensed have a LicenseProviderAttribute applied to it.

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. If the control is not licensed, it will fail to load.

The LicenseManager

Validate method validates the license by calling the GetLicense method of the indicated LicenseProvider, which retrieves the licenses, and then checks its validity by calling the IsKeyValid method of the LicenseProvider. If the result is true, the license is granted. If the result is false, the control will not be loaded. The particular validation scheme depends on the implementation of LicenseProvider. The .NET Framework includes an implementation of LicenseProvider called 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 again 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.

To implement licensing management for your control

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

file in accordance with the implementation of the LicenseProvider that you are using.
In the constructor of your control, call LicenseManager.Validate to validate your license.

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

Hosting Your Control in Internet Explorer

Every Windows Forms control can be hosted in Microsoft 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.

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

related post

DAY 11 OOPS INTRODUCTION

DAY 12 POLYMORPHISM

DAY 13 INHERITANCE AND POLYMORPHISM

DAY 14 EBUGGING TOOLS IN DOT NET

DAY 15 DEBUG AND TRACE IN CLASSES

DAY 16 UNIT TEST PLAN

DAY 17 EXCEPTIONS IN VISUAL STUDIO

DAY 19 ADO.NET INTRODUCTION

DAY 20 DATA ACCESSING IN DOT NET

DAY 21 DATA BASE OBJECTS


No comments:

Post a Comment