.net的优点变缺点(郁闷了一天)
今天在改写‘试题编辑器’时,
发现我在InitializeComponent()方法中写的代码消失了
(我在InitializeComponent中进行了PanelEdit = new PanelClass)
而我的PanelClass为自定义的panel.
编译的时候出现了PanelEdit 未对象事例的错误。
一看InitializeComponent中自己写的代码已经消失了
尴尬的花了一个小时才找到原因:.net自作聪明的根据form元素帮你重新生成代码,把我写的代码删除掉了。
解决方案:(1)适合于:(webForm)。定义为一个方法,在OnInit里进行调用
(2 )适合于(WinForm):定义为一个方法,构造函数里进行调用。且在InitializeComponent()方法前进行调用。
eg:
private void getPanleEidt()
{
this.PanelEdit = new WordInDOTNET.PanelClass();
// PanelEdit
this.PanelEdit.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.PanelEdit.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.PanelEdit.diffNumber = 0;
this.PanelEdit.Disp = null;
this.PanelEdit.Location = new System.Drawing.Point(136, 80);
this.PanelEdit.Name = "PanelEdit";
this.PanelEdit.Size = new System.Drawing.Size(712, 240);
this.PanelEdit.TabIndex = 26;
this.PanelEdit.testType = null;
this.Controls.Add(this.PanelEdit);
}
//构造函数
public frmMain()
{
//
// Required for Windows Form Designer support
//
//初始PanleEidt
this.getPanleEidt();
InitializeComponent();
}
发现我在InitializeComponent()方法中写的代码消失了
(我在InitializeComponent中进行了PanelEdit = new PanelClass)
而我的PanelClass为自定义的panel.
编译的时候出现了PanelEdit 未对象事例的错误。
一看InitializeComponent中自己写的代码已经消失了
尴尬的花了一个小时才找到原因:.net自作聪明的根据form元素帮你重新生成代码,把我写的代码删除掉了。
解决方案:(1)适合于:(webForm)。定义为一个方法,在OnInit里进行调用
(2 )适合于(WinForm):定义为一个方法,构造函数里进行调用。且在InitializeComponent()方法前进行调用。
eg:
private void getPanleEidt()
{
this.PanelEdit = new WordInDOTNET.PanelClass();
// PanelEdit
this.PanelEdit.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.PanelEdit.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.PanelEdit.diffNumber = 0;
this.PanelEdit.Disp = null;
this.PanelEdit.Location = new System.Drawing.Point(136, 80);
this.PanelEdit.Name = "PanelEdit";
this.PanelEdit.Size = new System.Drawing.Size(712, 240);
this.PanelEdit.TabIndex = 26;
this.PanelEdit.testType = null;
this.Controls.Add(this.PanelEdit);
}
//构造函数
public frmMain()
{
//
// Required for Windows Form Designer support
//
//初始PanleEidt
this.getPanleEidt();
InitializeComponent();
}