Process of Replacing Controls

Process of Replacing Controls

Because the Visual Basic .NET Upgrade Wizard does not automatically replace your ActiveX controls with Windows Forms equivalents, the process of replacing the ActiveX controls in your upgraded project with equivalent controls provided in the Windows Forms package is a manual one. This process involves the following general steps:

  1. Copy the design-time property settings for the ActiveX control.

  2. Delete the ActiveX control from the form.

  3. Place the equivalent Windows Forms control on the form.

  4. Rename the Windows Forms control to match the name of the ActiveX control.

  5. Paste the design-time settings for the control.

  6. Fix up the design-time settings.

  7. Resolve errors in the code.

  8. Remove the ActiveX control library reference.

The example in this section demonstrates each of these steps in turn.

Manually Upgrading a Control

Let s take a look at a simple example of replacing the ActiveX ProgressBar control with the Windows Forms ProgressBar control. First you need to start with a Visual Basic 6 application that uses the ProgressBar control.

The companion CD includes a sample Visual Basic 6 application called PBar that serves as the basis for demonstrating how to manually upgrade an ActiveX control to its Windows Form equivalent control. Specifically, it demonstrates how to manually upgrade the ActiveX ProgressBar control to the Windows Forms ProgressBar control. The application consists of a form containing a ProgressBar ActiveX control and a command button. The command button click event (Command1_Click) contains the following code, which we ll use to demonstrate how to upgrade code associated with an ActiveX control:

   Dim i As Integer    Dim StartTime As Single    For i = ProgressBar1.Min To ProgressBar1.Max       StartTime = Timer       Do While Timer - StartTime < 0.01       Loop       ProgressBar1.Value = i       ProgressBar1.Refresh    Next

The sample application also includes the following ProgressBar Click event for the purpose of demonstrating how to upgrade an event associated with an ActiveX control:

   Private Sub ProgressBar1_Click()       MsgBox "You clicked my progress bar!"    End Sub

First load the Visual Basic 6 sample PBar application and try it out. Then choose Start from the Run menu, and click the Show Progress button. You should see the progress automatically increment from minimum to maximum. Figure 19-1 shows an example of the application running.

Figure 19-1

Visual Basic 6 ProgressBar application.

Now you ll upgrade the application to Visual Basic .NET. To do this, copy the PBar project from the companion CD to your hard drive, run Visual Basic .NET and open the project file for the Visual Basic 6 application PBar.vbp. The Upgrade Wizard appears. Step through each of the wizard s pages, selecting the default options. The upgraded application will be loaded in Visual Basic .NET. Test it by choosing Start from the Debug menu and clicking the Show Progress button. The application should run as it did in Visual Basic 6.

In the next section, we will replace the ProgressBar ActiveX control, following the steps outlined earlier.

Copy the Design-Time Property Settings for the ActiveX Control

Design-time settings are stored in two places: in the upgraded form file and in the original Visual Basic 6 form file. The upgraded form file contains the extended design-time settings for the control. These are the settings common to all controls on the form that define attributes such as the control s name, its size and position on the form, and its tab order. The Visual Basic 6 form file contains custom property settings for the form. In the case of the ProgressBar, these settings include the Max and Min property settings.

Copy the extended property settings

Let s start by copying the upgraded extended property settings found in the upgraded form file. To see these property settings, view the code for the Visual Basic .NET form file and expand the Windows Form Designer generated code region. The property settings for the ProgessBar control are located in the InitializeComponent subroutine as follows:

Me.ProgressBar1.Location = New System.Drawing.Point(8, 64) Me.ProgressBar1.Name = "ProgressBar1" Me.ProgressBar1.OcxState = _    CType(resources.GetObject("ProgressBar1.OcxState"), _    System.Windows.Forms.AxHost.State) Me.ProgressBar1.TabIndex = 0 Me.ProgressBar1.Size = New System.Drawing.Size(297, 57)

