Advertisement
Slootiasha

SampleWindowC#

May 19th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.71 KB | None | 0 0
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using System.Xml.Serialization;
  6.  
  7. namespace AES
  8. {
  9.     /// <summary>
  10.     /// Логика взаимодействия для MainWindow.xaml
  11.     /// </summary>
  12.     public partial class SampleWindow : Window
  13.     {
  14.         private const String textboxSampleNumberDefault = "Номер замера";
  15.         private const String textboxTimeDefault         = "ЧЧ:ММ";
  16.         private const String textboxMEDDefault          = "МЭД, мкЗв/ч";
  17.  
  18.         private static MainWindow main = (MainWindow)Application.Current.MainWindow;
  19.  
  20.         private int index = -1;
  21.  
  22.         public SampleWindow()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.  
  27.         public SampleWindow(int index, Sample sample)
  28.         {
  29.             InitializeComponent();
  30.  
  31.             comboBox_Location.Text = sample.location;
  32.             button_date.Content = sample.dateTime.ToString("dd.MM.yyyy");
  33.             textBox_Time.Text = sample.dateTime.ToString("HH:mm");
  34.             textBox_MED.Text = sample.MED.ToString();
  35.             comboBox_Dosimeter.Text = sample.dosimeter;
  36.  
  37.             this.index = index;
  38.         }
  39.  
  40.         [XmlInclude(typeof(DATA))]
  41.         private void button_Save_Click(object sender, RoutedEventArgs e)
  42.         {
  43.             if (!checkAllFields()) return;
  44.             hideIncorrectLabels();
  45.            
  46.             String date = button_date.Content.ToString(), time = textBox_Time.Text;
  47.             DateTime dateTime = DateTime.Parse(date + " " + time);
  48.  
  49.             if (index == -1)
  50.             {
  51.                 main.addSampleToList(new Sample(
  52.                     comboBox_Location.Text,
  53.                     dateTime,
  54.                     comboBox_Dosimeter.Text,
  55.                     Double.Parse(textBox_MED.Text)));
  56.             }
  57.             else
  58.             {
  59.                 main.editSampleInList(index, new Sample(
  60.                     comboBox_Location.Text,
  61.                     dateTime,
  62.                     comboBox_Dosimeter.Text,
  63.                     Double.Parse(textBox_MED.Text)));
  64.             }
  65.             Close();
  66.         }
  67.  
  68.         private void hideIncorrectLabels()
  69.         {
  70.             label_incorrectLocation.Visibility     = Visibility.Hidden;
  71.             label_incorrectDate.Visibility         = Visibility.Hidden;
  72.             label_incorrectTime.Visibility         = Visibility.Hidden;
  73.             label_incorrectMED.Visibility          = Visibility.Hidden;
  74.             label_incorrectDosimeter.Visibility    = Visibility.Hidden;
  75.         }
  76.  
  77.         private bool checkAllFields()
  78.         {
  79.             return
  80.                 checkLocation()     &
  81.                 checkDate()         &
  82.                 checkTime()         &
  83.                 checkMED()          &
  84.                 checkDosimeter();
  85.         }
  86.  
  87.         private bool checkLocation()
  88.         {
  89.             if (comboBox_Location.Text == null || comboBox_Location.Text.Equals(""))
  90.             {
  91.                 label_incorrectLocation.Visibility = Visibility.Visible;
  92.                 return false;
  93.             }
  94.             return true;
  95.         }
  96.  
  97.         private bool checkDate()
  98.         {
  99.             if (button_date.Content.ToString().Equals("Выберите дату"))
  100.             {
  101.                 label_incorrectDate.Visibility = Visibility.Visible;
  102.                 return false;
  103.             }
  104.             return true;
  105.         }
  106.  
  107.         private bool checkTime()
  108.         {
  109.             if (!isTimeTextCorrect(textBox_Time.Text))
  110.             {
  111.                 label_incorrectTime.Visibility = Visibility.Visible;
  112.                 return false;
  113.             }
  114.             return true;
  115.         }
  116.  
  117.         private bool checkMED()
  118.         {
  119.             if (!isMEDTextCorrect(textBox_MED.Text))
  120.             {
  121.                 label_incorrectMED.Visibility = Visibility.Visible;
  122.                 return false;
  123.             }
  124.             return true;
  125.         }
  126.  
  127.         private bool checkDosimeter()
  128.         {
  129.             if (comboBox_Dosimeter.Text.Equals(""))
  130.             {
  131.                 label_incorrectDosimeter.Visibility = Visibility.Visible;
  132.                 return false;
  133.             }
  134.             return true;
  135.         }
  136.  
  137.         private bool isTimeTextCorrect(string time)
  138.         {
  139.             if (time.Length > 5) return false;
  140.  
  141.             if (time[2] != ':') return false;
  142.  
  143.             try
  144.             {
  145.                 int hour   = Int32.Parse(time.Substring(0, 2)),
  146.                     minute = Int32.Parse(time.Substring(3, 2));
  147.  
  148.                 if (hour < 0 || minute < 0 || hour > 23 || minute > 59) return false;
  149.             }
  150.             catch (Exception)
  151.             {
  152.                 return false;
  153.             }
  154.             return true;
  155.         }
  156.  
  157.         private bool isMEDTextCorrect(string MED)
  158.         {
  159.             int dotsCount = 0;
  160.             foreach (char c in MED)
  161.             {
  162.                 if (c == '.') dotsCount++;
  163.                 if (dotsCount > 1) return false;
  164.             }
  165.             return isTextNumbersAndDotsOnly(MED);
  166.         }
  167.  
  168.         private void button_Cancel_Click(object sender, RoutedEventArgs e)
  169.         {
  170.             int result = (int) MessageBox.Show(
  171.                 "Вы действительно хотите выйти?\nВсе несохранённые данны будут утеряны!", "Выход",
  172.                 MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No); //Создание диалог. окна
  173.  
  174.             switch (result) //Обработка ответов
  175.             {
  176.                 case (int)MessageBoxResult.Yes:
  177.                     {
  178.                         Close();
  179.                         break;
  180.                     }
  181.                 case (int)MessageBoxResult.No:
  182.                     break;
  183.             }
  184.             Close();
  185.         }
  186.  
  187.         private void textBox_Time_GotFocus(object sender, RoutedEventArgs e)
  188.         {
  189.             TextBox textBox = (TextBox)sender;
  190.             if (textBox.Text.Equals(textboxTimeDefault))
  191.             {
  192.                 textBox.Text = "";
  193.                 textBox.Opacity = 1.0;
  194.             }
  195.         }
  196.  
  197.         private void textBox_Time_LostFocus(object sender, RoutedEventArgs e)
  198.         {
  199.             TextBox textBox = (TextBox)sender;
  200.             if (textBox.Text.Equals(textboxTimeDefault) || textBox.Text.Equals(""))
  201.             {
  202.                 textBox.Text = textboxTimeDefault;
  203.                 textBox.Opacity = 0.5;
  204.             }
  205.         }
  206.  
  207.         private void textBox_Time_PreviewTextInput(object sender, TextCompositionEventArgs e)
  208.         {
  209.             e.Handled = !isTextNumbersOnly(e.Text);
  210.             TextBox textBox = (TextBox)sender;
  211.             String currentText = textBox.Text;
  212.             int length = currentText.Length;
  213.  
  214.             if (length == 2)
  215.             {
  216.                 textBox.Text = currentText + ':';
  217.                 textBox.CaretIndex = length + 1;
  218.             }
  219.             else if (length >= 5)
  220.             {
  221.                 textBox.Text = currentText.Substring(0, 4);
  222.                 textBox.CaretIndex = 4;
  223.             }
  224.         }
  225.  
  226.         private void textBox_MED_GotFocus(object sender, RoutedEventArgs e)
  227.         {
  228.             TextBox textBox = (TextBox)sender;
  229.             textBox.Opacity = 1.0;
  230.             if (textBox.Text.Equals(textboxMEDDefault))
  231.             {
  232.                 textBox.Text = "";
  233.             }
  234.         }
  235.  
  236.         private void textBox_MED_LostFocus(object sender, RoutedEventArgs e)
  237.         {
  238.             TextBox textBox = (TextBox)sender;
  239.             if (textBox.Text.Equals(textboxMEDDefault) || textBox.Text.Equals(""))
  240.             {
  241.                 textBox.Text = textboxMEDDefault;
  242.                 textBox.Opacity = 0.5;
  243.             }
  244.         }
  245.  
  246.         private bool isTextNumbersOnly(string text)
  247.         {
  248.             foreach(char c in text)
  249.             {
  250.                 if (!Char.IsDigit(c)) return false;
  251.             }
  252.             return true;
  253.         }
  254.  
  255.         private bool isTextNumbersAndDotsOnly(string text)
  256.         {
  257.             foreach(char c in text)
  258.             {
  259.                 if (!Char.IsDigit(c) && c != ',') return false;
  260.             }
  261.             return true;
  262.         }
  263.  
  264.         private void textBox_MED_PreviewTextInput(object sender, TextCompositionEventArgs e)
  265.         {
  266.             e.Handled = !isTextNumbersAndDotsOnly(e.Text);
  267.         }
  268.  
  269.         private void button_date_Click(object sender, RoutedEventArgs e)
  270.         {
  271.             if (calendar.IsEnabled)
  272.             {
  273.                 calendar.Visibility = Visibility.Collapsed;
  274.                 calendar.IsEnabled = false;
  275.             }
  276.             else
  277.             {
  278.                 calendar.Visibility = Visibility.Visible;
  279.                 calendar.IsEnabled = true;
  280.             }
  281.         }
  282.  
  283.         private void calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
  284.         {
  285.             button_date.Content = ((DateTime)calendar.SelectedDate).ToString("dd.MM.yyyy");
  286.             calendar.Visibility = Visibility.Hidden;
  287.             calendar.IsEnabled = false;
  288.         }
  289.  
  290.         private void textBox_SampleNumber_PreviewTextInput(object sender, TextCompositionEventArgs e)
  291.         {
  292.             e.Handled = !isTextNumbersOnly(e.Text);
  293.         }
  294.  
  295.         private void DebugButton_Click(object sender, RoutedEventArgs e)
  296.         {
  297.             comboBox_Location.Text = "Пост №1";
  298.             button_date.Content = DateTime.Now.ToString("dd.MM.yyyy");
  299.             textBox_Time.Text = DateTime.Now.ToString("HH:mm");
  300.             textBox_MED.Text = "3,14159";
  301.             comboBox_Dosimeter.Text = "ИРД-02";
  302.         }
  303.     }
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement