1

I made a simple change in Designer.cs which is

this.dateTimePicker.MaxDate = 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?

SMUsamaShah
  • 7,677
  • 22
  • 88
  • 131

1 Answers1

8

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:

do not modify the contents of this method with the code editor

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;
        }
    }
}
Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130