0

I made a web browser in C# Windows forms, and I made form 3 to be the history, form 3 contains listbox and a button called go!

I want button_click to navigate the webbrowser1 (located in form1) to listbox1.selecteditem.tostring().

In form1 constructor:

public Form1()
{
    x = new Form3();
    x.Show();
    x.Visible = false;
    InitializeComponent();
}

and in the button that open the form 3

{
    x.Visible = true;
}

in form 3 button that says go :

{
    // namespace.form1.webbrowser1.navigate(listbox1.selecteditem.tostring()); // 
    this.Visible = false;
}

the error in the comment line , what is the solution to access the webbrowser from form 3 !!

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    Possible duplicate of [How to access a Textbox in Form3 from Form1?](http://stackoverflow.com/questions/10158051/how-to-access-a-textbox-in-form3-from-form1) – Uwe Keim Dec 17 '15 at 15:00

1 Answers1

1

Pass Form1 to Form3 constructor as parameter:

class Form3
{
    Form1 _parent;
    public Form3(Form1 parent)
    {
        _parent = parent;
    }

    public void Method()
    {
        _parent.webbrowser1.navigate(listbox1.selecteditem.tostring());
        this.Visible = false;
    }
}

also, make webbrowser1 public or better make a public method in Form1:

class Form1
{  
    public void Navigate(string uri)
    {
        webbrowser1.navigate(uri);
    }
}

and call it from Form3:

    public void Method()
    {
        _parent.Navigate(listbox1.selecteditem.tostring());
        this.Visible = false;
    }
Backs
  • 24,430
  • 5
  • 58
  • 85