Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] [WinForms] Getting control from TabPage

Started by d h k Jun 24, 2009 at 4:42 PM 1 replies 1.8k views
Original Post
d h k
d h k
Okay, I'm really somewhat new to .NET and WinForms but I couldn't find an answer anywhere, so maybe somebody can enlighten me here... I have a TabControl that starts out with no tabpages. I also have a button, if you click it a new tab with a WebBrowser control in it is created like this:

tabControl->TabPages->Add ( "tab", "Empty Tab" );

WebBrowser^ webBrowser = gcnew WebBrowser ( );
		
webBrowser->Name = "webBrowser";
webBrowser->AutoSize = true;
webBrowser->Dock = DockStyle::Fill;
		
tabControl->TabPages["tab"]->Controls->Add ( webBrowser );

This works fine. Now, when another event is fired I want to retrieve the WebBrowser control from the tab that is currently selected. This is what I have:

WebBrowser^ webBrowser = gcnew WebBrowser ( );
			
webBrowser = tabControl->SelectedTab->Controls["webBrowser"];

I just want to retrieve that specific control but I can't seem to find any way. I know that "tabControl->SelectedTab->Controls["webBrowser"]" IS a webBrowser control at runtime (if I try to access its "text" property it crashes and tells me that WebBrowser doesn't have a "text") but I can't seem to find a way to convert that general control structure into a specific WebBrowser one. Is there a way? Any help is appreciated.
Fiddler
Fiddler
In C#, you'd do a runtime cast like this:
WebBrowser webBrowser = tabControl.SelectedTab.Controls["webBrowser"] as WebBrowser;if (webBrowser != null){    ...}

In C++ you have the dynamic_cast<> operator, which requires RTTI. I'd guess this is enabled by default for C++/CLI, but I have no clue whether this operator is usable with managed classes.

Edit: it is usable and it's equivalent to the C# 'as' operator.
[OpenTK: C# OpenGL 4.4, OpenGL ES 3.0 and OpenAL 1.1. Now with Linux/KMS support!]
d h k
d h k
Oh man, that was so easy, haha.

I totally didn't think this was an option in C++/CLI but it works flawlessly.

Thanks for the quick help!

[Edited by - d h k on June 25, 2009 11:28:54 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.