Why Does This Not Work - DirectCast

  • Thread starter Thread starter Paul Timmerman
  • Start date Start date
P

Paul Timmerman

Dim CurrentTabPage As Control
CurrentTabPage = DirectCast("tcontrol.SelectedTab.Name",
Control)
MessageBox.Show(CurrentTabPage.Name)


I get a SPECIFIED CAT IS NOT VALID


Simply described:

I want to determine the name of the tabpage that I am on
(it is created dynamically) and then be able to reference
the tabpages properties. That is why I am trying to
convert it to a control.

I think I am missing something simple.

TIA

Paul A. Timmerman
 
Dim CurrentTabPage As Control
CurrentTabPage = DirectCast("tcontrol.SelectedTab.Name",
Control)

Firstly, Name is a string; 'SelectedTab' is the tabpage you are looking for.
Secondly, you don't need the surrounding double quotes. Thirdly, you can
simply define CurrentTabPage of type 'TabPage' which means you wouldn't need
to cast in the first place since SelectedTab returns an object of type
'TabPage' itself. This is how you can get the reference you the current tab:

dim CurrentTabPage As TabPage = tcontrol.SelectedTab

and then MessageBox.Show(CurrentTabPage.Name)

hope that helps..
Imran.
 
Try:

CurrentTabPage = DirectCast (tcontrol.SelectedTab, Control)
OR
Dim tabName As String = DirectCast (tcontrol.SelectedTab.Name, String)

Hope this helps.

Dim CurrentTabPage As Control
CurrentTabPage = DirectCast("tcontrol.SelectedTab.Name",
Control)
MessageBox.Show(CurrentTabPage.Name)


I get a SPECIFIED CAT IS NOT VALID


Simply described:

I want to determine the name of the tabpage that I am on
(it is created dynamically) and then be able to reference
the tabpages properties. That is why I am trying to
convert it to a control.

I think I am missing something simple.

TIA

Paul A. Timmerman
 
Paul,

Now you try to use a String as a Control, that does not work.

However when you want to do that in a click event of that tabpage than you
can use by instance this
\\\
Dim tbp As TabControl = DirectCast(sender, TabControl)
Select Case tbp.SelectedTab.Name

etc
///
Cor
 
* "Paul Timmerman said:
Dim CurrentTabPage As Control
CurrentTabPage = DirectCast("tcontrol.SelectedTab.Name",
Control)
MessageBox.Show(CurrentTabPage.Name)

You cannot cast a string to a type. The string is not a subtype of
'Control'.
I want to determine the name of the tabpage that I am on
(it is created dynamically) and then be able to reference
the tabpages properties. That is why I am trying to
convert it to a control.

Why don't you simply use 'Me.TabControl1.SelectedTab.Name'?
 
When you need to cast a String to type you will need to use Reflections.. I
don't have the syntax with me sorry

VJ
 

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

Back
Top