Windows Control Basics

function OpenWin(url, w, h) { if(!w) w = 400; if(!h) h = 300; window.open(url, "_new", "width=" + w + ",height=" + h + ",menubar=no,toobar=no,scrollbars=yes", true); } function Print() { window.focus(); if(window.print) { window.print(); window.setTimeout('window.close();',5000); } }
Team-Fly    

Special Edition Using Microsoft® Visual Basic® .NET
By Brian Siler, Jeff Spotts
Table of Contents
Chapter 16.  Creating Your Own Windows Controls


You are already familiar with controls such as the TextBox and Label controls. To use these controls, you draw them on a form and direct their behavior through properties, methods, and events. When you create your own Windows control you are creating a similar object, except you are determining the properties, methods, and events. After you create your own control, you and others can use it in other Visual Basic projects, just like a TextBox control. You can use your controls in any application or development tool that can use Windows controls, including other .NET projects or Microsoft Internet Explorer. In fact, using custom controls is an ideal way to create a reusable component that can be used both inside a traditional client/server program and on the Internet.

Note

In Visual Basic .NET, custom controls are created in class modules. For simplicity's sake, the sample that you will create in this chapter will contain a single control; however, you can create as many controls as you like within a project by adding multiple custom control class modules.


Steps Involved in Building Custom Windows Controls

Creating a customWindows control in Visual Basic is different from creating a standard Windows Application. Therefore, a brief overview of the steps involved is useful:

  1. Create a high-level design to determine what you want your Windows control to do. A Windows control is like a standalone object, so you need to ask yourself: What purpose does the object serve? What appearance do you want it have onscreen? What properties, methods, and events need to be made available to the program using the control?

  2. Determine whether you will be using other controls as building blocks for your control. For example, you might want to create a Windows control that includes a third-party grid. When you are using other controls within your control, consider licensing and distribution issues.

  3. Start a new Windows Control Library project, and draw the interface for your control.

  4. Add code to enable all the properties, methods, and events you want your control to have.

  5. A custom Windows control cannot run by itself. Its "runtime" is design time of the host application in which your control is used. You will need to add your custom control to a test project and test it from within Visual Basic. Make sure to use all the properties, methods, and events that you give your control.

  6. Compile your control into a DLL file, and perform testing on the compiled version of your control.

  7. Deploy your custom control appropriately.

Development Strategies

Building a User control in Visual Basic can be as easy or difficult as you choose. The level of difficulty depends on whether you use existing controls in your design, how sophisticated the interface will be, and (of course) how much code you have to write to make it work. In any case, there are at least three general reasons why you may want to build a custom control:

  • Combine the functionality of existing controls. This approach is also known as building a control from constituent controls. It is the easiest way to build a Windows control, because you are just bringing existing controls together. For example, you can package a combo box and a text box together and write minimal code that causes them to interact in the desired manner.

  • Add new functionality to an existing control. You can use an existing control as a starting point for your own creative efforts by modifying the control properties. For example, you can take a text box and add a custom property called TextCaps that causes the text to always be displayed in capital letters. This approach is a good way to get the exact functionality that you need in a control.

  • Create an entirely new control. You can draw the interface yourself with graphics methods, creating a totally original control that does not include any existing controls. Drawing your own control requires a good bit more work than the other two strategies but can be done to provide an original or different user interface. Because of the complexity of this approach, it will not be demonstrated in this chapter. However, you can get an introduction to the types of graphics techniques required to create a control in this manner in Chapter 14, "Designing an Effective User Interface."


    Team-Fly    
    Top
     



    Special Edition Using Visual Basic. NET
    Special Edition Using Visual Basic.NET
    ISBN: 078972572X
    EAN: 2147483647
    Year: 2001
    Pages: 198

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