Solved

How to access the tab header name?

Posted on 2010-11-09
18
1,605 Views
Last Modified: 2013-12-17
Experts
I want to get the name of the header name of the tab while clicking the tab .
The code by which I am creating the new tab is as followes:
private void mnuSave_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            try
            {
                this.txtTabName.Text = string.Empty;
                winNewTabname.Header = "Set Name of Tab";
                winNewTabname.Top = this._mousePosition.Y;
                winNewTabname.Left = this._mousePosition.X - 200;
                winNewTabname.WindowStartupLocation = WindowStartupLocation.Manual;
                winNewTabname.ResizeMode = ResizeMode.NoResize;
                winNewTabname.Closed += new EventHandler<WindowClosedEventArgs>(winNewTabname_Closed);
                winNewTabname.ShowDialog();
            }
            catch (Exception ex)
            {
                Globals.HandleUIExceptions(ex);
            }
        }
        private void winNewTabname_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true && !string.IsNullOrEmpty(this.txtTabName.Text.Trim()))
            {
                var found = from Object obj in tabNotes.Items
                            where (obj as RadTabItem).Header.ToString() == this.txtTabName.Text.Trim()
                            select obj;

                if (found.ToList().Count == 0)
                {
                    RadTabItem tabItem = new RadTabItem();
                    tabItem.Tag = this.txtTabName.Text;
                    tabItem.Header = this.txtTabName.Text;
                    tabNotes.Items.Add(tabItem);
                    this.tabNotes.SelectedIndex = tabNotes.Items.Count - 1;
                    RadContextMenu.SetContextMenu(tabItem, _ContextMenu);
                }
                else
                {

                    this.tabNotes.SelectedItem = found.ToList()[0] as RadTabItem;
                }
            }
        }

Now please suggest me how to get the tag name
 1
private void mnuSave_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)

        {

            try

            {

                this.txtTabName.Text = string.Empty;

                winNewTabname.Header = "Set Name of Tab";

                winNewTabname.Top = this._mousePosition.Y;

                winNewTabname.Left = this._mousePosition.X - 200;

                winNewTabname.WindowStartupLocation = WindowStartupLocation.Manual;

                winNewTabname.ResizeMode = ResizeMode.NoResize;

                winNewTabname.Closed += new EventHandler<WindowClosedEventArgs>(winNewTabname_Closed);

                winNewTabname.ShowDialog();

            }

            catch (Exception ex)

            {

                Globals.HandleUIExceptions(ex);

            }

        }

        private void winNewTabname_Closed(object sender, WindowClosedEventArgs e)

        {

            if (e.DialogResult == true && !string.IsNullOrEmpty(this.txtTabName.Text.Trim()))

            {

                var found = from Object obj in tabNotes.Items

                            where (obj as RadTabItem).Header.ToString() == this.txtTabName.Text.Trim()

                            select obj;



                if (found.ToList().Count == 0)

                {

                    RadTabItem tabItem = new RadTabItem();

                    tabItem.Tag = this.txtTabName.Text;

                    tabItem.Header = this.txtTabName.Text;

                    tabNotes.Items.Add(tabItem);

                    this.tabNotes.SelectedIndex = tabNotes.Items.Count - 1;

                    RadContextMenu.SetContextMenu(tabItem, _ContextMenu);

                }

                else

                {



                    this.tabNotes.SelectedItem = found.ToList()[0] as RadTabItem;

                }

            }

        }

        private void winRenameTabname_Closed(object sender, WindowClosedEventArgs e)

        {

         }





//here is the full code 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using Telerik.Windows.Controls;

using UBS.Rx.Presentation.Common;



namespace UBS.Rx.Presentation.UI.Moola

{

    public partial class TabbedNotesControl : UserControl

    {

        Notes _Notes;

        double _Height, _Width;

        int _Selectedindex = -1;

        Point _mousePosition = new Point(0, 0);

        RadContextMenu _ContextMenu;



        public TabbedNotesControl()

        {

            InitializeComponent();

            _Notes = new Notes();

            StyleManager.SetTheme(this.tabNotes, ThemeManager.FromName(Globals.MoolaThemeName));

            this.tabNotes.SelectionChanged += new RoutedEventHandler(tabNotes_SelectionChanged);

            mnuSave.Click += new Telerik.Windows.RadRoutedEventHandler(mnuSave_Click);

            this.txtTabName.KeyDown += new KeyEventHandler(txtTabName_KeyDown);

            this.MouseMove += new MouseEventHandler(TabbedNotesControl_MouseMove);

            _ContextMenu = new RadContextMenu();

            RadMenuItem mnuTabItemAdd = new RadMenuItem() { Name = "mnuTabItemAdd", Header = "Add a new tab" };

            _ContextMenu.Items.Add(mnuTabItemAdd);

            mnuTabItemAdd.Click += new Telerik.Windows.RadRoutedEventHandler(mnuSave_Click);





            RadMenuItem mnuTabItemRemove = new RadMenuItem() { Name = "mnuTabItemRemove", Header = "Remove tab" };

            _ContextMenu.Items.Add(mnuTabItemRemove);

            mnuTabItemRemove.Click += new Telerik.Windows.RadRoutedEventHandler(mnuTabItemRemove_Click);



            RadMenuItem mnuTabItemRename = new RadMenuItem() { Name = "mnuTabItemRename", Header = "Rename tab" };

            _ContextMenu.Items.Add(mnuTabItemRename);

            mnuTabItemRename.Click += new Telerik.Windows.RadRoutedEventHandler(mnuTabItemRename_Click);

        }



        private void mnuTabItemRename_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)

        {

            //Rename

            try

            {

                this.txtTabName.Text = string.Empty;

                winNewTabname.Header = "Rename Tab";

                winNewTabname.Top = this._mousePosition.Y;

                winNewTabname.Left = this._mousePosition.X - 200;

                winNewTabname.WindowStartupLocation = WindowStartupLocation.Manual;

                winNewTabname.ResizeMode = ResizeMode.NoResize;

                winNewTabname.Closed += new EventHandler<WindowClosedEventArgs>(winRenameTabname_Closed);

                winNewTabname.ShowDialog();

            }

            catch (Exception ex)

            {

                Globals.HandleUIExceptions(ex);

            }

        }



        private void mnuTabItemRemove_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)

        {

           //Remove

        }



        private void TabbedNotesControl_MouseMove(object sender, MouseEventArgs e)

        {

            try

            {

                _mousePosition = e.GetPosition(null);

            }

            catch

            {



            }

        }



        void txtTabName_KeyDown(object sender, KeyEventArgs e)

        {

            if (e.Key == Key.Enter)

            {

                this.winNewTabname.DialogResult = true;

                this.winNewTabname.Close();



            }

            else if (e.Key == Key.Escape)

            {

                this.winNewTabname.DialogResult = false;

                this.winNewTabname.Close();

            }

        }



        private void mnuSave_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)

        {

            try

            {

                this.txtTabName.Text = string.Empty;

                winNewTabname.Header = "Set Name of Tab";

                winNewTabname.Top = this._mousePosition.Y;

                winNewTabname.Left = this._mousePosition.X - 200;

                winNewTabname.WindowStartupLocation = WindowStartupLocation.Manual;

                winNewTabname.ResizeMode = ResizeMode.NoResize;

                winNewTabname.Closed += new EventHandler<WindowClosedEventArgs>(winNewTabname_Closed);

                winNewTabname.ShowDialog();

            }

            catch (Exception ex)

            {

                Globals.HandleUIExceptions(ex);

            }

        }

        private void winNewTabname_Closed(object sender, WindowClosedEventArgs e)

        {

            if (e.DialogResult == true && !string.IsNullOrEmpty(this.txtTabName.Text.Trim()))

            {

                var found = from Object obj in tabNotes.Items

                            where (obj as RadTabItem).Header.ToString() == this.txtTabName.Text.Trim()

                            select obj;



                if (found.ToList().Count == 0)

                {

                    RadTabItem tabItem = new RadTabItem();

                    tabItem.Tag = this.txtTabName.Text;

                    tabItem.Header = this.txtTabName.Text;

                    tabNotes.Items.Add(tabItem);

                    this.tabNotes.SelectedIndex = tabNotes.Items.Count - 1;

                    RadContextMenu.SetContextMenu(tabItem, _ContextMenu);

                }

                else

                {



                    this.tabNotes.SelectedItem = found.ToList()[0] as RadTabItem;

                }

            }

        }

        private void winRenameTabname_Closed(object sender, WindowClosedEventArgs e)

        {

            



            ////if (e.DialogResult == true && !string.IsNullOrEmpty(this.txtTabName.Text.Trim()))

            ////{

            //    var found = from Object obj in tabNotes.Items

            //                where (obj as RadTabItem).Header.ToString() == this.txtTabName.Text.Trim()

            //                select obj;

            //    string oldtabname = found.ToString();

            //    a = this.txtTabName.Text;

            //    header

            //    (obj as RadTabItem).Header.ToString() = oldtabname;

            //    if (found.ToList().Count == 0)

            //    {

            //        RadTabItem tabItem = new RadTabItem();

            //        tabItem.Tag = this.txtTabName.Text;

            //        tabItem.Header = this.txtTabName.Text;

            //        tabNotes.Items.Add(tabItem);

            //        this.tabNotes.SelectedIndex = tabNotes.Items.Count - 1;

            //        RadContextMenu.SetContextMenu(tabItem, _ContextMenu);

            //    }

            //    else

            //    {



            //        this.tabNotes.SelectedItem = found.ToList()[0] as RadTabItem;

            //    }

            ////}

        }

        void tabNotes_SelectionChanged(object sender, RoutedEventArgs e)

        {



            if (_Selectedindex > -1)

            {

                (tabNotes.Items[_Selectedindex] as RadTabItem).Content = null;

            }

            RadTabItem tabItem = this.tabNotes.SelectedItem as RadTabItem;

            _Notes.setDimestion(_Height, _Width);

            tabItem.Content = _Notes;

            _Notes.TabName = Convert.ToString(tabItem.Header);

            _Notes.LoadNotes(Convert.ToString(tabItem.Header));

            _Selectedindex = this.tabNotes.SelectedIndex;

        }



        public TabbedNotesControl(int noOfTabs)

            : this() //Here also we can pass type for the commensts also.

        {



            RadTabItem tabItem = new RadTabItem();

            tabItem.Tag = "Comments";

            tabItem.Header = "Comments";

            tabNotes.Items.Add(tabItem);

            RadContextMenu.SetContextMenu(tabItem, _ContextMenu);

        }



        public void setDimestion(double height, double width)

        {

            _Height = this.LayoutRoot.MaxHeight = this.LayoutRoot.Height = height;

            _Width = this.LayoutRoot.MaxWidth = this.LayoutRoot.Width = width;

            this.tabNotes.SelectedIndex = 0;

        }

    }

}

Open in new window

0
Comment
Question by:ANINDYA
  • 7
  • 4
  • 4
  • +1
18 Comments
 
LVL 3

Accepted Solution

by:
abdkhlaif earned 251 total points
Comment Utility
The TabControl stores the tabs in the Controls collection:
int i = tabControl1.SelectedIndex;

MessageBox.Show(tabControl1.Controls[i].Name);

Open in new window

0
 

Author Comment

by:ANINDYA
Comment Utility
Expert AbdKhalif
Thanks for the reply.
Please see there is an error is coming .
Thanking you
Anindya
work.JPG
0
 
LVL 3

Assisted Solution

