Newbie: TabPage inheritance woes

M

mat

Hi all,

I have a class which extends System.Windows.Forms.TabPage that I add to
a tab control like this:

NoteTabPage newTab;
newTab = new NoteTabPage();
tabControl.Controls.Add(newTab);

It seems to work fine. However, when I try to call a method of the
selected tab, tabControl.SelectedTab.Cut(), I get the following compile
error:

'System.Windows.Forms.TabPage' does not contain a definition for 'Cut'

I understand why I get this, but not how to get around it. I realise
that I could just use a TabPage instead of my class and add a
RichTextBox to it, but where would I then hold the properties?!

Any help would be great,

mat.
 
B

Bruce Wood

You need to tell the compiler that _you_ know that the TabPage that is
selected is a NoteTabPage, even if the compiler doesn't know that:

NoteTabPage page = tablControl.SelectedTab as NoteTabPage;
if (page != null)
{
page.Cut(); // Or whatever you want to do with your flavour of tab
page.
}

If "page" is null, it means either that nothing was selected, or that
the selected page was not a NoteTabPage.
 
M

mat

Bruce said:
You need to tell the compiler that _you_ know that the TabPage that is
selected is a NoteTabPage, even if the compiler doesn't know that:

NoteTabPage page = tablControl.SelectedTab as NoteTabPage;
if (page != null)
{
page.Cut(); // Or whatever you want to do with your flavour of tab
page.
}

If "page" is null, it means either that nothing was selected, or that
the selected page was not a NoteTabPage.

Brilliant, many thanks Bruce.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top