DocumentCloseButton Event?

5 posts, 0 answers
  1. Eric
    Eric avatar
    32 posts
    Member since:
    Jun 2011

    Posted 16 Jan 2012 Link to this post

    I'm using the Q3 2009 Telerik controls (2009.3.9.1203) and have a scenario where I populate a RadDock initially and allow the user to add/remove additional DocumentWindows. When the user closes a DocumentWindow through the 'X' next to each tab (the button added to the title by setting ShowDocumentCloseButton to true), I'd like a prompt to come up to warn the user that they'd lose their data. However, the application at points removes and adds DocumentWindows to the same RadDock, and I don't want the prompt to appear in those scenarios.

    Right now I am hooked into the DockWindowClosing event. Through debugging I haven't found any information in the sender or event args that specify where the event was called from. In this case, when the program has to programmatically remove forms the user gets the prompt I created. I figure the best way to go about this is to ditch using the DockWindowClosing event, and try to hook into the Click event of that DocumentCloseButton.

    Is this at all possible? Is there another way to achieve what I'd like to do?
  2. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 19 Jan 2012 Link to this post

    Hi Eric,

    The DockWindowClosing event is the most suitable solution for your application. You can get the DockWindow and validate some data or show a message box to the user. Here is a simple example where MdiChildren Forms are added using AutoDetectMdiChildren and the close logic validates the hosted form object:

    void radDock1_DockWindowClosing(object sender, DockWindowCancelEventArgs e)
    {
        HostWindow hostWindow = e.DockWindow as HostWindow;
        if (hostWindow != null)
        {
            Form form = hostWindow.Content as Form;
            e.Cancel = form.Validate();
        }
    }

    I hope this helps.

    All the best,
    Julian Benkov
    the Telerik team

    SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Eric
    Eric avatar
    32 posts
    Member since:
    Jun 2011

    Posted 19 Jan 2012 Link to this post

    Unfortuantely I've gotten to this point. However, if the user decides to change the file we need a way to remove the documents from the DocumentManager without setting off any user prompts. We only want to prompt if the user clicks the close button. There will be other automated events (such as closing the application or changing the file which reloads the UI) where we don't want this prompt to appear.
  5. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 24 Jan 2012 Link to this post

    Hi Eric,

    In this case you can cancel the closing operation and manually handle the Click event. Please consider the sample below:

    using System;
    using System.Windows.Forms;
    using Telerik.WinControls.UI.Docking;
     
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
                RadDock radDock1 = new RadDock();
                radDock1.Dock = DockStyle.Fill;
                this.Controls.Add(radDock1);
     
                radDock1.ShowDocumentCloseButton = true;
                radDock1.DockWindowClosing += new DockWindowCancelEventHandler(radDock1_DockWindowClosing);
                ToolWindow toolWindow = new ToolWindow();
                radDock1.DockWindow(toolWindow, DockPosition.Right);
     
                DocumentWindow document = new DocumentWindow();
                document.DocumentButtons = DocumentStripButtons.None;
                radDock1.AddDocument(document);
                document.TabStripItem.CloseButton.Click += new EventHandler(CloseButton_Click);
     
                DocumentWindow document2 = new DocumentWindow();
                document2.DocumentButtons = DocumentStripButtons.None;
                radDock1.AddDocument(document2);
                document2.TabStripItem.CloseButton.Click += new EventHandler(CloseButton_Click);
     
                DocumentWindow document3 = new DocumentWindow();
                radDock1.AddDocument(document3);
                document3.TabStripItem.CloseButton.Click += new EventHandler(CloseButton_Click);
            }
     
            void radDock1_DockWindowClosing(object sender, DockWindowCancelEventArgs e)
            {
                if (e.DockWindow is HostWindow || e.DockWindow is DocumentWindow)
                {
                    e.Cancel = true;
                }
            }
     
            void CloseButton_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Close Click");
            }
        }
    }

    I hope this helps. 

    Regards,
    Julian Benkov
    the Telerik team

    SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

  6. Eric
    Eric avatar
    32 posts
    Member since:
    Jun 2011

    Posted 24 Jan 2012 Link to this post

    Thanks, this did the trick.
Back to Top
UI for WinForms is Visual Studio 2017 Ready