Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a user control named "test"
C#
public partial class test : UserControl
{
    string clean;
    public test()
    {
        InitializeComponent();
        this.Disposed += new EventHandler(test_Disposed);
    }

    void test_Disposed(object sender, EventArgs e)
    {
        if(clean!=textBox1.Text)
            MessageBox.Show("Do you want save form?");
    }

    private void test_Load(object sender, EventArgs e)
    {
        clean = textBox1.Text;
    }
}


and a parent form which call "test" user control
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void radButton1_Click(object sender, EventArgs e)
    {
        DocumentWindow DW_test = new DocumentWindow();
        DW_test.Text = "something";
        DW_test.Name = "test";
        DW_test.CloseAction = DockWindowCloseAction.CloseAndDispose;
        var Frm = new test();
        Frm.Dock = DockStyle.Fill;
        DW_test.Controls.Add(Frm);
        this.radDock1.AddDocument(DW_test);
        this.radDock1.ActiveWindow = DW_test;
        Frm.Show();
    }
}

I want when Test usercontrol is closing,the new value of textbox be compared with old valu of that. but in disposing event always is textbox.tex==""
but I didnt change value of textbox manually. whats the problem?
Posted
Updated 27-Nov-14 21:23pm
v4

You should use the user control parent Closing event to manage your job and not to use Dispose, because when Dispose will be invoked will be to late.

So in you test user control Load event you should do like in the example bellow:
C#
private void test_Load(object sender, EventArgs e)
{
clean = textBox1.Text;
((Form)this.Parent).Closing += new CancelEventHandler(ParentClosing);
}

private void ParentClosing(object sender, CancelEventArgs e)
{
if(clean!=textBox1.Text)
      MessageBox.Show("Do you want save form?");
//...
}
 
Share this answer
 
Comments
Member 11133908 28-Nov-14 2:53am    
sorry I have an error on this line:
((Form)this.Parent).Closing += new CancelEventHandler(ParentClosing);
error:
"Unable to cast object of type 'Telerik.WinControls.UI.Docking.DocumentWindow' to type 'System.Windows.Forms.Form'."
Raul Iloc 28-Nov-14 6:37am    
1.The user control does not have a closing event so the idea is to use the parent form closing event.
2.So in your case you should use the parent of the parent, becuase your user control is hosted by another control so you should change the code:
((Form)((Telerik.WinControls.UI.Docking.DocumentWindow)this.Parent).Parent).Closing += new CancelEventHandler(ParentClosing);
I'm aware you are using Telerik Controls here, and I am not sure if that introduces anything "unique" into this situation: the code shown here is what I would write in Windows Forms.

Your code for 'Disposed is executing after the WindowDocument is closed and its 'Dispose handler has executed: so, the contents of any Controls within it, and the Controls within Controls, etc., like a TextBox in a UserControl, are ... gone.

I strongly suggest you handle the WindowDocument's 'Closing event, and have a simple public variable (or Property) in your UserControl that will be accessed by its Parent container when the Parent closes:
C#
// in the UserControl
// get rid of the 'Dispose stuff
public bool IsDirty()
{
    return textBox1.Text != clean;
}
And, in the Form/DocumentWindow:
C#
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (test1.IsDirty)
    {
        if (ShowDialog("Do you want save form?") == DialogResult.Yes)
        {
            // save whatever ?
            // your code to save
        }
        else
        {
            // cancel the Form Closing ?
            e.Cancel = true;
        }
    }
}
I find your use of 'test for both the name of the UserControl, and in the Form code as a variable name for an instance of 'DocumentWindow is very confusing, and I predict this will be a source of confusion for anyone trying to use/read your code ... including yourself when you look at your code in the future.

It doesn't cost you anything to use descriptive unique names, and it's an important skill/habit to develop.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900