Results 1 to 40 of 135

Thread: Webbrowser Control Tip and Examples

Threaded View

  1. #2

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

    Re: Webbrowser Control Tip and Examples

    Grab all links on the page
    This code shows how to grab and list all the links on a page, this can be used a spider software for a search engine site.
    In order to get this code to load you must add the "Microsoft HTML Object Library" into your app refrences.
    VB Code:
    1. Option Explicit
    2. Private Sub Form_Load()
    3.     WebBrowser1.Navigate "www.vbforums.com"
    4. End Sub
    5. Private Sub WebBrowser1_DownloadComplete()
    6.     'you must add the "Microsoft HTML Object Library"!!!!!!!!!
    7.     Dim HTMLdoc As HTMLDocument
    8.         Dim HTMLlinks As HTMLAnchorElement
    9.             Dim STRtxt As String
    10.     ' List the links.
    11.     On Error Resume Next
    12.         Set HTMLdoc = WebBrowser1.Document
    13.             For Each HTMLlinks In HTMLdoc.links
    14.                 STRtxt = STRtxt & HTMLlinks.href & vbCrLf
    15.             Next HTMLlinks
    16.         Text1.Text = STRtxt
    17. End Sub
    You can add this code in order to log this files.
    VB Code:
    1. Open "C:\Documents and Settings\[YOU USERNAME]\Desktop\link log.txt" For Append As #1
    2. Print #1, STRtxt
    3. Close #1
    Save Page
    This code shows you how to save the browser's page.
    VB Code:
    1. Option Explicit
    2. Private Sub Command1_Click()
    3. WebBrowser1.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT
    4. End Sub
    5. Private Sub Form_Load()
    6. WebBrowser1.Navigate2 "www.google.com"
    7. End Sub
    Open Page
    Here is how to load a webpage into the webbrowser.
    VB Code:
    1. Private Sub Command2_Click()
    2.     WebBrowser1.ExecWB OLECMDID_OPEN, OLECMDEXECOPT_PROMPTUSER
    3. End Sub
    This is how to open a page, using the comman dialog's way
    VB Code:
    1. Option Explicit
    2. Private Sub Command1_Click()
    3. On Error Resume Next
    4.     With CommonDialog1
    5.         .DialogTitle = "Open File"
    6.         .Filter = "Web page (*.htm;*.html) | *.htm;*.html|" & _
    7.         "All Supported Picture formats|*.gif;*.tif;*.pcd;*.jpg;*.wmf;" & _
    8.         "*.tga;*.jpeg;*.ras;*.png;*.eps;*.bmp;*.pcx|" & _
    9.         "Text formats (*.txt;*.doc)|*.txt;*.doc|" & _
    10.         "All files (*.*)|*.*|"
    11.         .ShowOpen
    12.         .Flags = 5
    13.     WebBrowser1.Navigate2 .FileName
    14.     End With
    15. End Sub
    16. Private Sub Form_Load()
    17.     WebBrowser1.Navigate2 "www.google.com"
    18. End Sub
    Auto Submit
    This Simple Code I Made To show you how to make a submittion form. This code will autofill the need filled and submit it.
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim strwebsite As String
    3.     Dim stremail As String
    4.         strwebsite = "http://www.mysite.com"
    5.             stremail = "[email protected]"
    6. WebBrowser1.Document.addurl.URL.Value = strwebsite
    7.     WebBrowser1.Document.addurl.Email.Value = stremail
    8.         WebBrowser1.Document.addurl.Submit
    9. End Sub
    10. Private Sub Form_Load()
    11.     WebBrowser1.Navigate "http://www.scrubtheweb.com/addurl.html"
    12. End Sub
    Using A ProgressBar With The Webbrowser
    This is to show how to use a progressbar with a webbrowser control.
    VB Code:
    1. Private Sub Form_Load()
    2.     WebBrowser1.Navigate "www.msn.com"
    3.     ProgressBar1.Appearance = ccFlat
    4.     ProgressBar1.Scrolling = ccScrollingSmooth
    5. End Sub
    6.  
    7. Private Sub WebBrowser1_ProgressChange(ByVal Progress As Long, ByVal ProgressMax As Long)
    8. On Error Resume Next
    9.     If Progress = -1 Then ProgressBar1.Value = 100
    10.         Me.Caption = "100%"
    11.     If Progress > 0 And ProgressMax > 0 Then
    12.         ProgressBar1.Value = Progress * 100 / ProgressMax
    13.         Me.Caption = Int(Progress * 100 / ProgressMax) & "%"
    14.     End If
    15.     Exit Sub
    16. End Sub
    Setting a Control in a Webbrowser to focus
    This shows how to set a control inside the webbrowser into focus.
    VB Code:
    1. Private Sub Command1_Click()
    2.     WebBrowser1.Document.All("q").focus 'Set the search text filed in focus
    3. End Sub
    4.  
    5. Private Sub Command2_Click()
    6.     WebBrowser1.Document.All("btnI").focus 'Set the google "I Am feeling lucky in focus button"
    7. End Sub
    8.  
    9. Private Sub Form_Load()
    10.     WebBrowser1.Navigate "http://www.google.com/"
    11. End Sub

    Or you can use:
    VB Code:
    1. WebBrowser1.Document.getElementById("Object's Name").Focus
    Checkbox in a page, how to control it
    This is an example on how to check or uncheck the remember me checkbox on the google login page:

    VB Code:
    1. Private Sub Form_Load()
    2. WebBrowser1.Navigate "https://www.google.com/accounts/ManageAccount"
    3. End Sub
    4. Private Sub Check1_Click()
    5.     If Check1.Value = 0 Then
    6.         WebBrowser1.Document.All.PersistentCookie.Checked = False 'unchecked
    7.     Else
    8.         WebBrowser1.Document.All.PersistentCookie.Checked = True 'checked
    9.     End If
    10. End Sub
    Or

    VB Code:
    1. Private Sub Form_Load()
    2. WebBrowser1.Navigate "https://www.google.com/accounts/ManageAccount"
    3. End Sub
    4. Private Sub Check1_Click()
    5.     If Check1.Value = 0 Then
    6.         WebBrowser1.Document.All.PersistentCookie.Checked = 0 'unchecked
    7.     Else
    8.         WebBrowser1.Document.All.PersistentCookie.Checked = 1 'checked
    9.     End If
    10. End Sub
    Or
    VB Code:
    1. Private Sub Form_Load()
    2. WebBrowser1.Navigate "https://www.google.com/accounts/ManageAccount"
    3. End Sub
    4. Private Sub Check1_Click()
    5.     If Check1.Value = 0 Then
    6.         WebBrowser1.Document.getElementById("PersistentCookie").Checked = False 'unchecked
    7.     Else
    8.         WebBrowser1.Document.getElementById("PersistentCookie").Checked = True 'checked
    9.     End If
    10. End Sub
    Custom Right Click Menu
    This is an example show how to make your own custom right click menu. in order for this to work you must add the "Must Add Microsoft HTML Object Library" to your refrance.Also make your own custom menu using the menu editor I named my "mnu"
    Please Note it will effect all the context menus in the webbrowser.

    VB Code:
    1. 'Must Add Microsoft HTML Object Library
    2. Option Explicit
    3.     Public WithEvents HTML As HTMLDocument
    4.  
    5. Private Function HTML_oncontextmenu() As Boolean
    6.    HTML_oncontextmenu = False
    7.    PopupMenu mnu '<---Check the mnu to your own menu name
    8. End Function
    9.  
    10. Private Sub Form_Load()
    11.     WebBrowser1.Navigate "www.google.com"
    12. End Sub
    13.  
    14. Private Sub Form_Unload(Cancel As Integer)
    15.    Set HTML = Nothing
    16. End Sub
    17. Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, _
    18.                                                  URL As Variant)
    19.    Set HTML = WebBrowser1.Document
    20. End Sub
    Last edited by wiz126; Mar 23rd, 2006 at 04:34 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