Select all property settings and copy the code to the Clipboard. Run Notepad and paste the code into it. Notepad will serve as a temporary holding place for this code. Delete the line containing the OcxState setting. OcxState represents the internal, saved state of the ActiveX control and cannot be applied to the ProgressBar Windows Forms control. It is easier to get the OcxState information from the original Visual Basic 6 form file, as we will demonstrate in the next section.

Copy the control-specific property settings

Now let s copy the property settings found in the original Visual Basic 6 form file. Run another instance of Notepad, and open PBar.frm (copied previously from the companion CD). Look for the following section in the FRM file:

   Begin MSComctlLib.ProgressBar ProgressBar1        Height          =   855       Left            =   120       TabIndex        =   0       Top             =   960       Width           =   4455       _ExtentX        =   7858       _ExtentY        =   1508       _Version        =   393216       Appearance      =   1       Min             =   1       Max             =   200    End 

Let s copy the ActiveX control-specific property settings. In this case, the control-specific property settings are Appearance, Min, and Max. Custom property settings appear in the FRM file s property settings block for a control, after the extended property settings. Extended property settings relate to properties that Visual Basic 6 maintains on behalf of the ActiveX control. The following list contains the extended properties supported by Visual Basic 6. You do not need to copy these settings when replacing the ActiveX control with a Windows Forms control. The reason is that the equivalent Visual Basic .NET property settings can be found in the InitializeComponent subroutine of the upgraded form file. It is easier to copy the upgraded extended property settings found in the Visual Basic .NET form file than it is to copy the ones found in the original Visual Basic 6 FRM file, as we demonstrated in the previous section.

_ExtentY

DataFormat

Left

_ExtentX

DataMember

Name

_Version

DataSource

TabIndex

Extender properties

Default

TabStop

Align

DragIcon

Tag

Cancel

DragMode

TooltipText

CausesValidation

Enabled

Top

Container

Height

Visible

DataBindings

HelpContextID

WhatsThisHelpID

DataChanged

Index

Width

DataField

Copy the following settings to the original instance of Notepad containing the extended property settings. Paste them after the extended property settings.

   Appearance      =   1    Min             =   1    Max             =   200

After you ve copied the settings, Notepad should contain the following:

   Me.ProgressBar1.Location = New System.Drawing.Point(8, 64)    Me.ProgressBar1.Name = "ProgressBar1"    Me.ProgressBar1.TabIndex = 0    Me.ProgressBar1.Size = New System.Drawing.Size(297, 57)    Appearance      =   1    Min             =   1    Max             =   200

Modify the settings in Notepad by converting each of the settings you ve just copied to Visual Basic code in the form Me.<controlname>.<property name> = <value>. Your code should appear as follows after modification:

   Me.ProgressBar1.Location = New System.Drawing.Point(8, 64)    Me.ProgressBar1.Name = "ProgressBar1"    Me.ProgressBar1.TabIndex = 0    Me.ProgressBar1.Size = New System.Drawing.Size(297, 57)    Me.ProgressBar1.Appearance = 1    Me.ProgressBar1.Min = 1    Me.ProgressBar1.Max = 200

Delete the ActiveX Control from the Form

The next step is an easy one. Switch to Design view for the form, and delete the ProgressBar ActiveX control from the upgraded Visual Basic .NET form, Form1.

Place the Equivalent Windows Forms Control on the Form

Select the Windows Forms ProgressBar control on the Windows Forms tab of the Toolbox. Place the control on Form1. You don t need to worry about placing the control in exactly the same position as the previous control. The position and size will be restored later when you copy the code from Notepad.

Rename the Windows Forms Control to Match the Name of the ActiveX Control

Change the Name property of the control to ProgressBar1 to match the name of the original ActiveX control.

Paste the Design-Time Settings for the Control

All that monkeying around you did in Notepad will now pay off. Copy the property settings you created in Notepad to the InitializeComponent subroutine in Form1, and replace the existing property settings for ProgressBar that you find. The InitializeComponent subroutine is located within the hidden block labeled Windows Form Designer generated code.

Fix Up the Design-Time Settings

