防止Designer.cs自动代码更改特定行 [英] Prevent Auto code change in Designer.cs for a specific line
问题描述
我取得了Designer.cs一个简单的变化是
I made a simple change in Designer.cs which is
this.dateTimePicker.MaxDate = DateTime.Now;
但每当我做造型设计有所改变 DateTime.Now
由当前日期时间值代替。如何防止这种自动代码更改?
but whenever i do some change in form design DateTime.Now
is replaced by current DateTime value. How to prevent this automatic code change?
推荐答案
您不应该在InitializeComponent方法更改代码,因为它是自动生成的,并会当你在设计的变化进行更换。该方法总结说,这一切:
You should not change code in the InitializeComponent method as it is auto-generated and will be replaced when you make changes in the designer. The method summary says it all:
不要使用代码编辑器
将您的代码在你的代码隐藏文件的构造函数,而不是(后InitializeComponent调用)。这将设置的值可以覆盖已经在InitializeComponent方法中指定的任何值:
Place your code in the constructor of your code-behind file instead (after the call to InitializeComponent). This will set the value and override any value that may have been specified in the InitializeComponent method:
using System;
using System.Windows.Forms;
namespace Bling
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dateTimePicker.MaxDate = DateTime.Now;
}
}
}
这篇关于防止Designer.cs自动代码更改特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!