Results 1 to 40 of 135

Thread: Webbrowser Control Tip and Examples

Threaded View

  1. #1

    Thread Starter
    Frenzied Member wiz126's Avatar
    Join Date
    Jul 2005
    Location
    Mars,Milky Way... Chit Chat Posts: 5,733
    Posts
    1,080

    Cool Webbrowser Control Tip and Examples

    Webbrowser control

    Since I have been seen lately many people asking questions about the webbrowser control.

    First of all the webbrowser control is not one of the controls that come by default in the control box. All you have to do to add it there is to press Ctrl + T key or (Project -> Components using the menu). Once in the components select window scroll down and check the box next to “Microsoft Internet Controls” and click ok.

    Examples:
    • Navigating to a site
    • Popup browser using your own form
    • Check if word/string is found on the page
    • Making page on startup
    • Regular Browser Functions
    • Advanced browser functions
    • Changing web browser’s Font Size
    • Disabling functions appropriately (Back/Forward)
    • Disabling functions appropriately (page setup/print preview/print setup)
    • Removing Right Click Menu From the browser control
    • Grab all links on the page
    • Save Page
    • Open Page
    • Auto Submit
    • Using A ProgressBar With The Webbrowser
    • Setting a Control in a Webbrowser to focus
    • Checkbox in a page, how to control it
    • Custom Right Click Menu


    Navigating to a site:
    Navigating to a site...
    VB Code:
    1. WebBrowser1.Navigate "www.google.com"
    Opening a popup window with your app
    If the user go to a site where a new page needs to come in a new browser this code is made to open the target site in a new form.
    VB Code:
    1. Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)
    2. Dim frm As Form1
    3. Set frm = New Form1
    4. Set ppDisp = frm.WebBrowser1.Object
    5. frm.Show
    6. End Sub
    Check if word/string is found on the page
    This code will let you check the page for a word or a sentence.
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim strfindword As String
    3.         strfindword = InputBox("What are you looking for?", "Find", "") ' what word to find?
    4.             If WebPageContains(strfindword) = True Then 'check if the word is in page
    5.                 MsgBox "The webpage contains the text" 'string is in page
    6.             Else
    7.                 MsgBox "The webpage doesn't contains the text" 'string is not in page
    8.             End If
    9. End Sub
    10. Private Function WebPageContains(ByVal s As String) As Boolean
    11.     Dim i As Long, EHTML
    12.     For i = 1 To WebBrowser1.Document.All.length
    13.         Set EHTML = _
    14.         WebBrowser1.Document.All.Item(i)
    15.  
    16.  
    17.         If Not (EHTML Is Nothing) Then
    18.             If InStr(1, EHTML.innerHTML, _
    19.             s, vbTextCompare) > 0 Then
    20.             WebPageContains = True
    21.             Exit Function
    22.         End If
    23.     End If
    24. Next i
    25. End Function
    26. Private Sub Form_Load()
    27.     WebBrowser1.Navigate2 "www.msn.com"
    28. End Sub
    Making page on startup
    This code show you how to make a page and show it on event.
    VB Code:
    1. Private Sub Form_Load()
    2.     WebBrowser1.Navigate "about:blank"
    3. End Sub
    4.  
    5. Private Sub Command1_Click()
    6.     Dim HTML As String
    7.         '----------The HTML CODE GOES FROM HERE AND DOWN----------
    8.     HTML = "<HTML>" & _
    9.             "<TITLE>Page On Load</TITLE>" & _
    10.             "<BODY>" & _
    11.             "<FONT COLOR = BLUE>" & _
    12.             "This is a " & _
    13.             "<FONT SIZE = 5>" & _
    14.             "<B>" & _
    15.             "programmatically " & _
    16.             "</B>" & _
    17.             "</FONT SIZE>" & _
    18.             "made page" & _
    19.             "</FONT>" & _
    20.             "</BODY>" & _
    21.             "</HTML>"
    22.             '----------The HTML CODE GOES HERE AND ABOVE----------
    23.     WebBrowser1.Document.Write HTML
    24. End Sub
    Regular Browser Functions
    This are the basic internet browser's functions.
    VB Code:
    1. Private Sub Command1_Click(Index As Integer)
    2. On Error Resume Next ' just in case there is no page back or forward
    3.                     'I showed how to disabel them if you scroll down more
    4.     Select Case Index
    5.         Case 0 'Go Back Button
    6.             WebBrowser1.GoBack 'Go Back one Page
    7.         Case 1 'Go Forward Button
    8.             WebBrowser1.GoForward 'Go Forward one Page
    9.         Case 2 'Stop Button
    10.             WebBrowser1.Stop 'stop page
    11.         Case 3 'Refresh Button
    12.             WebBrowser1.Refresh 'refresh page
    13.         Case 4 'Go Home Button
    14.             WebBrowser1.GoHome 'Go to home page
    15.         Case 5 'Search Button
    16.             WebBrowser1.GoSearch 'Search
    17.     End Select
    18. End Sub
    Advanced Browser Functions
    This are the more complex browser functions like print and page Properties.
    VB Code:
    1. Private Sub Command1_Click() 'Print Button
    2.     WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DODEFAULT 'Show Print Window
    3. End Sub
    4. Private Sub Command2_Click() 'Print Preview Button
    5.     WebBrowser1.ExecWB OLECMDID_PRINTPREVIEW, OLECMDEXECOPT_DODEFAULT 'Show Print Preview Window
    6. End Sub
    7. Private Sub Command3_Click() 'Page Setup Button
    8.     WebBrowser1.ExecWB OLECMDID_PAGESETUP, OLECMDEXECOPT_DODEFAULT 'Show Page Setup Window
    9. End Sub
    10. Private Sub Command4_Click() 'Page Properties Button
    11.     WebBrowser1.ExecWB OLECMDID_PROPERTIES, OLECMDEXECOPT_DODEFAULT 'Show Page Properties Window
    12. End Sub
    13. Private Sub Form_Load()
    14.     WebBrowser1.Navigate "www.google.com"
    15. End Sub
    Changing web browser’s Font Size
    This Code shows you how to change the page font size, just like internet explorer View -> Text Size Menu
    VB Code:
    1. Private Sub Command1_Click() ' Smallest Button
    2. On Error Resume Next
    3.     WebBrowser1.ExecWB OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(0), vbNull
    4. End Sub
    5. Private Sub Command2_Click() 'Small Button
    6. On Error Resume Next
    7.     WebBrowser1.ExecWB OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(1), vbNull
    8. End Sub
    9. Private Sub Command3_Click() 'Medium Button
    10. On Error Resume Next
    11.     WebBrowser1.ExecWB OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(2), vbNull
    12. End Sub
    13. Private Sub Command4_Click() 'Large Button
    14. On Error Resume Next
    15.     WebBrowser1.ExecWB OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(3), vbNull
    16. End Sub
    17. Private Sub Command5_Click() 'Largest Button
    18. On Error Resume Next
    19.     WebBrowser1.ExecWB OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(4), vbNull
    20. End Sub
    21. Private Sub Form_Load()
    22.     WebBrowser1.Navigate2 "www.google.com"
    23. End Sub
    Disabling functions appropriately (Back/Forward)
    This code shows you how to appropriately disable the back and forward button.
    VB Code:
    1. Private Sub Command1_Click() 'Go Back Button
    2.     WebBrowser1.GoBack 'Go Back
    3. End Sub
    4. Private Sub Command2_Click() 'Go Forward Button
    5.     WebBrowser1.GoForward 'Go Forward
    6. End Sub
    7. Private Sub Form_Load()
    8.     WebBrowser1.Navigate "www.google.com"
    9. End Sub
    10. Private Sub WebBrowser1_CommandStateChange(ByVal Command As Long, ByVal Enable As Boolean)
    11.     Select Case Command
    12.         Case 1 'Forward
    13.             Command2.Enabled = Enable
    14.         Case 2 'Back
    15.             Command1.Enabled = Enable
    16.     End Select
    17. End Sub
    Disabling functions appropriately (page setup/print preview/print setup)
    This code shows you how to appropriately disable the page setup or print setup.
    VB Code:
    1. Private Sub Command1_Click() 'Print Button
    2.     WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DODEFAULT 'Show Print Window
    3. End Sub
    4. Private Sub Command2_Click() 'Print Preview Button
    5.     WebBrowser1.ExecWB OLECMDID_PRINTPREVIEW, OLECMDEXECOPT_DODEFAULT 'Show Print Preview Window
    6. End Sub
    7. Private Sub Command3_Click() 'Page Setup Button
    8.     WebBrowser1.ExecWB OLECMDID_PAGESETUP, OLECMDEXECOPT_DODEFAULT 'Show Page Setup Window
    9. End Sub
    10. Private Sub Form_Load()
    11.     WebBrowser1.Navigate "www.google.com"
    12. End Sub
    13. Public Function Enable_or_Disable()
    14.     If WebBrowser1.QueryStatusWB(OLECMDID_PRINT) = 0 Then
    15.         Command1.Enabled = False
    16.     Else
    17.         Command1.Enabled = True
    18.     End If
    19.     If WebBrowser1.QueryStatusWB(OLECMDID_PRINTPREVIEW) = 0 Then
    20.         Command2.Enabled = False
    21.     Else
    22.         Command2.Enabled = True
    23.     End If
    24.  
    25.     If WebBrowser1.QueryStatusWB(OLECMDID_PAGESETUP) = 0 Then
    26.         Command3.Enabled = False
    27.     Else
    28.         Command3.Enabled = True
    29.     End If
    30. End Function
    31. Private Sub WebBrowser1_BeforeNavigate2 _
    32.                     (ByVal pDisp As Object, _
    33.                                 URL As Variant, _
    34.                                 Flags As Variant, _
    35.                         TargetFrameName As Variant, _
    36.                                 PostData As Variant, _
    37.                                     Headers As Variant, _
    38.                                         Cancel As Boolean)
    39.     Enable_or_Disable
    40. End Sub
    41. Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    42.     Enable_or_Disable
    43. End Sub
    Removing Right Click Menu From the browser control
    So you need to remove the right click menu from the control, right? well there are more then two ways one of them, which i knew but i won't bother telling is using lots of hooking, waist of thime since all you have to do is download a simple helper file.
    First you need to go to http://support.microsoft.com/kb/q183235/ and download the WBCustomizer.dll. once done go to the"Project Menu" and click on "Refrences" Click on browse and add the 'WBCustomizer.dll' to you app. once done just add this simple code.
    VB Code:
    1. Option Explicit
    2.     Dim CustomWB As WBCustomizer 'Deceler the CustomWB
    3. Private Sub Form_Load()
    4.    Set CustomWB = New WBCustomizer
    5.    With CustomWB
    6.       .EnableContextMenus = False 'Disable The Menu
    7.       .EnableAllAccelerators = True
    8.      
    9.       Set .WebBrowser = WebBrowser1
    10.    End With
    11.     WebBrowser1.Navigate "www.google.com"
    12.         CustomWB.EnableContextMenus = False
    13. End Sub
    Last edited by wiz126; Mar 23rd, 2006 at 03:46 PM.
    1) If your post has been adequately answered please click in your post on "Mark Thread Resolved".
    2) If someone has been useful to you please show your respect by rating their posts.
    3) Please use [highlight="VB"] 'your code goes in here [/highlight] tags when posting code.
    4) Before posting your question, make sure you checked this links:
    MICROSOFT MSDN -- VB FORUMS SEARCH

    5)Support Classic VB - A PETITION TO MICROSOFT

    ___________________________________________________________________________________
    THINGS TO KNOW ABOUT VB: || VB Examples/Demos
    What are Classes?
    || -
    Where to place a sub/function?(global) || Webbrowser control

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