Advertisement
RMarK0

Task1 MainWindow.xaml.cs

Jun 19th, 2021
1,257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using System.Windows;
  5. using System.Windows.Input;
  6. using static Task1.ViewModelHelper;
  7.  
  8. namespace Task1
  9. {
  10.     public partial class MainWindow : Window
  11.     {
  12.         private readonly MainWindow _activeWindow; // Variable, that contains reference to current window
  13.         public MainWindow()
  14.         {
  15.             InitializeComponent();
  16.             _activeWindow = this;
  17.         }
  18.  
  19.         private void CalculateButton_OnClick(object sender, RoutedEventArgs e) // Method that processes click for "Calculate" button
  20.         {
  21.             ClearPreviousResults(_activeWindow);                // First of all, we need to clear previous table
  22.             string input = StringsIdTextBox.Text;               //
  23.             List<int> cleanInput = CleanInputString(input);     // Then we need to clean our input
  24.  
  25.            
  26.             foreach (int clInput in cleanInput)
  27.             {
  28.                 string text = null;
  29.                 try
  30.                 {
  31.                     text = GetTextFromJson(ObtainJObject(clInput, _activeWindow), _activeWindow);   // Obtain text from json
  32.                     TextData temp = CalculateTextData(text);                                        // Calculate vowels and words count
  33.                     CreateTextNode(temp, _activeWindow);                                            // Create text nodes in table
  34.                 }
  35.                 catch (Exception exception)
  36.                 {
  37.                     CreateNotification(_activeWindow, exception.Message);   // If error occurs, create notification
  38.                     break;
  39.                 }
  40.                 if (badStringsId.Count > 0) // If we have bad IDs, create notification
  41.                 {
  42.                     string error = "Неверные идентификаторы: ";
  43.                     foreach (int item in badStringsId)
  44.                         error = String.Concat(error, item.ToString(), " ");
  45.                     CreateNotification(_activeWindow, error);
  46.                 }
  47.             }
  48.         }
  49.  
  50.         private static readonly Regex _regex = new Regex("[^0-9,;]+"); // Regex for preventing entering wrong symbols
  51.         private static bool IsTextAllowed(string text)
  52.         {
  53.             return !_regex.IsMatch(text);
  54.         }
  55.         private void StringsIdTextBox_OnPreviewTextInput(object sender, TextCompositionEventArgs e) // Method for detecting wrong symbols in StringsIdTextBox
  56.         {
  57.             e.Handled = !IsTextAllowed(e.Text);
  58.         }
  59.     }
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement