Tabcontrol.tag

textBox1.Text = TabControl1.SelectedTab.Tag;

That code doesnt work for some reason, could someone please tell what the working code is




Answer this question

Tabcontrol.tag

  • Craig J

    You can't put a object type in a string typed property. First cast or convert it:


    textBox1.Text = (String)TabControl1.SelectedTab.Tag;

    // Or
    textBox1.Text = TabControl1.SelectedTab.Tag as String;

    // Or
    textBox1.Text = TabControl1.SelectedTab.Tag.ToString();



  • bpeikes

    .text and .tag are 2 completely different things

    the .text is the name of the tabpage, the .tag is somewhere to store information,

    Im using the .tag to store a line of information which i would like 2 access during runtime, is this not possible



  • Belzebuth

    Sorry, I misunderstood what you were doing -  you didn't give many details.

    You certainly should be able to use the Tag as you say. What exactly is going wrong Is the .Tag field null when it shouldn't be How do you assign to the .Tag field

    Or are you getting a compile error You need to cast .Tag to string, since .Tag is an object.

  • Philip Coyner

    Then try this

    textBox1.Text = TabControl1.SelectedTab.Tag as string;

    textBox1.Text = (string)TabControl1.SelectedTab.Tag;


  • FredLarson

    ".Tag" is a field that all controls have, into which you can place a reference to any object - it isn't the text for a tab.

    What you want is SelectedTab.Text, I think.

  • Tabcontrol.tag