Control webBrowser does not load

1

You will see I have a webBroser that I pass a URL such that:

public Marcador()
    { 
        InitializeComponent();       
    }
public void playGol(String url)
    {          
        webBrowser1.Navigate(url);
    }

When I call the method playGol() from another form the webBroser1 does not show me anything, blank screen, however if I put the sentence: webBrowser1.Navigate("http://---"); in the constructor I load the web correctly, Does anyone know why pass? (The URL of the web arrives at the URL parameter)

Another thing is that I try to open a SWF and what it does is open the Chrome and download the file, how can I open the webBroser and not the chrome and show me (not download) the SWF ?

Thank you in advance

Edit: I'm calling from another form:

private void btnGol_Click(object sender, EventArgs e) 
{ 
Marcador marcador = new Marcador();
 marcador.playGol(@"C:\gol2.swf"); 
}
    
asked by Fran Pino 03.08.2016 в 09:28
source

1 answer

1

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....
    }
}
    
answered by 03.08.2016 / 14:15
source