Results 1 to 40 of 108

Thread: vb web browser tutorial

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    384

    Post vb web browser tutorial

    UPDATED ON 18/6/2006

    In this thread i'm going to talk about all the things that you might need in building an internet web browser using visualbasic 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 webbrowser 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:
    1. WebBrowser1.Navigate Combo1

    Now We will code the other buttons as the following:

    Back button:

    VB Code:
    1. On Error Resume Next
    2. WebBrowser1.GoBack

    Forward button:
    VB Code:
    1. On Error Resume Next
    2. WebBrowser1.GoForward

    Stop button:
    VB Code:
    1. On Error Resume Next
    2. WebBrowser1.Stop

    Refresh button:
    VB Code:
    1. WebBrowser1.Refresh

    Home button:
    VB Code:
    1. 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:
    1. 'This to make the progress bar work and to show a status message, and an image.
    2. Private Sub WebBrowser1_ProgressChange(ByVal Progress As Long, ByVal ProgressMax As Long)
    3. On Error Resume Next
    4.     If Progress = -1 Then ProgressBar1.Value = 100 'the name of the progress bar is "ProgressBar1".
    5.         Label1.Caption = "Done"
    6.         ProgressBar1.Visible = False 'This makes the progress bar disappear after the page is loaded.
    7.         Image1.Visible = True
    8.     If Progress > 0 And ProgressMax > 0 Then
    9.         ProgressBar1.Visible = True
    10.         Image1.Visible = False
    11.         ProgressBar1.Value = Progress * 100 / ProgressMax
    12.         Label1.Caption = Int(Progress * 100 / ProgressMax) & "%"
    13.     End If
    14.     Exit Sub
    15. 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:
    1. 'This to open a new window with our browser.
    2. Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)
    3. Dim frm As Form1
    4. Set frm = New Form1
    5. Set ppDisp = frm.WebBrowser1.Object
    6. frm.Show
    7. End Sub

    This to open the new window with your browser.


    * History and current visited site.

    VB Code:
    1. 'This keeps the visited sites history and also changes the title of the browser as the page title.
    2. Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
    3.     On Error Resume Next
    4.     Dim i As Integer
    5.     Dim bFound As Boolean
    6.     Me.Caption = WebBrowser1.LocationName
    7.     For i = 0 To Combo1.ListCount - 1
    8.         If Combo1.List(i) = WebBrowser1.LocationURL Then
    9.             bFound = True
    10.             Exit For
    11.         End If
    12.     Next i
    13.     mbDontNavigateNow = True
    14.     If bFound Then
    15.         Combo1.RemoveItem i
    16.     End If
    17.     Combo1.AddItem WebBrowser1.LocationURL, 0
    18.     Combo1.ListIndex = 0
    19.     mbDontNavigateNow = False
    20. End Sub


    5- More coding:

    You can add more buttons to your browser as those:

    * Find if a word is in the page (taken from a tutorial on this forum).

    VB Code:
    1. 'This to tell you if a word is in the page, Here we call the WebPageContains function.
    2. Private Sub Command7_Click()
    3.     Dim strfindword As String
    4.         strfindword = InputBox("What are you looking for?", "Find", "") ' what word to find?
    5.             If WebPageContains(strfindword) = True Then 'check if the word is in page
    6.                 MsgBox "The webpage contains the text" 'string is in page
    7.             Else
    8.                 MsgBox "The webpage doesn't contains the text" 'string is not in page
    9.             End If
    10. End Sub
    11.  
    12. 'This is the finding function.
    13. Private Function WebPageContains(ByVal s As String) As Boolean
    14.     Dim i As Long, EHTML
    15.     For i = 1 To WebBrowser1.Document.All.length
    16.         Set EHTML = _
    17.         WebBrowser1.Document.All.Item(i)
    18.  
    19.  
    20.         If Not (EHTML Is Nothing) Then
    21.             If InStr(1, EHTML.innerHTML, _
    22.             s, vbTextCompare) > 0 Then
    23.             WebPageContains = True
    24.             Exit Function
    25.         End If
    26.     End If
    27. Next i
    28. End Function

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

    * Print a page
    VB Code:
    1. WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DODEFAULT

    * Save a page
    VB Code:
    1. WebBrowser1.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT

    * Delete cookies from your computer

    VB Code:
    1. 'This code is used to empty the cookies from the user's computer / We call function from here.
    2. Private Declare Function GetUserName _
    3. Lib "advapi32.dll" Alias "GetUserNameW" ( _
    4. ByVal lpBuffer As Long, _
    5. ByRef nSize As Long) As Long
    6.  
    7. Private Declare Function SHGetSpecialFolderPath _
    8. Lib "shell32.dll" Alias "SHGetSpecialFolderPathA" ( _
    9. ByVal hwnd As Long, _
    10. ByVal pszPath As String, _
    11. ByVal csidl As Long, _
    12. ByVal fCreate As Long) As Long
    13.  
    14. Private Const CSIDL_COOKIES As Long = &H21
    15.  
    16. 'This calls the function that deletes the cookies.
    17. Public Sub Command1_Click()
    18. Dim sPath As String
    19. sPath = Space(260)
    20. Call SHGetSpecialFolderPath(0, sPath, CSIDL_COOKIES, False)
    21. sPath = Left$(sPath, InStr(sPath, vbNullChar) - 1) & "\*.txt*"
    22. On Error Resume Next
    23. Kill sPath
    24.  
    25. End Sub


    * Show webpage's source code:

    VB Code:
    1. Private Sub Form_Load()
    2. Text1.Text = Form1.browser.Document.documentElement.innerHTML
    3. End Sub


    6- Important codes: New!


    * Popups Blocker

    VB Code:
    1. Private Function IsPopupWindow() As Boolean
    2. On Error Resume Next
    3. If WebBrowser1.Document.activeElement.tagName = "BODY" Or WebBrowser1.Document.activeElement.tagName = "IFRAME" Then
    4. IsPopupWindow = True
    5. Else
    6. IsPopupWindow = False
    7. End If
    8. End Function
    9.  
    10. Private Sub webbrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)
    11. Dim frm As Form1
    12. Cancel = IsPopupWindow
    13. If Cancel = False Then
    14. Set frm = New Form1
    15. Set ppDisp = frm.WebBrowser1.object
    16. frm.Show
    17. End If
    18. End Sub

    This will block all the popups, but in the same time it will open links in a new window as usual.


    * JavaScripts handeling

    VB Code:
    1. 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:
    1. Private Sub Form_Resize()
    2. On Error Resume Next
    3. WebBrowser1.Width = Me.ScaleWidth
    4. WebBrowser1.Height = Me.ScaleHeight - 1680
    5. End Sub

    This code will make your webbrowser fit with the overall form structure, and also will make the scrollbars compitable with the form.


    Now you got a great web browser with many functions

    Need icons for your application? just do a fast forum search and you will find a great sites for icons or images.
    Attached Images Attached Images  
    Last edited by zeidhaddadin; Jun 17th, 2006 at 05:14 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width