Go Back   GamerzPlanet - For All Your Online Gaming Needs!! > Programming/Web Design > Visual Basic

Visual Basic VB Discussion. (Including VB.Net)


VB.Net Web Browser Tutorial

Visual Basic


Reply
 
Thread Tools Display Modes
Old 07-04-2009, 10:23 AM   #1
amonkeyz
[Crossfire Master]
 
Last Online: 08-23-2009 01:36 AM
Join Date: May 2009
Location: www.ruttconsulting.com
Posts: 352
Rep Power: 1
Rep Points: 22
amonkeyz is on a distinguished road
Feedback: (0)
Points: 846.98
Bank: 0.00
Total Points: 846.98
Post VB.Net Web Browser Tutorial

The following was not made by me. The following was made by zeidhaddadin from vbforums all credit to him. I take no credit in this.
----------------------------
In this thread I'm going to talk about all the things that you might need in building an internet web browser using visual basic component.

1- Adding components:

Go to "Project" on the top menu, and choose "Components" or click CTRL-T, then check "Microsoft HTML object library" and "Microsoft internet controls" and "Microsoft Windows common Controls 5.0".

You will see that new objects are appearing in the left box of your menu, now choose the web-browser icon which looks like an earth and add it to your form, and make its size as you want, (its name is WebBrowser1 in this tutorial)

Also click on the "Progress bar" component and add it to your form.

2- Adding buttons and objects:

Now add the "6" essential command buttons to your form, and name them as the following: "Back","Forward","Stop","Refresh","Home" and "GO", Now add 1 combo box which the user will enter the web address on it.

3- Coding your application:

First click on the "GO" button and write this code inside it:
VB Code:
WebBrowser1.Navigate Combo1

Now We will code the other buttons as the following:

Back button:

VB Code:
Code:
On Error Resume Next
WebBrowser1.GoBack
Forward button: VB Code:
Code:
On Error Resume Next
WebBrowser1.GoForward
Stop button: VB Code:
Code:
On Error Resume Next
WebBrowser1.Stop
Refresh button: VB Code:
Code:
WebBrowser1.Refresh
Home button: VB Code:
Code:
WebBrowser1.GoHome
NOW YOU CAN TRY USING YOUR NEW WEB BROWSER.

4- Advanced codes:

You can add the following codes to your application to make it work better, Just place these codes anywhere in your coding window.

* Add a progress bar

VB Code:
Code:
'This to make the progress bar work and to show a status message, and an image.

      Private Sub WebBrowser1_ProgressChange(ByVal Progress As Long, ByVal ProgressMax As Long)

      On Error Resume Next

          If Progress = -1 Then ProgressBar1.Value = 100 'the name of the progress bar is "ProgressBar1".

              Label1.Caption = "Done"

              ProgressBar1.Visible = False 'This makes the progress bar disappear after the page is loaded.

              Image1.Visible = True

          If Progress > 0 And ProgressMax > 0 Then

              ProgressBar1.Visible = True

              Image1.Visible = False

              ProgressBar1.Value = Progress * 100 / ProgressMax

              Label1.Caption = Int(Progress * 100 / ProgressMax) & "%"

          End If

          Exit Sub

      End Sub
But here you will need to add an label which is called "Label1" and also an small image such as a smile or earth or anything you want and the name is "image1"

* Open in new window

VB Code:
Code:
      'This to open a new window with our browser.

      Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)

      Dim frm As Form1

      Set frm = New Form1

      Set ppDisp = frm.WebBrowser1.Object

      frm.Show

      End Sub
This to open the new window with your browser.


* History and current visited site.

VB Code:
Code:
'This keeps the visited sites history and also changes the title of the browser as the page title.

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

          On Error Resume Next

          Dim i As Integer

          Dim bFound As Boolean

          Me.Caption = WebBrowser1.LocationName

          For i = 0 To Combo1.ListCount - 1

              If Combo1.List(i) = WebBrowser1.LocationURL Then

                  bFound = True

                  Exit For

              End If

          Next i

          mbDontNavigateNow = True

          If bFound Then

              Combo1.RemoveItem i

          End If

          Combo1.AddItem WebBrowser1.LocationURL, 0

          Combo1.ListIndex = 0

          mbDontNavigateNow = False

      End Sub
5- More coding:

You can add more buttons to your browser as those:

* Find if a word is in the page.

VB Code:
Code:
'This to tell you if a word is in the page, Here we call the WebPageContains function.

      Private Sub Command7_Click()

          Dim strfindword As String

              strfindword = InputBox("What are you looking for?", "Find", "") ' what word to find?

                  If WebPageContains(strfindword) = True Then 'check if the word is in page

                      MsgBox "The webpage contains the text" 'string is in page

                  Else

                      MsgBox "The webpage doesn't contains the text" 'string is not in page

                  End If

      End Sub

       

      'This is the finding function.

      Private Function WebPageContains(ByVal s As String) As Boolean

          Dim i As Long, EHTML

          For i = 1 To WebBrowser1.Document.All.length

              Set EHTML = _

              WebBrowser1.Document.All.Item(i)

       

       

              If Not (EHTML Is Nothing) Then

                  If InStr(1, EHTML.innerHTML, _

                  s, vbTextCompare) > 0 Then

                  WebPageContains = True

                  Exit Function

              End If

          End If

      Next i

      End Function
