-
Oct 22nd, 2015, 12:44 PM
#1
Thread Starter
Addicted Member
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:
If MainForm.WebBrowser1.Url.AbsoluteUri Is "about:blank" Then
MainForm.WebBrowser1.Navigate("http://www.domain.com")
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.
-
Oct 22nd, 2015, 11:52 PM
#2
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:
Public Class Form2 'Declare a Delegate with the Same Signature a the Original Sub to be Invoked on Form1 Public Delegate Sub NavigateWebBrowser(ByVal urlString As String) 'Create a Private Variable of the above Delegate Type that can be used in this Form2 Private myForm1Browser As NavigateWebBrowser Public Sub New(ByVal myBrowserCode As NavigateWebBrowser) InitializeComponent() myForm1Browser = myBrowserCode End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Invoke the Delegate to Execute the Code on Form1 myForm1Browser.Invoke("www.google.co.uk") End Sub End Class
Finally, pass a reference to the code to be executed from Form1 to Form2:-
vb.net Code:
Public Class Form1 Private Sub NavigateWebBrowser(ByVal urlString As String) WebBrowser1.Navigate(urlString) End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Open Form2 and Pass a Reference to the NavigateWebBrowser Sub to the Delegate Variable in Form2 Using mySecondForm As New Form2(AddressOf NavigateWebBrowser) mySecondForm.ShowDialog() End Using End Sub End Class
Hope that helps.
Cheers,
Ian
-
Oct 23rd, 2015, 12:14 PM
#3
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|