如何将可序列化设置为Window窗体 [英] How to set serializable to a Window form

查看:105
本文介绍了如何将可序列化设置为Window窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写Windows应用程序.

我创建一个表单,并在此表单上分配一些控件.
此应用程序将允许用户单击表单上的关闭"按钮以关闭此表单,还允许用户单击一个专用选项来重新打开此表单.

现在的条件和问题是:
在关闭此表单之前,我已经保存了表单对象.
关闭此表单后,它表明该表单对象已被放置,因此我无法再次显示此表单对象.
我想应该将表单对象保存到具有可序列化"功能的类中,并通过文件对该类进行序列化/反序列化,但是编译器显示该表单也需要设置为"Serializabe" .
所以,我的问题是:
1.如何设置表格的可序列化?
2.有人可以逐步展示完整的概念吗?

谢谢.

I am writing a Windows application.

I create a form, and assign some controls on this form.
This app will allow user to click ''close'' button on form to close this form and also allows users to click one dedicated option to re-open this form.

The condition and question now are that :
I''ve saved the form object before closing this form.
After closing this form, it shows that the form object is disposed, so I can not show this form object again.
I suppose that it should save the form object to a class with ''Serializable'' feature, and do serialize/deserialize for this class through a file but, the compiler shows that the Form also need to be set as ''Serializabe''.
So, my questions are :
1. How to set serializable for a form?
2. Can somebody show the full concept step by step?

Thanks.

推荐答案

隐藏表单而不是真正关闭表单会更容易吗?这将使您可以再次显示所有数据.

如果要避免在用户按下关闭按钮"x"时关闭窗体,则需要处理"FormClosing"事件:

Wouldn''t it be easier to just hide the form instead of really closing it? This would allow you to show it again with all the data on it.

If you want to avoid that the form closes when the user presses the close button "x", then you need to handle the "FormClosing" event:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
  if (!m_bClose)
  {
    //only hide the form without closing
    this.Hide();
    e.Cancel = true;
  }
  //else: the form is closed
}



这使您可以决定何时关闭表单或仅隐藏表单.



This allows you to decide when you want to close the form or just hide it.


这篇关于如何将可序列化设置为Window窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