Integrating IE in Your VFP Apps
Integrating IE in Your VFP Apps
Integrating IE in Your VFP Apps
Remi Caron
Okay, we all know that the browser is an integral part of the operating system. That much
has been made clear. What may not be clear to you is that you can incorporate the
browser in your own Visual FoxPro applications just as inextricably. Remi Caron explains
how.
In this article, I'll show you around Internet Explorer. Most of you probably use this very
cool control to surf the Internet. What you can do with it in your own application
development is the focus of this article. The samples provided in this article are written in
Visual FoxPro, but there's no reason at all to stop reading nowthe samples work for
anybody in any language. Look through the code, feel the power, and deploy it in your
own development.
IE as your VFP desktop
Let's start off with something lightputting up Internet Explorer as an active desktop in
your development IDE (see Figure 1). You could have your favorite newsgroup or Web
site in the background while you do your work. (Note: The links will work just like they
do in the browser.)
Figure 2. Select the Microsoft Web browser control in the FoxPro Options
dialog.
If this ActiveX control isn't installed on your system, you can download it free of
charge from www.microsoft.com/ie.
Now, create a form and place the controls on it as shown in Figure 3.
All this method does is navigate to the browser's internal page, set the form visibility to
true, and call the resize event to adjust itself to the size of the IDE frame. This way, the
browser fills the desktop.
* form: Resize Event
*------------------* Resize the browse each time the form is rezised.
WITH This.oBrowser
* Set the browser height keep address box visible
.Height = Thisform.Height - .Top
* Set the browser width
.Width = Thisform.Width
ENDWITH
* form: Navigate
*--------------* Call the navigate method in the ActiveX
Thisform.oBrowser.Navigate(ALLTRIM( ;
Thisform.txtAddress.value))
The navigate method will be invoked by the Go button on the screen as well.
* Go Button: Click event
*-------------------Thisform.Navigate()
Finally, there's one more "gotcha" you need to know to get this working properly.
There's one thing you must do to be able to run the form (thanks to Ken Levy for this tip).
In the Refresh() method of the browser control, include NODEFAULT. If you forget,
you'll get the error message shown in Figure 4 every time the control is refreshed.
After creating the form as specified, you can simply run it from the Command Window
with the following statement: DO FORM desktop. Done! That's all it takes to get this
working. The forms are in the Download file. (The Web browser control itself can't be
included.)
Type
Character
Character
Memo
Memo
Width
10
30
Decimals
0
0
The next thing we need is a FoxPro form with a reference to an instance of Internet
Explorer. The screen is shown in Figure 5. Since the screen is included with the Download
file, I won't go over all the code here, but I will address the important pieces of the code.
* Init
* create a reference to IE and navigate
* to your favorite website make IE visible
WITH Thisform
.AddProperty("oBrowser", CREATEOBJECT( ;
"internetexplorer.application"))
.oBrowser.Navigate("http://www.foxcentral.net")
.oBrowser.Visible = .T.
ENDWith
Now that we have a reference to IE, we can start looking for information we might
want to store in our own private knowledge base. I selected some text in IE and pressed
the New button of the knowledge base screen. The code behind the New button does
some checking and adds a record to the table with the selected text. The FoxPro code to
add a record and do the checking is left out of the text but can be found in the Download
file. Let's analyze the line of code that gets the selected text from IEthat's what matters
here:
* cmdNew: Click
lcArticle = ThisForm.oBrowser.Document. ;
selection.createrange.htmltext
* include the URL of the page
lcAddress = ThisForm.oBrowser.LocationUrl
The line of code that assigns a value to lcArticle is fairly long. Let me break it down:
Thisform.oBrowserGets into IE through the form.
Thisform.oBrowser.DocumentGets down to the document level.
Thisform.oBrowser.Document.SelectionGives us access to the selected text inside
the document.
Thisform.oBrowser.Document.Selection.CreateRangeCreates a range object of the
selection.
Thisform.oBrowser.Document.Selection.CreateRange.HTMLTextReturns the text of
the range object.
Now, about that line that assigns a value to lcAddress: To give my colleagues a chance
to read more than the selected text of the page, I also put in the table the URL of the page,
which they can visit by clicking on the Refresh button on the form.
This is all there is to building a very simple personal knowledge base system. This is a
simple example, of course, but take the idea behind it and utilize the power of IE in your
development efforts for either yourself or your customers.
A user-adjustable Help system
The first example showed how to get information out of a document that was loaded into
IE. But, if getting text out is possible, then putting text in should be possible too, right?
But for what purpose? Well... how about a user-extendible Help system? I started out with
a simple form as shown in Figure 6. As you can see, there are many "normal " FoxPro
controls on it, but the black spot is Microsoft's Internet Explorer as an ActiveX control.
As you can see in the HTML code, there are two IDs added: "HelpTopic" and
"Helptext." These IDs will be used from within our code later on to display the Help
content instead of the text that's displayed here right now. When you look at this code
rendered in a visual tool, you'll see something similar to Figure 7. You can use this page to
modify or extend its capabilities using FrontPage or another HTML editor.
The interaction between the data and the HTML form is as follows: I use the DOM to
get through the browser into the document. This is the code to make the HTML document
represent the information in the table for the selected topic. As I mentioned earlier, in this
method, I refer to the IDs that I placed on the tags in the HTML page.
* AfterRowColChange
WITH Thisform.oBrowser.Document.all
.Helptopic.Innertext = ALLTRIM(ttrade.topic)
.Helptext.Innertext = ALLTRIM(ttrade.details)
ENDWITH
It's a simple solution but flexible and extendible, and the source code is available in this
article's Download file, so you can play around with it and maybe embrace and extend it to
fit your own needs. If you want to deploy it for more than one customer or project, the
only thing you need to do is adjust the HTML file's look and feel, and you're done.
Next time, I'll show you how to use Internet Explorer as a content management tool
for data-driven Web content.
Download 04CARON.ZIP
Remi Caron is the CTO and co-founder of BizzView B.V. in The Netherlands. He's been programming in
FoxPro since Foxbase version 1.02. These days, his company uses all the Visual Studio tools available,
but FoxPro still has a special place in his developer's heart. In addition, he's president of the Microsoft
section within the Software Developers Group Netherlands. [email protected].