General Issues

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 fundamental differences should give you a good base to start from when you are trying 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, properties, 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 LostFocus 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 constructor 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 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 component. 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 represented 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 properties to set the position and size of controls on the form. The Upgrade Wizard will automatically map properties such as Left and Top to constructor 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 counterpart 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 properties 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 System.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 accomplish 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.

Table 12-1 User Interface Components and Their Visual Basic .NET Equivalents

Visual Basic 6

Visual Basic .NET

Graphics statements (such as Line and Circle)

System.Drawing.Graphics class

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 Framework supports these features. The Upgrade Wizard will preserve any Visual Basic 6 code you have written related to out-of-date objects, properties, methods, 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. For example, you can replace your DDE code with COM, or you can implement DDE support in your Visual Basic .NET application by declaring and calling the DDE-related API functions. The next section talks in more detail about ways to move code that uses out-of-date technologies to Visual Basic .NET.

Technology Differences

Since its inception in 1991, the focus of Visual Basic has been to enable Windows technologies for the developer. It allows you to quickly create Windows applications that use data, graphics, COM, and a host of other technologies. At the same time, Visual Basic has emphasized compatibility. This means that Visual Basic support language statements and object models for various technologies has accumulated over the years. Visual Basic 6 supports all technologies, regardless of their relevance in today s world. Objects and statements to support old technology coexist alongside the new: DDE with COM and the data objects DAO and RDO with ADO.

Visual Basic .NET, on the other hand, starts anew, wiping the technology slate clean. Visual Basic .NET and the .NET Framework provide first-class .NET support for the latest technologies, such as the .NET component model and ADO.NET. You can still use some of the existing technologies via COM interop, but you will not find .NET classes representing existing technologies such as ADO and DDE. Visual Basic .NET and the .NET Framework also offer renovated object models for technologies such as graphics and forms.

What does this all mean to you? It means that the Visual Basic code you write to take advantage of a particular technology is different from before. New .NET component models represent these technology areas. In many cases, you will find that you can accomplish the same tasks as before, but you will need to use different components, properties, methods, and events to do so. This section discusses changes to technology areas related to components and forms.

Graphics

Visual Basic 6 supports a number of graphics methods on the form, such as Line and Circle, and it supports PictureBox and Image controls that allow you to draw lines, circles, and text on a form or control. Visual Basic .NET does not support any methods on the form or PictureBox controls that let you draw. Instead you must respond to the Paint event, where you are passed a System.Drawing.Graphics object. You can use the System.Drawing.Graphics object and related objects to draw exactly as you have drawn before.

The Visual Basic .NET Upgrade Wizard does not automatically upgrade your code to respond to the Paint event because the model for drawing in Visual Basic 6 is dramatically different from that in Visual Basic .NET. Visual Basic 6 allows you to call a drawing method from any event or function in your code. Visual Basic .NET, on the other hand, requires any drawing code to be located in the Paint event or in subroutines called from the Paint event. It is therefore impossible for the wizard to pull calls to drawing methods, scattered about in various parts of your code, and arrange the code so that it will continue to work. Even if the wizard were able to pull off such a feat, you probably wouldn t recognize the resulting code. Therefore, the wizard leaves the calls to the Visual Basic 6 graphics methods in your upgraded Visual Basic .NET code as is. You need to manually port your graphics-related code to use the .NET equivalent graphics functions given in Table 12-2. For examples of manually upgrading Visual Basic 6 graphics statements such as Line and Circle to Visual Basic .NET, see Chapter 15.

Table 12-2 .NET Framework Equivalents for Visual Basic 6 Graphics Statements

Visual Basic 6

Visual Basic .NET

Circle

System.Drawing.Graphics.DrawEllipse method

Cls

System.Drawing.Graphics.Clear (color) method

CurrentX

X parameter to various graphics methods, such as System.Drawing.Graphics.DrawString

CurrentY

Y parameter to various graphics methods, such as System.Drawing.Graphics.DrawString

DrawMode

Attributes of System.Drawing.Pen passed to a Draw method on System.Drawing.Graphics

DrawStyle

Attributes of System.Drawing.Pen passed to a Draw method on System.Drawing.Graphics

DrawWidth

Width property of System.Drawing.Pen passed to a Draw method on System.Drawing.Graphics

FillColor

One of the colors contained in System.Drawing.Brushes passed to either System.Drawing.Graphics method Fill Ellipse or FillRectangle

FillStyle

HatchBrush class and HatchStyle enumeration in the System.Drawing.Drawing2D namespace

HDC

System.Drawing.Graphics.GetHdc method

Image

System.Drawing.Image class

Line

System.Drawing.Graphics.DrawLine method

PaintPicture

System.Drawing.Graphics.DrawImage method

Point

System.Drawing.Bitmap.GetPixel method

Print

System.Drawing.Graphics.DrawString method

PSet

System.Drawing.Bitmap.SetPixel method

TextHeight

System.Drawing.Graphics.MeasureString method

TextWidth

System.Drawing.Graphics.MeasureString method

Dynamic Data Exchange

Dynamic Data Exchange (DDE) was at one time one of the few ways you could pass data between applications without using a shared file. Visual Basic support for DDE dates back to Visual Basic 1, which included support for DDE on Windows 3. Visual Basic support for DDE, and the underlying Windows support for DDE, has remained largely unchanged since 1990. DDE, however, has since been replaced by COM.

