Home > DeveloperSection > Forums > How can I call webbrowser from another form c#
Mark Devid
Mark Devid

Total Post:102

Points:714
Posted on    October-05-2013 2:26 AM

 C# C# 
Ratings:


 1 Reply(s)
 1509  View(s)
Rate this:

I took two form (form1 & form2) in my program. in form1 i took a button & in form2 i took a webbrowser.

i want to run webbrowser on form2 by clicking on form1 button.

but my program not working.

Please help me, how to do this currently.

this is my program code:

public partial class Form1 : Form
{
 
    public Form1()
    {
        InitializeComponent();
    }
 
    private void progressBar1_Click(object sender, EventArgs e)
    {
 
    }
 
    private void button1_Click(object sender, EventArgs e)
    {
        try 
        {
            webBrowser1.Navigate(textBox1.Text);
        }
        catch(Exception)
        {
            MessageBox.Show("Connect to Internet First","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
        }
    }
 
    private void Form1_Load(object sender, EventArgs e)
    {
 
    }
}


Dag Hammarskjold
Dag Hammarskjold

Total Post:24

Points:168
Posted on    October-05-2013 4:13 AM

First, in form1.designer, change this line of code:

private System.Windows.Forms.WebBrowser webBrowser1;

to:

public System.Windows.Forms.WebBrowser webBrowser1;

now, in form1 button_click:

    Form2 f = new Form2();
    f.webBrowser1.Navigate("http://www.mindstick.com");
    f.Show(this);

solution II: In form1:

private void button1_Click(object sender, EventArgs e)
{
    Form2 f = new Form2();
    f.Url = "http://www.mindstick.com";
    f.Show(this);
}

In form2:

public string Url { get; set; }
 
    private void Form2_Load(object sender, EventArgs e)
    {
        this.webBrowser1.Navigate(this.Url);
    }

Don't want to miss updates? Please click the below button!

Follow MindStick