HOW CAN I CALL WEBBROWSER FROM ANOTHER FORM C#

Mark Devid

Total Post:102

Points:714
Posted by  Mark Devid
C# 
C#
 1844  View(s)
Ratings:
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)
    {
 
    }
}
  1. Dag Hammarskjold

    Post:24

    Points:168
    Re: How can I call webbrowser from another form c#

    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);
        }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!