Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.IO;
- using System.Windows.Forms;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Xml.Linq;
- using System.Text.RegularExpressions;
- namespace WWI_Auslesen
- {
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- public RichTextBoxScrollBars ScrollBars { get; set; }
- private void Choose_File(object sender, RoutedEventArgs e)
- {
- OpenFileDialog fileDialog = new OpenFileDialog();
- StringBuilder sb = new StringBuilder();
- fileDialog.Multiselect = true;
- fileDialog.Filter = "WWI Files|*.wwi|XML Files|*.xml";
- fileDialog.DefaultExt = "*.wwi";
- DialogResult result = fileDialog.ShowDialog();
- if (result == System.Windows.Forms.DialogResult.OK)
- {
- string[] xmlTextLines = File.ReadAllLines(fileDialog.FileName);
- foreach (var line in xmlTextLines)
- {
- if (string.IsNullOrWhiteSpace(line))
- continue;
- string timeIdentifier = line.Contains("S:<") ? "S:<" : "R:<";
- string timeString = line.Substring(0, line.IndexOf(timeIdentifier));
- string xmlString = line.Substring(line.IndexOf(timeIdentifier) + 2);
- xmlString = XDocument.Parse(xmlString).ToString();
- sb.Append(timeString);
- sb.AppendLine(timeIdentifier.Remove(timeIdentifier.Length - 1, 1));
- sb.AppendLine(xmlString);
- sb.AppendLine();
- RichTextBox1.AppendText(sb.ToString());
- }
- }
- }
- private void richtextBox1_PreviewDrop(object sender, System.Windows.DragEventArgs e)
- {
- StringBuilder sb = new StringBuilder();
- string[] fileloadup = (string[])e.Data.GetData(System.Windows.DataFormats.FileDrop);
- string[] xmlTextLines = File.ReadAllLines(fileloadup[0]);
- foreach (var line in xmlTextLines)
- {
- if (string.IsNullOrWhiteSpace(line))
- continue;
- string timeIdentifier = line.Contains("S:<") ? "S:<" : "R:<";
- string timeString = line.Substring(0, line.IndexOf(timeIdentifier));
- string xmlString = line.Substring(line.IndexOf(timeIdentifier) + 2);
- xmlString = XDocument.Parse(xmlString).ToString();
- sb.Append(timeString);
- sb.AppendLine(timeIdentifier.Remove(timeIdentifier.Length - 1, 1));
- sb.AppendLine(xmlString);
- sb.AppendLine();
- RichTextBox1.AppendText(sb.ToString());
- }
- e.Handled = true;
- }
- private void richtextBox1_PreviewDragOver(object sender, System.Windows.DragEventArgs e)
- {
- e.Effects = System.Windows.DragDropEffects.All;
- e.Handled = true;
- }
- private void Cancel(object sender, RoutedEventArgs e)
- {
- this.Close();
- }
- private void Button_Search(object sender, RoutedEventArgs e)
- {
- TextRange textRange = new TextRange(RichTextBox1.Document.ContentStart, RichTextBox1.Document.ContentEnd);
- textRange.ClearAllProperties();
- lbl_Status.Content = "";
- string textBoxText = textRange.Text;
- string searchText = userInput.Text;
- if (string.IsNullOrWhiteSpace(textBoxText) || string.IsNullOrWhiteSpace(searchText))
- {
- lbl_Status.Content = "Bitte erst eine Datei wählen!";
- }
- else
- {
- Regex regex = new Regex(searchText);
- int count_MatchFound = Regex.Matches(textBoxText, regex.ToString()).Count;
- for (TextPointer startPointer = RichTextBox1.Document.ContentStart;
- startPointer.CompareTo(RichTextBox1.Document.ContentEnd) <= 0;
- startPointer = startPointer.GetNextContextPosition(LogicalDirection.Forward))
- {
- if (startPointer.CompareTo(RichTextBox1.Document.ContentEnd) == 0)
- {
- break;
- }
- string parsedString = startPointer.GetTextInRun(LogicalDirection.Forward);
- int indexOfParseString = parsedString.IndexOf(searchText);
- if (indexOfParseString >= 0)
- {
- startPointer = startPointer.GetPositionAtOffset(indexOfParseString);
- if (startPointer != null)
- {
- TextPointer nextPointer = startPointer.GetPositionAtOffset(searchText.Length);
- TextRange searchedTextRange = new TextRange(startPointer, nextPointer);
- searchedTextRange.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Yellow));
- }
- }
- }
- if (count_MatchFound > 0)
- {
- lbl_Status.Content = "Erzielte Treffer: " + count_MatchFound;
- }
- else
- {
- lbl_Status.Content = "Kein Treffer erzielt!";
- }
- }
- }
- private void WatermarkedTxt_GotFocus(object sender, RoutedEventArgs e)
- {
- watermarkedTxt.Visibility = System.Windows.Visibility.Collapsed;
- userInput.Visibility = System.Windows.Visibility.Visible;
- userInput.Focus();
- }
- private void UserInput_LostFocus(object sender, RoutedEventArgs e)
- {
- if (string.IsNullOrEmpty(userInput.Text))
- {
- userInput.Visibility = System.Windows.Visibility.Collapsed;
- watermarkedTxt.Visibility = System.Windows.Visibility.Visible;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment