[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1676
  • Last Modified:

How to access the tab header name?

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
ANINDYA
Asked:
ANINDYA
  • 7
  • 4
  • 4
  • +1
8 Solutions
 
abdkhlaifCommented:
The TabControl stores the tabs in the Controls collection:
int i = tabControl1.SelectedIndex;
MessageBox.Show(tabControl1.Controls[i].Name);

Open in new window

0
 
ANINDYAAuthor Commented:
Expert AbdKhalif
Thanks for the reply.
Please see there is an error is coming .
Thanking you
Anindya
work.JPG
0
 
abdkhlaifCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ANINDYAAuthor Commented:
Expert AbdKhalif
name option is not associated with the Items .
As you can see in the attached code.
Thanking you,
work.JPG
0
 
abdkhlaifCommented:
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
 
Alpesh PatelAssistant ConsultantCommented:
Hi,

Get the SelectedTab in TabItem object and get text using TabItem.Text
0
 
ANINDYAAuthor Commented:
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
 
abdkhlaifCommented:
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
 
ANINDYAAuthor Commented:
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
 
ANINDYAAuthor Commented:
Expert Patel alphesh
With the TabItem object SelectedTab option is not coming.
Thanking you
Anindya
0
 
Shahan AyyubSenior Software Engineer - iOSCommented:
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
 
ANINDYAAuthor Commented:
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
 
Shahan AyyubSenior Software Engineer - iOSCommented:
What did you get ?? when used this code???
0
 
Shahan AyyubSenior Software Engineer - iOSCommented:
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
 
Shahan AyyubSenior Software Engineer - iOSCommented:
Hi! Anindya

Did you checked that ??
0
 
ANINDYAAuthor Commented:
thanks
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 7
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now