PDA

View Full Version : [VB6] WebBrowser Control Tutorial



Shane-
6th October 2006, 12:39 AM
For this we'l be using the WebBrowser Control (shdocvw.dll)

Open VB
Double click "Standard EXE"

That sould load your form (Form1)

In the bottom right hand corner, you sould have a box called "Properties"
At the top of that box, there is " (Name) ", Type in there: frmMain

frm = Its a form we'r naming
Main = We give it the name of Main as it will be our main form.

Now we've renamed the form, Take a look at your form, Bottom right hand corner there sould be a small blue box, Click and hold your mouse over that, And drag it (That will resize your form), Resize it to a size you feel suitable.

Now to add a WebBrowser.
Click " Project " then "Components"
http://img227.imageshack.us/img227/6307/vbtk9.png

Scroll down the list untill you find: Microsoft Internet Control.
Tick that and click "ok"

On the left hand side, You now sould have a "World" logo, That is your WebBrowser Control.

Select it, And drag/drop it onto your form, Use the same meathod i told you to about resizing the form, To resize the browser

(Leave about 10-20% of top of the form showing)

Now, You rember how we renamed the form to " frmMain "... Do that with the WebBrowser, And call it " IE "

Congraz, You have a large percent of the form done.

Double click the form (Gray part) and you sould acces the "Code Area", All you sould be able to see is:


Private Sub Form_Load()

End Sub

In the middle of those 2 lines, Add:
Me.Caption = "Gua Browser" 'Renames the titelbar
IE.Navigate "www.google.com" 'Navigates your browser
IE.Width = Me.ScaleWidth 'Changes width of your browser
IE.Left = 0 'Alines your browser
IE.Top = 800 'Gives up anought room at top of form for buttons
IE.Height = Me.ScaleHeight 'Changes the height of your browser
The green comments example what each line does.

Now to test your browser,
http://img141.imageshack.us/img141/5212/vbdw7.png <-- Shows

Now to give it a little life.

Double click "frmMain" in your Project browser (Top right corner) so your able to see your form again

On the left hand side, You sould see your "controls" hover over each of those to see what there are, When you find 'CommandButton', Drag and drop that into your form like you did with the Browser control

Now to the same with 'Textbox'

Place the TextBox and Button like:
http://img182.imageshack.us/img182/3940/vbst4.png

This will be your address bar, and your " Go " button

You rember how to give things names?... I hope so because im not telling you again

Name the Button: cmdGo
Name the Textbox: txtAddress

In the Properties menu, theres other options (To give captions to controls)
For the button, Find "caption" and type in ' Go! ", And for the Textbox, type in ' Google.com '

Double click your 'Go' button, and you'l see


Private Sub cmdGo_Click()

End Sub
In the middle of there, Enter:

On Error Resume Next 'If there is any errors, The project wont crash
IE.Navigate txtAddress.Text 'Navigates your browser to the giving site
Now double click the textbox (txtAddress)
You have:

Private Sub txtAddress_Change()

End Sub
Take a look at: http://img91.imageshack.us/img91/9031/vbfh3.png

First, We'r going to do the red one, Click " Click ", That will give you


Private Sub txtAddress_Click()

End Sub
Enter in there:
txtAddress.Text = "" 'This is to clear the textbox so we can enter a new URL

Now the blue one (KeyDown), That gives:

Private Sub txtAddress_KeyDown(KeyCode As Integer, Shift As Integer)

End Sub In the middle, Add
If KeyCode = vbKeyReturn Then IE.Navigate txtAddress.Text 'This is so when we hit enter on the keyboard, It takes us to the site, Aka saves pressing "go"


Now we have a basic brower set up... Now we need a " Back ", " Forward ", "Home" button... You know how to make those :-)

Put them under the address bar (And make it look nice), Rember to give them the right names...
Back = cmdBack
Forward = cmdForward
Home = cmdHome

Double click Back, and enter the code:
On Error Resume Next 'Provents any error
IE.GoBack

And double click Forward and enter:
On Error Resume Next 'Provents any error
IE.GoForward

And double click Home and enter:
On Error Resume Next 'Provents any error
IE.GoHome 'Goes to your IE home page

Okie, Now we have done:
Navigate
Home
Back
Forward