by:abdkhlaif
abdkhlaif earned 251 total points
Comment Utility
try Items instead of Controls
If I'm not mistaken, RadTabControl inherits from System.Windows.Controls.ItemsControl which has Items collection to hold the tabs.
int i = tabControl1.SelectedIndex;

MessageBox.Show(tabControl1.Items[i].Name);

Open in new window

0
 

Author Comment

by:ANINDYA
Comment Utility
Expert AbdKhalif
name option is not associated with the Items .
As you can see in the attached code.
Thanking you,
work.JPG
0
 
LVL 3

Assisted Solution

by:abdkhlaif
abdkhlaif earned 251 total points
Comment Utility
I just realized that RadTabControl is not a .NET control
refer to the product documentation at
http://www.telerik.com/help/wpf/telerik.windows.controls.navigation-telerik.windows.controls.radtabcontrol_members.html

one of the control members should let you get the name of the selected tab.
try SelectedItem property:
MessageBox.Show((string)tabControl1.SelectedItem); 

Open in new window

0
 
LVL 21

Assisted Solution

by:Alpesh Patel
Alpesh Patel earned 63 total points
Comment Utility
Hi,

Get the SelectedTab in TabItem object and get text using TabItem.Text
0
 

Author Comment

by:ANINDYA
Comment Utility
Expert AbdKhalif
in all cases it is taking a null value as I am just instantiating the tabcontrol and after that using your messagebox statement .
So it is not catching the value of the current tab which is clicked .
I have tried to use the this key word but failed .
can you please help me .
Thanking you,
Anindya
work.JPG
0
 
LVL 3

Assisted Solution

by:abdkhlaif
abdkhlaif earned 251 total points
Comment Utility
The tab control is already created & initialized in the InitializeComponent() method, you shouldn't create another tab control inside winRenameTabname_Closed()
so you need to replace "tabControl1" with the correct object name
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:ANINDYA
Comment Utility
Expert AbdKhalif
The object which is declared earlier is not accessible in the winRenameTabname_Closed()
You can see that in the intellisense.
Thanking you
work.JPG
0
 

Author Comment

by:ANINDYA
Comment Utility
Expert Patel alphesh
With the TabItem object SelectedTab option is not coming.
Thanking you
Anindya
0
 
LVL 19

Assisted Solution

by:Shahan Ayyub
Shahan Ayyub earned 186 total points
Comment Utility
Hi!

Its not a simple TabControl its custom Control so I can not tell you directly as my internet connection is very slow to download control and test it.

Check this: (Code for rename menu)

RadTabItem item = this.tabNotes.SelectedItem as RadTabItem;
String NameOfTab = item.Header;


Is this helpful ??
0
 

Author Comment

by:ANINDYA
Comment Utility
Expert ShahanDeveloper
this is not helping.
Can you please provide some more information regarding this or any pages from where I can get some more information regarding this.
Thanking you,
anindya
0
 
LVL 19

Assisted Solution

by:Shahan Ayyub
Shahan Ayyub earned 186 total points
Comment Utility
What did you get ?? when used this code???
0
 
LVL 19

Assisted Solution

by:Shahan Ayyub
Shahan Ayyub earned 186 total points
Comment Utility
Did you check this as well:

RadTabItem item = this.tabNotes.SelectedItem as RadTabItem;
object NameOfTab = item.Header;


You need to check this properties as well from the below link:
http://www.telerik.com/help/wpf/telerik.windows.controls.navigation-telerik.windows.controls.radtabcontrol_properties.html

Items property its a collection and find the current selected tab and search for Header property.
Header property


In this piece of code:

Dim index As Integer
Dim value As RadTabItem
value = tabNotes.Item(index)

Find Header Property under "value".
0
 
LVL 19

Expert Comment

by:Shahan Ayyub
Comment Utility
Hi! Anindya

Did you checked that ??
0
 

Author Closing Comment

by:ANINDYA
Comment Utility
thanks
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now