COM allows you to share data and invoke methods between applications in a more efficient and scalable manner. Since most Windows applications that expose public data and methods do so via COM, DDE is generally not the primary way that you retrieve public data and invoke public methods on a Windows application. You use COM as a means of communicating with most Windows applications.

After upgrading your application to Visual Basic .NET, it is highly recommended that you replace all DDE communication with COM. For example, if you have code that calls LinkExecute to invoke a method on a DDE server, look at the documentation or type library for the server and see whether there is a COM equivalent for the same method. If there is, add a COM reference to the Windows application and write Visual Basic code to call the method.

Neither Visual Basic .NET nor the .NET Framework provides support for DDE. If you absolutely must communicate with another application using DDE, you will need to implement DDE in your Visual Basic .NET application in one of two ways: by creating and interoperating with an ActiveX EXE server built in Visual Basic 6 that manages the DDE conversation or by declaring and implementing the DDE-related Windows API functions.

If you want to reuse your Visual Basic 6 DDE code, you can create a Visual Basic 6 ActiveX EXE project and add a public class to exchange the DDE-related information you need with your Visual Basic .NET application. For example, if you want to perform a DDE LinkExecute operation in your Visual Basic .NET code, you create a DDE helper class written in Visual Basic 6 as follows:

  1. Create a Visual Basic 6 ActiveX EXE project.

  2. Add a public method to Class1 called LinkExecute that takes two parameters: ServerTopic and ExecCommand.

  3. Add a form called Form1 to the ActiveX EXE project.

  4. Add a TextBox called Text1 to the form.

  5. Add the following code to the LinkExecute method in Class1:

    Dim f As New Form1 f.Text1.LinkMode = 0 'None f.Text1.LinkTopic = ServerTopic f.Text1.LinkMode = 2 'Manual f.Text1.LinkExecute ExecCommand

  6. Build the ActiveX EXE server as Project1.dll.

To perform the LinkExecute operation in Visual Basic .NET, add a COM reference to Project1.dll to your Visual Basic .NET project References list and enter the following code:

 Dim DDEClientHelper As New Project1.Class1Class() DDEClientHelper.LinkExecute("MyDDEServer MyTopic", "MyCommand")

If you want to implement DDE in your Visual Basic .NET application by calling the DDE-related API functions, you will, at a minimum, need to declare and call DdeInitialize and DdeUninitialize. To call DdeInitialize,you need to implement DdeCallbackProc. However, this is just a start. You will also need to call a number of other DDE-related API functions to establish a connection and send data. To implement functionality to perform LinkExecute, for example, requires calling at least eight other DDE-related API functions. This task also requires a deep understanding of Windows messaging architecture and memory management, not to mention a knowledge of how to represent Windows types such as handles and structures in Visual Basic code. This undertaking is not for the faint of heart. To learn more about the DDE-related Windows API, search for Dynamic Data Exchange Management Functions in the Microsoft Developer Network (MSDN). For more information on issues related to using Declare statements in your code, refer to Chapter 11.

Visual Basic Drag and Drop

Visual Basic drag and drop has been available since Visual Basic 1. This feature allows you to drag and drop a control at run time onto a form or onto any other control on a form. The result of the drag-and-drop action depends on the code you write for the DragDrop event for a form or control. For example, you could write code for a CommandButton that would check to see whether the control being dropped was a TextBox and, if so, copy the TextBox text to the CommandButton caption. At run time, you drag and drop the TextBox onto the CommandButton, and the button caption changes to the TextBox text.

Although Visual Basic drag and drop is useful in creating a rich user interface, it has some serious limitations. Its functionality is limited to dragging and dropping forms within the same application; you cannot drag and drop between applications. In addition, if you set DragMode to automatic, there is no result for the drag-and-drop operation unless you write code to define the drop action. This means that you need to write code in the DragDrop event for the form and all controls that are to support drag and drop. Finally, when a control is dragged over a particular form or control, there is no concept of a no-drop icon. You need to write code in the DragOver event and set the DragIcon property to a no-drop icon for all controls that you do not want to allow to be dropped.

Visual Basic OLE drag and drop was introduced in Visual Basic 5. It provides several advantages over Visual Basic drag and drop. First, OLE drag and drop is a COM standard, meaning that you can drag and drop data between any applications that support OLE drag and drop and the data that you are dragging. In addition, OLE drag and drop is automatic. You can enable it by setting the OLEDragMode and OLEDropMode properties of the form and controls that you want to have support OLE drag and drop. You are not required to write any code to enable basic drag-and-drop operations. Finally, in OLE drag and drop, the concept of a no-drop icon is built in. This means that by default the form and controls do not advertise themselves as targets of a drag-and-drop operation. You set the OLEDropMode property or write code for the controls that you want to have support OLE drag and drop.

Because Visual Basic drag and drop is considered an outdated feature, the Upgrade Wizard does not upgrade your DragDrop-related code. Instead, it preserves the Visual Basic 6 drag-and-drop code as is in the upgraded Visual Basic .NET project. The code will not compile, so you need to either delete it and remove the drag-and-drop functionality or change the code to use OLE drag and drop. For information on how to implement drag and drop in Windows Forms, see Chapter 10.



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

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