adding close button on Ribbon tab

7 posts, 1 answers
  1. Sason
    Sason avatar
    32 posts
    Member since:
    Aug 2012

    Posted 11 Oct 2010 Link to this post

    Hi

    can i add a close button on the ribbon tab ?
    like the image ?

    thank u

  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 11 Oct 2010 Link to this post

    Hi, 

    As far as I know it's not possible to add a close button in the place you want it. 
    You could though add a close button (as a RadButtonElement) to the group in the RibbonTab and then close the tab programatically on the click of that button 
    http://www.telerik.com/help/winforms/ribbonbarprogrammingtabsandchunks.html

    Hope that helps
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 11 Oct 2010 Link to this post

    Hello Dror,

    You can inherit the RibbonTab base class and add some custom elements to the children collection,
    I have prepared a very small example, it should get you started:

    using System;
        using System.Drawing;
        using System.Windows.Forms;
        using Telerik.WinControls;
        using Telerik.WinControls.UI;
     
        public partial class RadRibbonForm1 : Telerik.WinControls.UI.RadRibbonForm
        {
            public RadRibbonForm1()
            {
                InitializeComponent();
            }
     
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
     
                var tabItem1 = new CustomRibbonTab("Manage");
                var tabItem2 = new CustomRibbonTab("Remove");
                radRibbonBar1.CommandTabs.AddRange(new CustomRibbonTab[] { tabItem1, tabItem2 });
            }
        }
     
        public class CustomRibbonTab : RibbonTab
        {
            private RadButtonElement radButtonElement;
     
            protected override Type ThemeEffectiveType
            {
                get
                {
                    return typeof(TabItem);
                }
            }
     
            private Timer focusTimer;
     
            public CustomRibbonTab()
                : base()
            {
                CreateTimer();
            }
     
            public CustomRibbonTab(string text)
                : base(text)
            {
                CreateTimer();
            }
     
            private void CreateTimer()
            {
                this.focusTimer = new Timer { Interval = 500 };
                this.focusTimer.Tick += delegate
                    {
                        this.radButtonElement.Visibility = ElementVisibility.Collapsed;
                    };
            }
     
            protected override void CreateChildElements()
            {
                base.CreateChildElements();
     
                radButtonElement = new RadButtonElement("x");
                radButtonElement.Size = new Size(10, 10);
                radButtonElement.MinSize = new Size(10, 10);
                radButtonElement.MaxSize = new Size(10, 10);
                radButtonElement.Click += new EventHandler(radButtonElement_Click);
                radButtonElement.MouseEnter += new EventHandler(radButtonElement_MouseEnter);
                radButtonElement.MouseLeave += new EventHandler(radButtonElement_MouseLeave);
                radButtonElement.BorderElement.Visibility = ElementVisibility.Collapsed;
                radButtonElement.Visibility = ElementVisibility.Collapsed;
                this.Children.Add(radButtonElement);
            }
     
            void radButtonElement_MouseLeave(object sender, EventArgs e)
            {
                this.OnMouseLeave(e);
            }
     
            void radButtonElement_MouseEnter(object sender, EventArgs e)
            {
                this.OnMouseEnter(e);
            }
     
            void radButtonElement_Click(object sender, EventArgs e)
            {
                ((Telerik.WinControls.UI.RadTabStripElement)(this.ParentTabStrip)).Items.Remove(this);
            }
     
            protected override void OnMouseEnter(EventArgs e)
            {
                base.OnMouseEnter(e);
                focusTimer.Stop();
                radButtonElement.Visibility = ElementVisibility.Visible;
            }
     
            protected override void OnMouseLeave(EventArgs e)
            {
                base.OnMouseLeave(e);
                focusTimer.Start();
            }
     
            protected override SizeF ArrangeOverride(SizeF finalSize)
            {
                finalSize = new SizeF(finalSize.Width + this.Children[3].DesiredSize.Width, finalSize.Height);
                base.ArrangeOverride(finalSize);
     
                this.Children[3].Arrange(new RectangleF(
    (int)(finalSize.Width - 1.7 * this.Children[3].Size.Width), (finalSize.Height / 2) - (this.Children[3].DesiredSize.Height / 2 + 1), finalSize.Width, this.Children[3].DesiredSize.Height));
     
                return finalSize;
            }
     
            protected override void DisposeManagedResources()
            {
                radButtonElement.Click -= new EventHandler(radButtonElement_Click);
                radButtonElement.MouseEnter -= new EventHandler(radButtonElement_MouseEnter);
                radButtonElement.MouseLeave -= new EventHandler(radButtonElement_MouseLeave);
     
                base.DisposeManagedResources();
            }
        }

    Please see the attached image if this is what you wanted.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  5. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 11 Oct 2010 Link to this post

    forgot to upload image and it won't allow me to update my post with image...
  6. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 11 Oct 2010 Link to this post

    Works well Emanuel. nice.
  7. Sason
    Sason avatar
    32 posts
    Member since:
    Aug 2012

    Posted 12 Oct 2010 Link to this post

    Great !!! 
    thank you.
  8. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 12 Oct 2010 Link to this post

    Hello Dror, you're very welcome, it was fun.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
Back to Top
UI for WinForms is Visual Studio 2017 Ready