在关闭usercontrol之前提示保存值的问题 [英] Problem with prompting save values before usercontrol be closed
问题描述
我有一个名为test的用户控件
I have a user control named "test"
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
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();
}
}
我想在Test usercontrol关闭时,将textbox的新值与旧值进行比较。但在处理事件时总是textbox.tex ==
但我没有手动更改文本框的值。问题是什么?
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?
推荐答案
你应该使用用户控制父关闭
事件来管理你的工作,而不是使用Dispose
,因为当Dispose
将被调用时将会延迟。
所以在你的测试
用户控件加载
事件,你应该在下面的例子中做:
You should use the user control parentClosing
event to manage your job and not to useDispose
, because whenDispose
will be invoked will be to late.
So in youtest
user controlLoad
event you should do like in the example bellow:
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?");
//...
}
我知道你在这里使用Telerik Controls,我不确定是否在这种情况下引入了独特:这里显示的代码是我在Windows Forms中编写的代码。
关闭'Disposed正在执行之后 WindowDocument关闭并且'Dispose处理程序已执行:所以,任何控件的内容在其中,控件内的控件等,就像UserControl中的TextBox一样......已经消失了。
我强烈建议你处理WindowDocument的'关闭在您的UserControl中有一个简单的公共变量(或属性),当父关闭时,它的父容器将访问它:
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:
// in the UserControl
// get rid of the 'Dispose stuff
public bool IsDirty()
{
return textBox1.Text != clean;
}
并且,在Form / DocumentWindow中:
And, in the Form/DocumentWindow:
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;
}
}
}
我发现您使用'测试用户控件的名称,并在表单代码中使用对于'DocumentWindow实例的变量名非常混乱,我预测这会让任何试图使用/读取代码的人感到困惑......包括你在看代码时的自己在将来。
它不会花费任何使用描述性唯一名称的东西,它是一个发展的重要技能/习惯。
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.
这篇关于在关闭usercontrol之前提示保存值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!