Compiler errors will display for any property settings that the compiler doesn t recognize. For example, a compiler error will occur on the following three lines of code. It occurs because the property cannot be found in the Windows Forms ProgressBar control. You will need to either eliminate property settings for properties that are no longer supported or change the property name to the equivalent property found in the Windows Forms control.

   Me.ProgressBar1.Appearance = 1    Me.ProgressBar1.Min = 1    Me.ProgressBar1.Max = 200

Using IntelliSense, you can quickly get a feel for what properties the Windows Forms ProgressBar control does and doesn t support. To see a list of all properties associated with the ProgressBar control, type the name of the control in the code window, followed by a period. A drop-down list of the available properties and methods displays. You will find that the Appearance property is no longer supported, Min maps to Minimize, and Max maps to Maximize. Making the appropriate changes causes the compiler errors related to design-time settings to disappear. The resulting InitializeComponent code is as follows:

   Me.ProgressBar1.Minimize = 1    Me.ProgressBar1.Maximize = 200

Resolve Errors in the Code

Compiler errors are displayed for any code in which a property cannot be resolved. In the Command1_Click event, you will see three compiler errors related to the use of the properties and methods Min, Max, and CtlRefresh. In line with the changes you made to the design-time properties, change Min to Minimum and Max to Maximum. The CtlRefresh method for the ActiveX ProgressBar control corresponds to the Refresh method for the Windows Forms ProgressBar control. The ActiveX control property is renamed CtlRefresh in Windows Forms to avoid conflicts with the extended Refresh property applied to all controls. The Windows Forms ProgressBar control contains a single method called Refresh. Rename CtlRefresh to Refresh.

In addition to fixing up properties and methods, you need to update your event code. Specifically, when you delete the ActiveX control, the events for the control become disassociated and are converted to ordinary subroutines. You need to reassociate the events with the Windows Forms control. In our example, code is written in response to the ProgressBar Click event. When the ProgressBar ActiveX control is deleted, the following subroutine exists but is not associated with the Windows Forms ProgressBar control:

Private Sub ProgressBar1_ClickEvent( _    ByVal eventSender As System.Object, _    ByVal eventArgs As System.EventArgs)    MsgBox("You clicked my progress bar!") End Sub

The easiest way to reassociate the Click event with the ProgressBar control is to act as though you were writing new code for the Click event. Select the Click event for the ProgressBar from the Event drop-down list to create the ProgressBar1_Click event handler subroutine, as follows:

Private Sub ProgressBar1_Click(ByVal sender As Object, _    ByVal e As System.EventArgs) Handles ProgressBar1.Click End Sub

Cut and paste the body of the ProgressBar1_ClickEvent subroutine to the ProgressBar1_Click event subroutine, and then delete the original ProgressBar1_Click event subroutine, leaving the following code:

Private Sub ProgressBar1_Click(ByVal sender As Object, _    ByVal e As System.EventArgs) Handles ProgressBar1.Click    MsgBox("You clicked my progress bar!") End Sub

Re-creating the event subroutines in this manner causes the correct event parameters to be created in turn. You then need to resolve any compiler errors that result. For example, you may have code that references a method of an ActiveX control s event argument object, but the method may not exist or may have a different name in the event argument object of the corresponding Windows Forms control.

Remove the ActiveX Control Library Reference

The final step is optional and should be performed only if you have replaced all ActiveX controls for a given library with Windows Forms equivalents. In the case of the example demonstrated here, all controls contained in the Windows common controls library the ProgressBar control have been replaced with Windows Forms equivalents. Therefore, you can safely remove the reference to the ActiveX control library. To remove the reference, expand the References list for the project in the Solution Explorer. You will need to remove two references for each ActiveX control. Right-click AxMSComctlLib, and choose Remove from the shortcut menu. Then right-click MSComctlLib and choose Remove. Removing an ActiveX control library reference in this manner means that fewer files need to be deployed with the application.

With these changes in place, all compiler errors should be resolved. Run the application and test it out. It should behave exactly like the original Visual Basic 6 application.



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