I have a WebBrowser Control on a TabPage. The TabPage is created dynamically at runtime. When the Tabpage is created I also add a WebBrowser Control to it. I need to know how to access the WebBrowser Control from my Tab Page. Please keep in mind that both the TabPage and WebBrowser Controls are created at run time. Thank you.

Accessing a child control from a parent
TheBC
Ok. I found how to iterate the controls. but when I try to assign the control to an object that I can use to access the perperties and methods I get an error at compile-time "Error 1 Cannot implicitly convert type 'System.Windows.Forms.Control' to 'System.Windows.Forms.WebBrowser'. An explicit conversion exists (are you missing a cast )"
My Code is below
System.Windows.Forms.WebBrowser wbControl;
tabControl1.SelectTab("Tab3");
foreach (Control Controls in tabControl1.SelectedTab.Controls)
{
if (Controls is WebBrowser)
{
wbControl = Controls;
}
}
What do you think
REasley
Each control, including the main form, has a Controls collection which you can iterate through looking for the control you want. Or, you could create member variables and store them there, seeing as you're creating only one of each, you don't need to use a collection for that.
kef_c80
If you wouldn't mind, would you detail how I can do this Or if not, can you point me to a Howto resource I've never really had to do this before.
Thank you for your reply.
Babau
Form already maintains a Controls property, so your variable named Controls is conflicting with that. Try changing your foreach code to something like foreach(Control c in tabControl1.... like shown below:
foreach (Control c in tabControl1.SelectedTab.Controls)
{
if (c is WebBrowser)
MessageBox.Show("Web browser Url is " + ((WebBrowser)c).Url);
}
SchildB
Thank you guys for your assistance.
Thanks.
rosch
If you set a name to the TabPage and WebBrowser controls when you create them, you can access them much faster, without resorting to iterate through the entire collection.
For instance, if you set the Name property of the TabPage and WebBrowser controls respectively to "MyTab" and "MyBrowser", you can do one of the following:
1) Get the WebBrowser control using Find
Control [] Found = this.Controls.Find ("MyBrowser", true);
if (Found.Length > 0)
WebBrowser wb = (WebBrowser) Found [0];
if you are really sure that the control already exists, you can also use directly:
WebBrowser wb = (WebBrowser) this.Controls.Find ("MyBrowser", true) [0];
2) If the TabPage is directly contained in the Form, and the WebBrowser is directly contained in the TabPage, then you can use:
WebBrowser wb = (WebBrowser) (this.Controls ["MyTab"].Controls ["MyBrowser"]);
Obviously, if the TabPage is not directly contained in the Form, but is contained in another static control, you can simply substitute "this" with the static control.
HTH
--mc
Karol Zadora MSFT
My bad, even naming local variable as Controls still works:
foreach (Control Controls in tabControl1.SelectedTab.Controls)
{
if (Controls is WebBrowser)
MessageBox.Show("Web browser Url is " + ((WebBrowser)Controls).Url);
}
What you were missing was a typecast, so change your code inside if statement to:
wbControl = (WebBrowser)Controls;< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />