Replacing the OLE Container Control

Replacing the OLE Container Control

The OLE Container control was first introduced in Visual Basic 3 and is used to dynamically add objects to a form at run time. In Visual Basic 6, the OLE Container control is commonly used in three ways.

  • To create a control that contains an embedded object, such as a Microsoft Word document. When your application is compiled, its state (the contents of the object) is compiled into your program. If the user changes the object at run time, you can programmatically save the updated state to a file using the SaveToFile method and subsequently reload it using the ReadFromFile method.

  • To create a control that contains a linked object, such as a Word document. The difference between a linked object and an embedded object is that a linked object is linked to a file on disk instead of being compiled into the application. In the case of a linked document, any changes you make are immediately reflected in the document file, which can subsequently be loaded by Word.

  • To bind to an OLE object in a database typically to a picture. The OLE Container control can be data-bound to a field in a database using the DAO Data control or the RDO Remote Data control. The most common usage is to display a picture.

Visual Basic .NET does not support the OLE Container control. What happens to the control during an upgrade? Let s look at an example. Figure 15-1 shows a Visual Basic 6 form at design time, with an OLE Container control that has a link to a Word document stating, VB Rocks!!

Figure 15-1

OLE Container control with linked object in Visual Basic 6.

When we upgrade the project, the OLE control is replaced with a bright red label, indicating that the control could not be upgraded. Figure 15-2 shows the upgraded form. If you look at the upgrade report, you will find an entry saying that the control was not upgraded, and any code that manipulates the control will be marked with an upgrade warning similar to the following:

'UPGRADE_ISSUE: Ole method OLE1.CreateLink was not upgraded.

Figure 15-2

OLE Container control after upgrade.

Let s work on replacing the OLE control and getting our upgraded application to show the Word document as it did in Visual Basic 6. In many cases, we can use a WebBrowser ActiveX control to produce the same effect as a linked object within an OLE Container control. The WebBrowser resembles Microsoft Internet Explorer and can display documents, spreadsheets, and HTML pages using the Navigate method. It also allows you to edit these objects and save them to their original files. The WebBrowser is a lot like the linking part of OLE (which, you ll recall, is short for object linking and embedding ). Using the WebBrowser control, we can open the Word document and display it on the form, as shown in Figure 15-3.

Figure 15-3

Word document displayed using the WebBrowser control.

Here are the steps required to replace an OLE control with the WebBrowser control:

  1. Remove the red label from the form.

  2. Add a WebBrowser control to the form. Finding this control can be a little confusing. For starters, it is contained in the obscurely named Shdocvw.dll. In the Customize Toolbox component picker, you need to select Microsoft WebBrowser Control on the COM Components tab. In the Toolbox it is called an Explorer control, and when you add it to your form it is called a WebBrowser control.

  3. In Form_Load (or wherever you want to bind to the document), add the following code:

    Me.AxWebBrowser1.Navigate("C:\SampleCode\LinkedDoc.doc")

    You may want to substitute the filename in this example for the location of the object to which you are linking.

  4. Press F5, and that should do the trick the document should appear in the form. This method works for most objects that Internet Explorer can display, such as HTML files, documents, spreadsheets, GIFs, and JPEG files.

The other common use for the OLE Container control is to bind a control to an image in a database. In Visual Basic 6, this is done using the DAO and RDO Data controls. When such a project is upgraded to Visual Basic .NET, you ll see the red control mentioned previously, since neither the OLE Container control, the DAO Data control, nor the RDO Data control is supported in Visual Basic .NET.

Let s look at how you can achieve the same effect using ADO and a helper function in your Visual Basic .NET program. The following steps show how to bind a PictureBox to the Photo field of the Employees table in the Northwind database. It assumes that the NWind.mdb database is located in the root C:\ directory. If it is located somewhere else, you will need to change the file location in the following source. Here are the steps:

  1. Create a new Visual Basic .NET Windows application.

  2. Add a reference to ADODB.

  3. Add the adoHelper helper class to the application. For the source code of the helper class, see the Bind OLE Picture application on the companion CD.

  4. Add a PictureBox to the form.

  5. In the Form1_Load event, add this code:

    Dim cn As New ADODB.Connection() Dim rs As New ADODB.Recordset() Dim c As New adoHelper() cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _    "Data Source=C:\NWIND.MDB") rs.Open("Employees", cn) Me.PictureBox1.Image = c.rsFieldToBitmap(rs.Fields("photo"))

That s it! Press F5 and notice that the PictureBox retrieves the photo from the database, as shown in Figure 15-4.

Figure 15-4

Binding a PictureBox to an OLE image in a database.



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