读书人

为什么新建立项目有两个CS文件有什么

发布时间: 2012-03-01 10:25:47 作者: rapoo

为什么新建立项目有两个CS文件,有什么作用的?
新建立一个window应用程序项目,上面有
Form1.cs 和 Form1.Designer.cs
Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace _7_04
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
---------------------------
Form1.Designer.cs:
namespace _7_04
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name= "disposing "> 如果应释放托管资源,为 true;否则为 false。 </param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;


this.Text = "Form1 ";
}

#endregion
}
}

---------------------------
这两个文件有部份的代码是一至的,他们的关系是什么呢?




[解决办法]
看名字就知道 Form1.Designer.cs 是 Form1.cs 的设计器代码文件...它是IDE自动生成的...你在IDE中拖放控件、设置控件属性时所需要的代码都在这里面...

这是.NET 2.0以后才有的...以前的版本不支持 partial class...所以之前都放在一个文件里...

读书人网 >C#

热点推荐