Results 1 to 10 of 10

Thread: [RESOLVED] problems with navigating webbrowser on form1 from form2

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Location
    Overland Park Kansas
    Posts
    183

    Resolved [RESOLVED] problems with navigating webbrowser on form1 from form2

    I have webbrowser1 on form1.

    I would like for a user to be able to click a button on form2 and have that button do two things

    1. navigate form1.webbrowser1 from one document to another (for the sake of this lets just say www.google.com to www.yahoo.com)

    2. after the webbrowser has navigated continue with the rest of the code

    The problem is that the webbrowser will only navigate after the form2 button click is done or after i make a msgbox popup.

    Why would the webrowser only navigate after a msgbox pops up?


    an example of my form2.buttonclick event






    form1.webbrowser1.navigate("www.yahoo.com")

    debug.print form1.webbrowser1.url
    'shows www.google.com

    'i tried making a do while webbrowser1.readystate loop here but it goes forever because webbrowser1 never navigates
    'i also tried using the form1.webrowser1 on navigate completed event but again it fails because webrowser1 never navigates.


    msgbox ("this msgbox makes the webbrowser navigate")

    debug.print form1.webbrowser1.url
    'shows www.yahoo.com

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: problems with navigating webbrowser on form1 from form2

    It's not that the browser doesn't navigate. It's that your blocking the thread with your wait loop so it never shows the page. The msgbox doesn't have anything to do with anything. It just frees the thread to complete its task. If you're going to use a readystate loop at least allow the program to carry out its tasks ...

    Form1.WebBrowser1.Navigate("www.yahoo.com")
    While Form1.WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
    Me.Text = "Waiting"
    Application.DoEvents()
    End While
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: problems with navigating webbrowser on form1 from form2

    In your form1, you'll have 3 events. The first is a button's click, the second is the webbrowser's document completed, and the third is the form load. In the button, you'll show form2. In the document completed you perform any code you want to perform after you visit a website. In the load, you simply navigate to yahoo.
    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Form2.Show()
        End Sub
    
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            'Rest of the code once it's been navigated
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            WebBrowser1.Navigate("www.yahoo.com")
        End Sub
    End Class
    In form2, you'll generate the button's click event. In that event you'll navigate form1's webbrowser:
    Code:
    Option Strict On
    Option Explicit On
    Public Class Form2
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Form1.WebBrowser1.Navigate("www.google.com")
        End Sub
    End Class
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Location
    Overland Park Kansas
    Posts
    183

    Re: problems with navigating webbrowser on form1 from form2

    Quote Originally Posted by dday9 View Post
    In your form1, you'll have 3 events. The first is a button's click, the second is the webbrowser's document completed, and the third is the form load. In the button, you'll show form2. In the document completed you perform any code you want to perform after you visit a website. In the load, you simply navigate to yahoo.
    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Form2.Show()
        End Sub
    
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            'Rest of the code once it's been navigated
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            WebBrowser1.Navigate("www.yahoo.com")
        End Sub
    End Class
    In form2, you'll generate the button's click event. In that event you'll navigate form1's webbrowser:
    Code:
    Option Strict On
    Option Explicit On
    Public Class Form2
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Form1.WebBrowser1.Navigate("www.google.com")
        End Sub
    End Class

    This is not a solution. Let's just say that form2 will navigate form1.webbrowser1 and then, after that navigation is completed, put form2 in the yahoo search bar. I can't put the form2 into the yahoo search bar on every webbrowser1 navigate event because webbrowser1 will navigate away from yahoo and it starts on google and there will be an error when i try to put text into the yahoo search box and i'm on google.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Location
    Overland Park Kansas
    Posts
    183

    Re: problems with navigating webbrowser on form1 from form2

    Quote Originally Posted by dunfiddlin View Post
    It's not that the browser doesn't navigate. It's that your blocking the thread with your wait loop so it never shows the page. The msgbox doesn't have anything to do with anything. It just frees the thread to complete its task. If you're going to use a readystate loop at least allow the program to carry out its tasks ...

    Form1.WebBrowser1.Navigate("www.yahoo.com")
    While Form1.WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
    Me.Text = "Waiting"
    Application.DoEvents()
    End While
    I can't do application.doevents() because application is ambiguous it's been imported from excel, outlook and windows forms.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: problems with navigating webbrowser on form1 from form2

    And I suppose mine got overlooked ;]
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: problems with navigating webbrowser on form1 from form2

    Ok. No matter!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Location
    Overland Park Kansas
    Posts
    183

    Re: problems with navigating webbrowser on form1 from form2

    Quote Originally Posted by dday9 View Post
    And I suppose mine got overlooked ;]
    No it didn't get overlooked the problem is that i wanted form4 to be the only one to run the code after it navigated form1.webrowser1.

    form4 navigates form1 to google and fills out the google search box or form4 navigates form1 to yahoo and fills out the yahoo search box. If i put the filling out of the searchbox on form1.webbrowser1.navigatecomplete event then if the user navigates to www.cnn.com the form will try to fill out a yahoo or google search box where there is none.



    I found a workaround which involved making a wait function in a module and calling the wait function. the wait function was a combination of sleeping the thread and application.doevents() this solved all my problems. How do I mark this thread as resolved?
    Last edited by iamcpc; Jan 16th, 2013 at 07:50 PM. Reason: error

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: problems with navigating webbrowser on form1 from form2

    How do I mark this thread as resolved?
    Thread Tools up the top of the page.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Location
    Overland Park Kansas
    Posts
    183

    Re: problems with navigating webbrowser on form1 from form2

    Is there any way that I can mark this thread as unsolved because application.doevents() wound up causing a lot of headache.

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