We need 2 more buttons, cmdRefresh and cmdStop
cmdRefresh
On Error Resume Next
IE.Refresh

cmdStop
On Error Resume Next
IE.Stop

Ok, Basic Front Done

Double click your browser, You'l get

Private Sub IE_StatusTextChange(ByVal Text As String)

End Sub You rember how we changed the textbox "Change" to "Click"... We'r doing that again, Change StatusTextChange to DocumentComplete, which will give you:


Private Sub IE_DocumentComplete(ByVal pDisp As Object, URL As Variant)

End Sub
This is the Document Complete" function, when the browser has finished loading a page, This function is called.

Add: Me.Caption = "Gua Browser - " & IE.LocationName

That will change the title bar off the program to " Gua Browser - " then the name of the page your on, Like Firefox and IE do

Well... I think that covers the basics off making a small web browser

Codes at: http://www.shane.webwise-uk.com/projects/GuaBrowser.rar


Shane

El Vista
4th January 2007, 02:49 AM
How do you read the source of the opened HTML file using the shdocvw.dll?

i mean with the GuaBrowser's code above, I want a Show Source window, or something along those lines.

Any ideas?

Shane-
5th January 2007, 08:05 AM
How do you read the source of the opened HTML file using the shdocvw.dll?

i mean with the GuaBrowser's code above, I want a Show Source window, or something along those lines.

Any ideas?


Dim html as string
html = ie.Document.body.innerhtml
I asume your asking for that tool of yours, so i guess you'd also need a few html HTML tips


KoC.Document.All("usrname").Value = Text3.Text
KoC.Document.All("uemail").Value = Text4.Text
KoC.Document.All("peeword").Value = Text5.Text
KoC.Document.Forms(0).submit To enter values into submit boxes, and submit the form

We have to 'Submit' the form as the button dosnt have a html name, If it did, You'd use:
Koc.Document.body.all("Html value of the button").Click

Go grab info from the page use:

Private Sub Gold(HTML As String)
ix = 1
ix = InStr(1, HTML, "Gold:") + 60
GoldLine = Mid$(HTML, ix - 1, InStr(ix, HTML, "Turn:") - (ix + 77))
GoldLine = Replace(GoldLine, ",", "")
End Sub


Personally i think the best place to grab the html is in the IE_DocumentComplete event as you know the browser is fully "loaded",

End Sub

If you encounter any other questions, feel free to ask

Shane

El Vista
5th January 2007, 11:11 AM
Thanx a lot man! I owe you one! I worked with parsing and generic data manupulation... I made some DBMSs and stuff like that, but this is the first time I am working to make an app with some sort of connection to the net.

I'll let you know how it comes out...

I gave you some rep, that's the least I could do :D

Xull
19th January 2007, 02:45 PM
Just a few comments:
the webbrowser control uses the same engine as IE does.

Also I'm curious as to why vb6 as opposed to vb2005? I suppose the runtime for .net is larger than that of vb6, but most people would have it anyway and you can download vb2005 (express) off of microsoft's site for free. Is vb6 even sold anymore? I thought I read somewhere that it wasn't...
Well, its similar, so I guess it doesn't matter too much.

Still, vb6 brings back some memories :D

Shane-
19th January 2007, 10:50 PM
Just a few comments:
the webbrowser control uses the same engine as IE does.

Also I'm curious as to why vb6 as opposed to vb2005? I suppose the runtime for .net is larger than that of vb6, but most people would have it anyway and you can download vb2005 (express) off of microsoft's site for free. Is vb6 even sold anymore? I thought I read somewhere that it wasn't...
Well, its similar, so I guess it doesn't matter too much.

Still, vb6 brings back some memories :D

"Back in my day there wasnt VB2005" :p

I play with VB6, and Vb2005, but personally i think VB6 is alot easyer to learn, But id give an bios opion on that as it was my first langages, But once you learned vb, you have a basic understanding of programming that helps you in other lanuages, I learned php rather fast as i had a decent understandance of codeing via vb6 ^^

Plus, The runtime thing, It sucks asking people to download the 24mb pacage, But i guess you still get that in vb6, Including ocx/dll's in the zip, All in all, Vb6 still a decent lanuage to use as a first :p

Shane