Accessing a web form control from a different web forms code behind.

I am new to Winform programming and need help figuring something out.

Basically I have two winforms in my project(Winform1 and Winform2).
The first one has a button control on it. The second has a WebBrowser control on it.
The control is named "webBrowser1"
 

When the button on the first Windows form is pressed, in the code behind for that button,
I want to display the second web form which has the WebBrowser control on it. Before
I display it though, I need to access the WebBrowser control and set the URL for it.
So my question is how do I access the WebBrowser control from the code behind of the first web form.

This is as far as I got:


// Button click event for Form1
private void button1_Click(object sender, EventArgs e)
{

   Form2 frm = new Form2();
   // Now how do I access the control webBrowser1 and set the URL for it?
   



   frm.ShowDialog();

}
LVL 2
brgdotnetcontractorAsked:
Who is Participating?
 
haxkmavConnect With a Mentor Commented:
Hi,

Form2 frm=new Form2();
WebBrowser web=(WebBrowser)frm.Controls["webBrowser1"]
web. ...

frm.ShowDialog();

This will work for you, just replace the name in the quotes with your web browser control name.
0
 
brgdotnetcontractorAuthor Commented:
Special thanks, it worked great.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.