Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Xml.Serialization;
- namespace AES
- {
- /// <summary>
- /// Логика взаимодействия для MainWindow.xaml
- /// </summary>
- public partial class SampleWindow : Window
- {
- private const String textboxSampleNumberDefault = "Номер замера";
- private const String textboxTimeDefault = "ЧЧ:ММ";
- private const String textboxMEDDefault = "МЭД, мкЗв/ч";
- private static MainWindow main = (MainWindow)Application.Current.MainWindow;
- private int index = -1;
- public SampleWindow()
- {
- InitializeComponent();
- }
- public SampleWindow(int index, Sample sample)
- {
- InitializeComponent();
- comboBox_Location.Text = sample.location;
- button_date.Content = sample.dateTime.ToString("dd.MM.yyyy");
- textBox_Time.Text = sample.dateTime.ToString("HH:mm");
- textBox_MED.Text = sample.MED.ToString();
- comboBox_Dosimeter.Text = sample.dosimeter;
- this.index = index;
- }
- [XmlInclude(typeof(DATA))]
- private void button_Save_Click(object sender, RoutedEventArgs e)
- {
- if (!checkAllFields()) return;
- hideIncorrectLabels();
- String date = button_date.Content.ToString(), time = textBox_Time.Text;
- DateTime dateTime = DateTime.Parse(date + " " + time);
- if (index == -1)
- {
- main.addSampleToList(new Sample(
- comboBox_Location.Text,
- dateTime,
- comboBox_Dosimeter.Text,
- Double.Parse(textBox_MED.Text)));
- }
- else
- {
- main.editSampleInList(index, new Sample(
- comboBox_Location.Text,
- dateTime,
- comboBox_Dosimeter.Text,
- Double.Parse(textBox_MED.Text)));
- }
- Close();
- }
- private void hideIncorrectLabels()
- {
- label_incorrectLocation.Visibility = Visibility.Hidden;
- label_incorrectDate.Visibility = Visibility.Hidden;
- label_incorrectTime.Visibility = Visibility.Hidden;
- label_incorrectMED.Visibility = Visibility.Hidden;
- label_incorrectDosimeter.Visibility = Visibility.Hidden;
- }
- private bool checkAllFields()
- {
- return
- checkLocation() &
- checkDate() &
- checkTime() &
- checkMED() &
- checkDosimeter();
- }
- private bool checkLocation()
- {
- if (comboBox_Location.Text == null || comboBox_Location.Text.Equals(""))
- {
- label_incorrectLocation.Visibility = Visibility.Visible;
- return false;
- }
- return true;
- }
- private bool checkDate()
- {
- if (button_date.Content.ToString().Equals("Выберите дату"))
- {
- label_incorrectDate.Visibility = Visibility.Visible;
- return false;
- }
- return true;
- }
- private bool checkTime()
- {
- if (!isTimeTextCorrect(textBox_Time.Text))
- {
- label_incorrectTime.Visibility = Visibility.Visible;
- return false;
- }
- return true;
- }
- private bool checkMED()
- {
- if (!isMEDTextCorrect(textBox_MED.Text))
- {
- label_incorrectMED.Visibility = Visibility.Visible;
- return false;
- }
- return true;
- }
- private bool checkDosimeter()
- {
- if (comboBox_Dosimeter.Text.Equals(""))
- {
- label_incorrectDosimeter.Visibility = Visibility.Visible;
- return false;
- }
- return true;
- }
- private bool isTimeTextCorrect(string time)
- {
- if (time.Length > 5) return false;
- if (time[2] != ':') return false;
- try
- {
- int hour = Int32.Parse(time.Substring(0, 2)),
- minute = Int32.Parse(time.Substring(3, 2));
- if (hour < 0 || minute < 0 || hour > 23 || minute > 59) return false;
- }
- catch (Exception)
- {
- return false;
- }
- return true;
- }
- private bool isMEDTextCorrect(string MED)
- {
- int dotsCount = 0;
- foreach (char c in MED)
- {
- if (c == '.') dotsCount++;
- if (dotsCount > 1) return false;
- }
- return isTextNumbersAndDotsOnly(MED);
- }
- private void button_Cancel_Click(object sender, RoutedEventArgs e)
- {
- int result = (int) MessageBox.Show(
- "Вы действительно хотите выйти?\nВсе несохранённые данны будут утеряны!", "Выход",
- MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No); //Создание диалог. окна
- switch (result) //Обработка ответов
- {
- case (int)MessageBoxResult.Yes:
- {
- Close();
- break;
- }
- case (int)MessageBoxResult.No:
- break;
- }
- Close();
- }
- private void textBox_Time_GotFocus(object sender, RoutedEventArgs e)
- {
- TextBox textBox = (TextBox)sender;
- if (textBox.Text.Equals(textboxTimeDefault))
- {
- textBox.Text = "";
- textBox.Opacity = 1.0;
- }
- }
- private void textBox_Time_LostFocus(object sender, RoutedEventArgs e)
- {
- TextBox textBox = (TextBox)sender;
- if (textBox.Text.Equals(textboxTimeDefault) || textBox.Text.Equals(""))
- {
- textBox.Text = textboxTimeDefault;
- textBox.Opacity = 0.5;
- }
- }
- private void textBox_Time_PreviewTextInput(object sender, TextCompositionEventArgs e)
- {
- e.Handled = !isTextNumbersOnly(e.Text);
- TextBox textBox = (TextBox)sender;
- String currentText = textBox.Text;
- int length = currentText.Length;
- if (length == 2)
- {
- textBox.Text = currentText + ':';
- textBox.CaretIndex = length + 1;
- }
- else if (length >= 5)
- {
- textBox.Text = currentText.Substring(0, 4);
- textBox.CaretIndex = 4;
- }
- }
- private void textBox_MED_GotFocus(object sender, RoutedEventArgs e)
- {
- TextBox textBox = (TextBox)sender;
- textBox.Opacity = 1.0;
- if (textBox.Text.Equals(textboxMEDDefault))
- {
- textBox.Text = "";
- }
- }
- private void textBox_MED_LostFocus(object sender, RoutedEventArgs e)
- {
- TextBox textBox = (TextBox)sender;
- if (textBox.Text.Equals(textboxMEDDefault) || textBox.Text.Equals(""))
- {
- textBox.Text = textboxMEDDefault;
- textBox.Opacity = 0.5;
- }
- }
- private bool isTextNumbersOnly(string text)
- {
- foreach(char c in text)
- {
- if (!Char.IsDigit(c)) return false;
- }
- return true;
- }
- private bool isTextNumbersAndDotsOnly(string text)
- {
- foreach(char c in text)
- {
- if (!Char.IsDigit(c) && c != ',') return false;
- }
- return true;
- }
- private void textBox_MED_PreviewTextInput(object sender, TextCompositionEventArgs e)
- {
- e.Handled = !isTextNumbersAndDotsOnly(e.Text);
- }
- private void button_date_Click(object sender, RoutedEventArgs e)
- {
- if (calendar.IsEnabled)
- {
- calendar.Visibility = Visibility.Collapsed;
- calendar.IsEnabled = false;
- }
- else
- {
- calendar.Visibility = Visibility.Visible;
- calendar.IsEnabled = true;
- }
- }
- private void calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
- {
- button_date.Content = ((DateTime)calendar.SelectedDate).ToString("dd.MM.yyyy");
- calendar.Visibility = Visibility.Hidden;
- calendar.IsEnabled = false;
- }
- private void textBox_SampleNumber_PreviewTextInput(object sender, TextCompositionEventArgs e)
- {
- e.Handled = !isTextNumbersOnly(e.Text);
- }
- private void DebugButton_Click(object sender, RoutedEventArgs e)
- {
- comboBox_Location.Text = "Пост №1";
- button_date.Content = DateTime.Now.ToString("dd.MM.yyyy");
- textBox_Time.Text = DateTime.Now.ToString("HH:mm");
- textBox_MED.Text = "3,14159";
- comboBox_Dosimeter.Text = "ИРД-02";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement