I have a WinForms-based (.NET Framework 4.7.2) form that flickers when displayed because it has a lot of controls. I turned on DoubleBuffred and overridden CreateParams. Although it seemed to stop flickering, it would flicker again after minimizing and opening again, so I overridden OnResize and used RecreateHandle() when FormWindowState.Normal. This does solve the flickering problem, but I found that when I minimized the form and then restored it, the default value of DateTimePicker changed. And other controls such as CheckBox are normal. Why is this?
The relevant code is as follows:
private void FormSettings_Load(object sender, EventArgs e)
{
DateTimePickerMain.Value = ConfigManager.IsValidData(SelectedTime) ? SelectedTime : DateTime.Now;
// MessageBox.Show($"{SelectedTime}");
// At first I thought SelectedTime was invalid, then I checked it using MessageBox and found that
// it was indeed a valid date, but the DateTimePicker displayed the current date.
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (WindowState == FormWindowState.Normal)
{
RecreateHandle();
Activate();
}
}
More importantly, even if I use DateTimePickerMain.Value = new DateTime(2024, 1, 1) directly, it will display the current date after minimizing.