this is what I am doing,
//this doesnt set the datetimepicker value to the set value
class{
constructor
{
InitializeComponent(); // -> this initializes all the form components
DateTimePicker.Value = System.DateTime.Now.AddDays(30); //->trying to set the date time picker value to a date one month from now.
}
}
//but this does set the date to the desired value..
class{
constructor
{
InitializeComponent(); // -> this initializes all the form components
}
form_onLoad() //->on form load event
{
DateTimePicker.Value = System.DateTime.Now.AddDays(30);
}
}
Can someone please explain whats the difference and why it doesnt change date with previous method? and why it sets with the latter method?
The first time you use this line:
DateTimePicker.Value = System.DateTime.Now.AddDays(30);is before the form loads, in the form constructor. When the form actually loads, the value is reset. You can’t manipulate controls in the instantiation code of the container.