Results 1 to 3 of 3

Thread: Can't navigate from different form

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Can't navigate from different form

    Hello, I have 2 forms in my app and want to navigate the webbrowser1 on MainForm using SettingsForm. Here's my code:

    vb.net Code:
    1. If MainForm.WebBrowser1.Url.AbsoluteUri Is "about:blank" Then
    2.     MainForm.WebBrowser1.Navigate("http://www.domain.com")
    3. End If

    My code can't do this, what am I doing wrong?

    EDIT: When I test with MessageBox.Show(MainForm.WebBrowser1.Url.AbsoluteUri) it shows me about:blank
    Last edited by nikel; Oct 22nd, 2015 at 12:52 PM.

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Can't navigate from different form

    Hi,

    I have no idea what the example code you posted has to do with your question but if I ignore that for the moment and assume that you want to control the navigation of a WebBrowser control on Form1 from Form2?

    The best way to do this is to use a Delegate and pass a Reference to a block of code to be executed on Form1 to a variable of that Delegate Type on Form2. You can then Invoke that Variable on Form2 which then executes the code on Form1. As an example have a play with this:-

    Create a Delegate on Form2 and then add a Constructor to accept a reference to the Code to be executed:-
    vb.net Code:
    1. Public Class Form2
    2.  
    3.   'Declare a Delegate with the Same Signature a the Original Sub to be Invoked on Form1
    4.   Public Delegate Sub NavigateWebBrowser(ByVal urlString As String)
    5.   'Create a Private Variable of the above Delegate Type that can be used in this Form2
    6.   Private myForm1Browser As NavigateWebBrowser
    7.  
    8.   Public Sub New(ByVal myBrowserCode As NavigateWebBrowser)
    9.     InitializeComponent()
    10.     myForm1Browser = myBrowserCode
    11.   End Sub
    12.  
    13.   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    14.     'Invoke the Delegate to Execute the Code on Form1
    15.     myForm1Browser.Invoke("www.google.co.uk")
    16.   End Sub
    17. End Class

    Finally, pass a reference to the code to be executed from Form1 to Form2:-
    vb.net Code:
    1. Public Class Form1
    2.  
    3.   Private Sub NavigateWebBrowser(ByVal urlString As String)
    4.     WebBrowser1.Navigate(urlString)
    5.   End Sub
    6.  
    7.   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    8.     'Open Form2 and Pass a Reference to the NavigateWebBrowser Sub to the Delegate Variable in Form2
    9.     Using mySecondForm As New Form2(AddressOf NavigateWebBrowser)
    10.       mySecondForm.ShowDialog()
    11.     End Using
    12.   End Sub
    13. End Class

    Hope that helps.

    Cheers,

    Ian

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Can't navigate from different form

    Hello, I couldn't fix the problem using your code, however I called navigate function just after ShowDialog function in my MainForm. Now it's OK. Thanks for your interest anyway.

Tags for this Thread

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