在.Net中,请问使用C#能否序列化一个标准控件? ( 积分: 50 )

  • 主题发起人 mycwcgr_new
  • 开始时间
M

mycwcgr_new

Unregistered / Unconfirmed
GUEST, unregistred user!
在.Net中,请问使用C#能否序列化一个标准控件?
下面是一个Windows Form窗口,有一个控件textBox1,我想在运行时可以序列化这个控件,以保存它的有关属性如Text,
然后在需要时,反序列化以恢复保存的状态,请问能不能做到?如能,请给出一个具体的示例!

这样做的目的是可以方便的保持控件的状态,如大小、位置等,而不用将其写入XML或INI文件中!



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace sample
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(80, 64);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
 
在.Net中,请问使用C#能否序列化一个标准控件?
下面是一个Windows Form窗口,有一个控件textBox1,我想在运行时可以序列化这个控件,以保存它的有关属性如Text,
然后在需要时,反序列化以恢复保存的状态,请问能不能做到?如能,请给出一个具体的示例!

这样做的目的是可以方便的保持控件的状态,如大小、位置等,而不用将其写入XML或INI文件中!



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace sample
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(80, 64);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
 
WinForms控件中有Windows 窗口句炳,无法支持.net标准的序列化。
建议1:
如果只是记录和恢复控件的布局(位置、大小),我觉得在Form上实现就可以了。保存和恢复每个子控件的位置和大小就可以了。
建议2:
如果要记录和恢复更复杂的属性,比如每个控件用户输入的值(TextBox.Text, CheckBox.Checked...)。有两种方案:
1. 从每种控件下,派生新的类。在派生类中加上保存和恢复属性的代码。我写过一个Form设计器,允许用户将自己设计的数据库结构中的每个字段绑定到控件上,并在涉及其中修改控件的属性。就是通过类似这种方法做的。
由于.net不支持多重继承,这种方案会非常繁琐。我在实现时,没有严格按照这个方法,而是将继承关系倒置。在基类中加入保存和恢复属性的代码,在派生类中创建对应的控件。
2. 给每种控件写一个Adapter类,每个Adapter类和一个控件对应,在Adapter类中加上保存和恢复属性的代码。
对了,在vs.net Form设计器里,可以将控件的属性保存在配置文件里。这样通过修改配置文件,也能修改部分属性。我没有用过,不知道行不行。我觉得就算行,也太繁琐了。
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
621
import
I
I
回复
0
查看
736
import
I
顶部