Resolving Issues with Forms
Total Page:16
File Type:pdf, Size:1020Kb
C1261587x.fm Page 265 Thursday, November 15, 2001 3:51 PM Resolving Issues with Forms In 1988, Alan Cooper demonstrated a prototype called Ruby to Bill Gates. Ruby provided a form designer that allowed you to drag and drop controls, then known as gizmos, to quickly and easily create composite forms—such as dialog boxes, entry forms, and report forms. Microsoft took Cooper’s Ruby product and combined it with Basic to create Microsoft Visual Basic 1. Microsoft has since shipped a version of Ruby with every version of Visual Basic, versions 1 through 6. With every version, that is, until Visual Basic .NET. Visual Basic .NET provides a new forms package called Windows Forms. Although the Windows Forms package was designed using the same basic prin- ciple as Ruby—it is a form designer that allows you to drag and drop controls and set properties—it was never meant to be an extension of, nor to be com- patible with, Ruby. Therefore, there are fundamental differences between the two forms packages that affect the way you create Visual Basic applications. This chapter focuses on some of the fundamental differences between the Ruby and Windows Forms packages. Specifically, it discusses issues that the Upgrade Wizard does not handle for you. Before we get into the differences, however, let’s look at what Windows Forms and Ruby have in common. Similarities in Form Structure When you create a new project in Visual Basic .NET, you will find yourself at home in the environment. The way you create and design forms is the same in Visual Basic .NET as it is in Visual Basic 6. Although the names of some of the properties, methods, and events may have changed, you should be able to 265 C1261587x.fm Page 266 Thursday, November 15, 2001 3:51 PM 266 Part 3 Getting Your Project Working quickly find the one you need to use. In fact, you will find that you can create the exact same form in both Visual Basic 6 and Visual Basic .NET, using largely the same actions, the same controls, and the same property settings. The Upgrade Wizard can re-create your Visual Basic 6 forms in Visual Basic .NET. This is possible because the essential pieces of Visual Basic .NET are, for the most part, similar to those in Visual Basic 6: equivalent controls and equivalent properties, methods, and events. Figures 12-1 and 12-2 demonstrate a Visual Basic 6 form before and after being upgraded to Visual Basic .NET. F12km01 Figure 12-1 Visual Basic 6 form in design view. F12km02 Figure 12-2 Upgraded Visual Basic .NET form in design view. C1261587x.fm Page 267 Thursday, November 15, 2001 3:51 PM Chapter 12 Resolving Issues with Forms 267 As for the differences between forms in Visual Basic 6 and Visual Basic .NET, most of them are subtle, many related to the renaming or restructuring of components. It is these subtle differences, however, that can give you the most grief. We will spend the remainder of the chapter talking about them. General Issues Whether you are editing an upgraded application or writing a new Visual Basic .NET application from scratch, you will encounter certain general issues. These issues apply across forms, controls, and components. For example, you need to be aware of some basic differences between the Visual Basic 6 and Visual Basic .NET property, method, and event (PME) models. Understanding these funda- mental differences should give you a good base to start from when you are try- ing to understand issues you encounter involving a specific form or control. Differences in Properties, Methods, and Events The component models for Visual Basic 6 and Visual Basic .NET are similar in the sense that objects are composed of the same pieces: properties, methods, and events. You use properties, methods, and events in exactly the same way in Visual Basic .NET as you do in Visual Basic 6. The main difference is that the specific properties, methods, and events that make up any given object do not always match up. Let’s explore some of the general PME model differences. Some Properties, Methods, and Events Have Been Renamed One difference between the Visual Basic 6 and Visual Basic .NET PME models is that in some cases they use different names to refer to the same thing. For example, Visual Basic .NET does not contain a Caption property. Instead, every .NET control and component uses the Text property. In many other cases, prop- erties, methods, or events have been renamed. For example, SetFocus in Visual Basic 6 is Focus in Visual Basic .NET, and the Visual Basic 6 GotFocus and Lost- Focus events are known as Enter and Leave in Visual Basic .NET. If you are upgrading an application, you do not need to be too concerned about property naming differences. The Upgrade Wizard will automatically upgrade your code to use the new names. Your challenge will be to recognize and use the renamed properties when you start to modify your upgraded project or write new code. Appendix A contains a complete mapping of all Visual Basic 6 properties, events, and methods to their counterparts in Visual Basic 6. Multiple Properties Can Be Mapped into a Single Object Property In some cases, the Upgrade Wizard will map more than one property to con- structor parameters on a object. For example, an equivalent way of setting a form’s Left and Top properties is to use the Location property. To set the Location C1261587x.fm Page 268 Thursday, November 15, 2001 3:51 PM 268 Part 3 Getting Your Project Working property, you must first create an instance of the System.Drawing.Point object and initialize System.Drawing.Point with the left and top position of the com- ponent. For example, you can replace the following Visual Basic 6 code: Command1.Left = 10 Command1.Top = 20 with the following Visual Basic .NET code: Button1.Location = New System.Drawing.Point(10, 20) In a similar manner, the Visual Basic 6 Width and Height properties can be rep- resented by the Size property in Visual Basic .NET. Note The Left, Top, Width, and Height properties are available in Visual Basic .NET. We use Location and Size as an example because if you look in the code generated by the Windows Forms Designer in the “Windows Form Designer generated code” #Region section of the file, you will see that the designer uses the Location and Size proper- ties to set the position and size of controls on the form. The Upgrade Wizard will automatically map properties such as Left and Top to con- structor parameters on an object such as Point. Some Properties, Methods, and Events Are Not Supported In some cases you will find that there is no equivalent Visual Basic .NET coun- terpart for a particular property, method, or event in Visual Basic 6. This will be the case for one of two reasons: support is provided in a different manner, or the feature is considered out of date. Support Available in a Different Manner You won’t find the Visual Basic 6 prop- erties and methods related to drawing and graphics in Visual Basic .NET. At least, you won’t find them supported in the same way. These properties and methods, such as AutoRedraw, Line, and Circle, do have equivalents in Visual Basic .NET. The equivalent functions are provided in the .NET Framework Sys- tem.Drawing.Graphics class. In addition, global objects such as Clipboard and Printer do not have matching objects in Visual Basic .NET. However, you will find a Clipboard class and a PrintDocument class in the .NET Framework that you can use to accom- plish the same task. Table 12-1 provides a list of Visual Basic 6 statements and objects for which equivalent functionality can be found in the .NET Framework. C1261587x.fm Page 269 Thursday, November 15, 2001 3:51 PM Chapter 12 Resolving Issues with Forms 269 Table 12-1 User Interface Components and Their Visual Basic .NET Equivalents Visual Basic 6 Visual Basic .NET Graphics statements System.Drawing.Graphics class (such as Line and Circle) OLE drag and drop Windows Forms drag-and-drop properties, methods, and events to implement manual drag and drop Clipboard object System.Windows.Forms.Clipboard class Printer object System.Drawing.Printing.PrintDocument class The Visual Basic Upgrade Wizard does not upgrade objects, properties, methods, or events for which there is no equivalent in Visual Basic .NET. It therefore will not upgrade any properties, methods, or events related to the areas listed in Table 12-1. The wizard simply preserves your Visual Basic 6 code related to these areas as is. You need to upgrade your code manually to take advantage of the equivalent functionality provided by Visual Basic .NET or the .NET Framework. We’ll discuss graphics statements in more detail shortly. For information on how to add replacements for the Clipboard object, see Chapter 15. OLE drag and drop was discussed in Chapter 10. Out-of-Date Features Features such as Dynamic Data Exchange (DDE) and Visual Basic drag and drop, not to be confused with OLE drag and drop, are considered to be out of date. Neither Visual Basic .NET nor the .NET Frame- work supports these features. The Upgrade Wizard will preserve any Visual Basic 6 code you have written related to out-of-date objects, properties, meth- ods, or events as is. You need to either change your code to adopt a more up- to-date technology or implement support for the technology in .NET by calling the Windows API functions that relate to the technology.