I do not know what you call the Form
from the other, but imagining that you have some global class for it:
Class AdministradorDeForms
, is responsible for managing your forms in the program:
public class AdministradorDeForms
{
public Form1 FormConWebBrowser;
public Form2 FormQueLoLlama;
public AdministradorDeForms()
{
FormConWebBrowser = new Form1(this); // Ambos apuntan hacia esta instancia
FormQueLoLlama = new Form2(this); // para navegar.
FormConWebBrowser.Show(); // Muestra el form con el webBrowser
}
}
Form1 (The Form
that has the WebBrowser
):
public partial class Form1 : Form
{
private AdministradorDeForms Admin;
public Form1() { InitializeComponent(); }
public Form1(AdministradorDeForms frm) { InitializeComponent(); Admin = frm; }
public void playGol(string Url)
{
webBrowser1.Navigate(Url);
// Solo por si acaso, actualizamos el form y el webBrowser.
webBrowser1.Refresh(); this.Refresh();
}
// Otras implementaciones y eventos...
}
Form2 (The Form
that you will call Form1
to navigate):
public partial class Form2 : Form
{
private AdministradorDeForms Admin;
public Form2() { InitializeComponent(); }
public Form2(AdministradorDeForms frm) { InitializeComponent(); Admin = frm; }
private void AlgunObjeto_Accion(...)
{
// Supongamos que obtenemos la url de cualquier otro lugar, yo pondré un string
string url = "https://google.com/";
// Llamamos al administrador para navegar en el otro form:
Admin.FormConWebBrowser.playGol(url); // Debería de funcionar :D
}
// Otras implementaciones y eventos...
}
In the AlgunObjeto_Accion(...)
you must change the name and its event parameters for the object that will run the navigation in the other Form
and the class AdministradorDeForms
the instances at the beginning of the program, in void Main()
of your application.
EDIT : For what you implement, you are calling a new instance from a button, it is better to do this following:
Let's imagine that you have your form Marcador
:
// Form marcador
public partial class Marcador : Form
{
private string URL; // Creamos un campo url para pasarlo por el inicializador
public Marcador() { InitializeComponent(); }
public Marcador(string url) { InitializeComponent(); URL = url; }
private void Marcador_Load(object sender, EventArgs e)
{
webBrowser1.Navigate(URL); // Al mostrar el formulario, entonces sale esto!
webBrowser1.Refresh(); // Por si acaso.
}
}
And in this Form
you have the button:
// Form que llama al marcador con el botón:
public partial class Form1 : Form
{
private void btnGol_Click(object sender, EventArgs e)
{
// Al hacer click en el botón btnGol, se crea una nueva instancia
// con la URL
Marcador marcador = new Marcador(@"C:\gol2.swf");
marcador.Show(); // Y se muestra el form....
}
}