Close TabStrip Tab item by pressing the middle mouse button

7 posts, 2 answers
  1. Themos
    Themos avatar
    18 posts
    Member since:
    Jul 2011

    Posted 23 Jul 2012 Link to this post

    Hello,

    I have a number of tabs within a radpageview control in tabstrip mode, and I would like to allow the user to close them by pressing the middle mouse button (like a regular web browser).

    This is what I've done so far:

    //add event handler
    newPage.Item.MouseDown += new MouseEventHandler(radButtonItem_MouseDown);

    and the function:

    private void radButtonItem_MouseDown(object sender, MouseEventArgs e)
     {
         //button was clicked by the mouse
         if (e.Button == MouseButtons.Middle)
         {
             MessageBox.Show("Middle button"); 
      
             rpvApplication.Pages.Remove(((RadPageViewStripItem)sender).Page); // crashes!
             //rpvApplication.Pages.RemoveAt(3); // crashes!
         }
     }

    I've tried calling the Pages.Remove function using the sender object as well as by the tab's index, but the result is always the same:
    "nullreference exception" right after the page is removed.

    Is there another way I can execute the 'close tab' functionality (just like clicking the 'x' button), without getting a null reference exception?

    Thanks in advance!
  2. Answer
    Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 26 Jul 2012 Link to this post

    Hi Themos,

    Thank you for writing.

    The desired functionality can be easily achieved by using the MouseDown event of RadPageView. Here is a sample:
    void radPageView1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Middle)
        {
            RadPageViewStripItem item = radPageView1.ElementTree.GetElementAtPoint(e.Location) as RadPageViewStripItem;
            if (item != null)
            {
                radPageView1.Pages.Remove(item.Page);
            }
        }
    }

    I hope this helps.
     
    Kind regards,
    Stefan
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Themos
    Themos avatar
    18 posts
    Member since:
    Jul 2011

    Posted 26 Jul 2012 Link to this post

    Thanks for your reply, this was just what I needed!

    The problem was that I was setting the event handler on the tabitem itself, instead of the radpageview!
    I simply modified your code to prevent the user from closing some specific tabs, by using the following code:

                    RadPageViewStripItem item = rpvApplication.ElementTree.GetElementAtPoint(e.Location) as RadPageViewStripItem;
                    if (item != null && item.ButtonsPanel.CloseButton.Visibility != Telerik.WinControls.ElementVisibility.Collapsed)
                    {
                        rpvApplication.Pages.Remove(item.Page);
                    }

    On a similar note, is there a way to override the default behavior of the Radpageview strip buttons, and specifically the Close Button, so as to prevent the user from closing those tabs that have their closebutton visibility set to collapsed?

    Thanks again for your time!
  5. Themos
    Themos avatar
    18 posts
    Member since:
    Jul 2011

    Posted 30 Jul 2012 Link to this post

    Is there no way to prevent specific tabs from closing? i.e. disabling the radpageview's global close button when those pages are selected?
  6. Answer
    Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 30 Jul 2012 Link to this post

    Hi Themos,

    Thank you for writing.

    You can prevent a page from being removed by subscribing to the PageRemoving event of the control:
    void radPageView1_PageRemoving(object sender, RadPageViewCancelEventArgs e)
    {
        if (e.Page.Item.ButtonsPanel.CloseButton.Visibility == ElementVisibility.Collapsed)
        {
            e.Cancel = true;
        }
    }

    In order to disable the main close button upon some condition, you can use the SelectedPageChanged event:
    void radPageView1_SelectedPageChanged(object sender, EventArgs e)
    {
        if (radPageView1.SelectedPage.Item.ButtonsPanel.CloseButton.Visibility == ElementVisibility.Collapsed)
        {
            ((RadPageViewStripElement)radPageView1.ViewElement).ItemContainer.ButtonsPanel.CloseButton.Enabled = false;
            ((RadPageViewStripElement)radPageView1.ViewElement).ItemContainer.ButtonsPanel.CloseButton.UseDefaultDisabledPaint = true;
        }
        else
        {
            ((RadPageViewStripElement)radPageView1.ViewElement).ItemContainer.ButtonsPanel.CloseButton.Enabled = true;
        }
    }

    I hope this helps.
     
    Regards,
    Stefan
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  7. Themos
    Themos avatar
    18 posts
    Member since:
    Jul 2011

    Posted 31 Jul 2012 Link to this post

    The second part of the code didn't seem to make a difference, but the first one worked like a charm! Thanks a lot!

    PS: I now changed the second part of the code to hide the 'X' button instead of disabling it, and it works even better!
  8. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 02 Aug 2012 Link to this post

    Hello Themos,

    I am glad that I could help.  You are right, enabling does not work out of the box and collapsing would be easier in this case. However here is the whole sample with disabling if someone is interested:
        public Form1()
        {
            InitializeComponent();
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
     
            radPageView1.PageRemoving += new EventHandler<Telerik.WinControls.UI.RadPageViewCancelEventArgs>(radPageView1_PageRemoving);
            radPageView1.SelectedPageChanged += new EventHandler(radPageView1_SelectedPageChanged);
     
            UpdateCloseButtonVisivility();
            ((RadPageViewStripElement)radPageView1.ViewElement).ShowItemCloseButton = true;
            radPageViewPage2.Item.ButtonsPanel.CloseButton.Visibility = ElementVisibility.Collapsed;
            radPageViewPage4.Item.ButtonsPanel.CloseButton.Visibility = ElementVisibility.Collapsed;
            ((RadPageViewStripElement)radPageView1.ViewElement).ItemContainer.ButtonsPanel.CloseButton.RadPropertyChanged += new RadPropertyChangedEventHandler(CloseButton_RadPropertyChanged);
        }
     
        void CloseButton_RadPropertyChanged(object sender, RadPropertyChangedEventArgs e)
        {
            if (e.Property.Name == "Enabled")
            {
                UpdateCloseButtonVisivility();
            }
        }
     
        void radPageView1_SelectedPageChanged(object sender, EventArgs e)
        {
            UpdateCloseButtonVisivility();
        }
     
        private void UpdateCloseButtonVisivility()
        {
            if (radPageView1.SelectedPage.Item.ButtonsPanel.CloseButton.Visibility == ElementVisibility.Collapsed)
            {
                ((RadPageViewStripElement)radPageView1.ViewElement).ItemContainer.ButtonsPanel.CloseButton.UseDefaultDisabledPaint = true;
                ((RadPageViewStripElement)radPageView1.ViewElement).ItemContainer.ButtonsPanel.CloseButton.Enabled = false;
            }
            else
            {
                ((RadPageViewStripElement)radPageView1.ViewElement).ItemContainer.ButtonsPanel.CloseButton.Enabled = true;
            }
        }
     
     
        void radPageView1_PageRemoving(object sender, Telerik.WinControls.UI.RadPageViewCancelEventArgs e)
        {
            if (e.Page.Item.ButtonsPanel.CloseButton.Visibility == ElementVisibility.Collapsed)
            {
                   e.Cancel = true;
            }
        }
    }


    Regards,
    Stefan
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready