Reputation: 59
I have WPF application that holds some buttons. One button is named ChangeTime so when clicked, dialog opens and there is hh/mm/ss what you can change. I want to store picked time in variable.
My problem is that I only want store that new time if user really changed time. Currently when user presses button, event triggers and new value is stored. Also it shouldn't store new value when dialog is closed without touching Time field.
Essentially I want to change BtnWasClicked to true when time is actually changed. Poor choice of variable name indeed.
Any way to prevent it?
private DateTimePicker _timePortionDateTimePicker;
private DateTime _pickedTime;
private bool btnWasClicked = false;
private bool timeChanged = false;
private void TimeIsChanged(object sender, EventArgs e)
{
timeChanged = true;
if (timeChanged)
{
btnWasClicked = true;
}
}
private void BtnChangeTime_OnClick(object sender, EventArgs e)
{
Form timeDialog = new Form();
_timePortionDateTimePicker = new DateTimePicker();
_timePortionDateTimePicker.Format = DateTimePickerFormat.Time;
_timePortionDateTimePicker.ShowUpDown = true;
_timePortionDateTimePicker.ValueChanged += new
EventHandler(TimeIsChanged);
timeDialog.Controls.Add(_timePortionDateTimePicker);
timeDialog.ShowDialog();
_pickedTime = _timePortionDateTimePicker.Value;
}
Upvotes: 1
Views: 90
Reputation: 274835
You don't actually need TimeIsChanged
. You can just check whether _timePortionDateTimePicker.Value
is different from _pickedTime
in BtnChangeTime_OnClick
.
Form timeDialog = new Form();
_timePortionDateTimePicker = new DateTimePicker();
_timePortionDateTimePicker.Format = DateTimePickerFormat.Time;
_timePortionDateTimePicker.ShowUpDown = true;
// start with the previous chosen time
_timePortionDateTimePicker.Value = _pickedTime;
timeDialog.Controls.Add(_timePortionDateTimePicker);
timeDialog.ShowDialog();
if (_pickedTime != _timePortionDateTimePicker.Value) {
// time has changed, assign to _pickedTime and other variables
_pickedTime != _timePortionDateTimePicker.Value;
timeChanged = true;
btnWasClicked = true;
} // otherwise do nothing
Upvotes: 1