private void DateTimePicker1_CalendarClosed(object sender, RoutedEventArgs e) { bool b2 = false; using (StreamReader file = new StreamReader("WriteLines.txt")) { string ln; while ((ln = file.ReadLine()) != null) { if (ln == DateTimePicker1.ToString()) { Textbox1.Text = file.ReadLine(); b2 = true; } } file.Close(); } if (b2 == false) { if (this.isDataDirty) { string msg = "Data is dirty. Close without saving?"; MessageBoxResult result = MessageBox.Show( msg, "Data App", MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result == MessageBoxResult.No) { // If user doesn't want to close, cancel closure } else { Textbox1.Text = ""; this.isDataDirty = false; } } } }