* Page properties
VB Code:
Code:
WebBrowser1.ExecWB OLECMDID_PROPERTIES, OLECMDEXECOPT_DODEFAULT
This will run the page properties.



* Print a page
VB Code:
Code:
WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DODEFAULT
* Save a page
VB Code:
Code:
WebBrowser1.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT
* Delete cookies from your computer

VB Code:
Code:
'This code is used to empty the cookies from the user's computer / We call function from here.

      Private Declare Function GetUserName _

      Lib "advapi32.dll" Alias "GetUserNameW" ( _

      ByVal lpBuffer As Long, _

      ByRef nSize As Long) As Long

       

      Private Declare Function SHGetSpecialFolderPath _

      Lib "shell32.dll" Alias "SHGetSpecialFolderPathA" ( _

      ByVal hwnd As Long, _

      ByVal pszPath As String, _

      ByVal csidl As Long, _

      ByVal fCreate As Long) As Long

       

      Private Const CSIDL_COOKIES As Long = &H21

       

      'This calls the function that deletes the cookies.

      Public Sub Command1_Click()

      Dim sPath As String

      sPath = Space(260)

      Call SHGetSpecialFolderPath(0, sPath, CSIDL_COOKIES, False)

      sPath = Left$(sPath, InStr(sPath, vbNullChar) - 1) & "\*.txt*"

      On Error Resume Next

      Kill sPath

       

      End Sub
* Show web-page's source code:

VB Code:
Code:
Private Sub Form_Load()
Text1.Text = Form1.browser.Document.documentElement.innerHTML
End Sub
6- Important codes: New!


* Pop-ups Blocker

VB Code:
Code:
      Private Function IsPopupWindow() As Boolean

      On Error Resume Next

      If WebBrowser1.Document.activeElement.tagName = "BODY" Or WebBrowser1.Document.activeElement.tagName = "IFRAME" Then

      IsPopupWindow = True

      Else

      IsPopupWindow = False

      End If

      End Function

       

      Private Sub webbrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)

      Dim frm As Form1

      Cancel = IsPopupWindow

      If Cancel = False Then

      Set frm = New Form1

      Set ppDisp = frm.WebBrowser1.object

      frm.Show

      End If

      End Sub
This will block all the pop-ups, but in the same time it will open links in a new window as usual.


* JavaScripts handling

VB Code:
Code:
WebBrowser1.Silent = True 
This will not show any errors from pages while you are using your web-browser, put it in the form load event.

* The size of the browser and the scrollbars code

VB Code:
Code:
      Private Sub Form_Resize()
      On Error Resume Next
      WebBrowser1.Width = Me.ScaleWidth
      WebBrowser1.Height = Me.ScaleHeight - 1680
      End Sub
This code will make your web-browser fit with the overall form structure, and also will make the scrollbars compatible with the form.

Now you got a great web browser with many functions
----------------------------------------------------------------------

I did not make this tutorial zeidhaddadin from vbforums made this tutorial all credits to him. Original Tutorial - [Only registered and activated users can see links. ]

Last edited by amonkeyz; 07-04-2009 at 12:58 PM.
amonkeyz is offline   Reply With Quote
Sponsored Links
Old 07-04-2009, 12:37 PM   #2
Guy
Trial Mod
 
Guy's Avatar
 
Last Online: Yesterday 10:18 PM
Join Date: May 2009
Location: ntoskrnl.exe
Posts: 426
Rep Power: 4
Rep Points: 327
Guy is a jewel in the roughGuy is a jewel in the roughGuy is a jewel in the roughGuy is a jewel in the rough
Feedback: (0)
Points: 13,992.07
Bank: 33.07
Total Points: 14,025.14
Re: VB.Net Web Browser Tutorial

Do you have the original authors permission to be posting this?
__________________
Guy is offline   Reply With Quote
Old 07-04-2009, 12:57 PM   #3
amonkeyz
[Crossfire Master]
 
Last Online: 08-23-2009 01:36 AM
Join Date: May 2009
Location: www.ruttconsulting.com
Posts: 352
Rep Power: 1
Rep Points: 22
amonkeyz is on a distinguished road
Feedback: (0)
Points: 846.98
Bank: 0.00
Total Points: 846.98
Re: VB.Net Web Browser Tutorial

I have not asked the original author that is why I posted credits and a link.
I'm pretty sure all he did was get pieces of code and basic knowledge of VB and put this together.
amonkeyz is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump

All times are GMT -7. The time now is 06:34 AM.

 

Copyright ©2009, GamerzPlanet.Net
Visits: