382 lines
12 KiB
C#
382 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Test_SearchAndFind
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
#region Version und Copyright
|
|
// Version und Copyright
|
|
string programName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen
|
|
string programVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
|
|
static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
|
|
string copyright = GenerateCopyright();
|
|
string icon = Application.StartupPath + "\\Info.bmp";
|
|
//Assembly Datum und Zeit
|
|
static DateTime value = AssemblyDateTime();
|
|
string date = value.ToShortDateString();
|
|
string time = value.ToLongTimeString();
|
|
private static DateTime AssemblyDateTime()
|
|
{
|
|
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
|
|
var buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(TimeSpan.TicksPerDay * version.Build + TimeSpan.TicksPerSecond * 2 * version.Revision));
|
|
//Tage seit dem 1. Januar 2000 und Sekunden seit Mitternacht, (multiplizier mit 2 ergibt das Original)
|
|
return buildDateTime;
|
|
}
|
|
|
|
private static string CurrentYear()
|
|
{
|
|
DateTime dtn = DateTime.Now;
|
|
return dtn.Year.ToString();
|
|
}
|
|
|
|
private static string StartYear()
|
|
{
|
|
string startYear = ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
|
|
return startYear.Remove(0, startYear.LastIndexOf(" ") + 1);
|
|
}
|
|
|
|
private static string GenerateCopyright()
|
|
{
|
|
string startYear = StartYear();
|
|
string currentYear = CurrentYear();
|
|
|
|
if (startYear == currentYear)
|
|
{
|
|
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
|
|
}
|
|
else
|
|
{
|
|
return String.Concat(((AssemblyCopyrightAttribute)attributes[0]).Copyright, "-", currentYear);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Programm-Info
|
|
/// <summary>
|
|
/// Contains the name of the program file
|
|
/// </summary>
|
|
public static string ProgramName { set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the version of the program file
|
|
/// </summary>
|
|
public static string ProgramVersion { set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the copyright notice
|
|
/// </summary>
|
|
public static string Copyright { set; get; }
|
|
|
|
private void SetProgramInfo()
|
|
{
|
|
//Name, Version und Copyright setzen
|
|
ProgramName = programName;
|
|
ProgramVersion = programVersion;
|
|
Copyright = copyright;
|
|
}
|
|
#endregion
|
|
public Form1()
|
|
{
|
|
SetProgramInfo();
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void Form1_Load(object sender, EventArgs e)
|
|
{
|
|
this.Text = String.Concat("Test_Search, Find and Replace - Version: ", ProgramVersion, " - ", Copyright, " by E. Höglinger");
|
|
textBoxText.Focus();
|
|
textBoxText.Select(0, 0);
|
|
|
|
//textBoxSearchstring.Focus();
|
|
//textBoxSearchstring.Text = "";
|
|
this.ActiveControl = textBoxSearchstring;
|
|
}
|
|
|
|
List<int> AllPositions = new List<int>();
|
|
int SelectedPosition { set; get; }
|
|
|
|
private List<int> Positions(string myText, string searchText)
|
|
{
|
|
List<int> pos = new List<int>();
|
|
|
|
if (String.IsNullOrEmpty(myText) || String.IsNullOrEmpty(searchText))
|
|
{
|
|
return pos;
|
|
}
|
|
|
|
int tempPos = 0;
|
|
int currentPos = 0;
|
|
|
|
while (myText.Contains(searchText))
|
|
{
|
|
if (checkBoxFullword.Checked)
|
|
{
|
|
//Nur ganzes Wort suchen
|
|
currentPos = IndexOfWord(myText, searchText);
|
|
}
|
|
else
|
|
{
|
|
//Suchstring suchen
|
|
currentPos = myText.IndexOf(searchText);
|
|
}
|
|
|
|
tempPos += currentPos;
|
|
pos.Add(tempPos);
|
|
tempPos += searchText.Length;
|
|
|
|
myText = myText.Remove(0, currentPos + searchText.Length);
|
|
|
|
if (currentPos == -1)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
return pos;
|
|
}
|
|
|
|
private Regex myRegex;
|
|
|
|
public int IndexOfWord(string myString, string myWord)
|
|
{
|
|
myRegex = new Regex("\\b" + Regex.Escape(myWord) + "\\b", RegexOptions.Compiled);
|
|
|
|
Match mtch = myRegex.Match(myString);
|
|
|
|
if (mtch.Success)
|
|
return mtch.Index;
|
|
else
|
|
return -1;
|
|
}
|
|
|
|
private void Search()
|
|
{
|
|
AllPositions.Clear();
|
|
|
|
string fullText = textBoxText.Text;
|
|
string searchText = textBoxSearchstring.Text;
|
|
|
|
if (checkBoxCase.Checked)
|
|
{
|
|
//Groß-/Kleinschreibung ignorieren
|
|
fullText = fullText.ToLower();
|
|
searchText = searchText.ToLower();
|
|
}
|
|
|
|
if (!String.IsNullOrEmpty(searchText) && fullText.Contains(searchText))
|
|
{
|
|
AllPositions = Positions(fullText, searchText);
|
|
|
|
SelectedPosition = 0;
|
|
|
|
if (AllPositions[SelectedPosition] != -1)
|
|
{
|
|
SelectSearchString(AllPositions[SelectedPosition], searchText.Length);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Es wurde kein (weiterer) Suchtext gefunden");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Es wurde kein (weiterer) Suchtext gefunden");
|
|
}
|
|
}
|
|
|
|
private void Previous()
|
|
{
|
|
if (!String.IsNullOrEmpty(textBoxSearchstring.Text))
|
|
{
|
|
if (AllPositions.Count == 0)
|
|
{
|
|
Search();
|
|
if (textBoxText.Text.Contains(textBoxSearchstring.Text))
|
|
{
|
|
Previous(); //Den letzten gefundenen Suchstring selektieren
|
|
}
|
|
}
|
|
else if (SelectedPosition > 0)
|
|
{
|
|
//Position um eins verringern und Suchstring im Text selektieren
|
|
SelectedPosition--;
|
|
SelectSearchString(AllPositions[SelectedPosition], textBoxSearchstring.Text.Length);
|
|
}
|
|
else
|
|
{
|
|
//Selected Position auf die letzte Position setzten und Suchstring im Text selektieren
|
|
SelectedPosition = AllPositions.Count - 1;
|
|
SelectSearchString(AllPositions[SelectedPosition], textBoxSearchstring.Text.Length);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Next()
|
|
{
|
|
if (!String.IsNullOrEmpty(textBoxSearchstring.Text))
|
|
{
|
|
if (AllPositions.Count == 0)
|
|
{
|
|
Search();
|
|
}
|
|
else if (SelectedPosition + 1 < AllPositions.Count)
|
|
{
|
|
//Position um eins erhöhen und Suchstring im Text selektieren
|
|
SelectedPosition++;
|
|
SelectSearchString(AllPositions[SelectedPosition], textBoxSearchstring.Text.Length);
|
|
}
|
|
else
|
|
{
|
|
//Selected Position auf 0 setzten und Suchstring im Text selektieren
|
|
SelectedPosition = 0;
|
|
SelectSearchString(AllPositions[SelectedPosition], textBoxSearchstring.Text.Length);
|
|
}
|
|
}
|
|
}
|
|
|
|
private int Replace()
|
|
{
|
|
if (AllPositions[SelectedPosition] != -1)
|
|
{
|
|
textBoxText.Text = textBoxText.Text.Remove(AllPositions[SelectedPosition], textBoxSearchstring.Text.Length);
|
|
textBoxText.Text = textBoxText.Text.Insert(AllPositions[SelectedPosition], textBoxReplaceText.Text);
|
|
textBoxText.Refresh();
|
|
Search(); //Neu durchsuchen, damit die Starindexes wieder passen
|
|
return 0; //Ende noch nicht erreicht
|
|
}
|
|
else
|
|
{
|
|
return -1; //Ende erreicht
|
|
}
|
|
}
|
|
|
|
private void ReplaceAll()
|
|
{
|
|
string fullText = textBoxText.Text;
|
|
string searchText = textBoxSearchstring.Text;
|
|
|
|
if (checkBoxCase.Checked)
|
|
{
|
|
//Groß-/Kleinschreibung ignorieren
|
|
fullText = fullText.ToLower();
|
|
searchText = searchText.ToLower();
|
|
}
|
|
|
|
int nextRun = 0;
|
|
|
|
while (fullText.Contains(searchText) && nextRun != -1)
|
|
{
|
|
nextRun = Replace();
|
|
fullText = textBoxText.Text;
|
|
|
|
if (checkBoxCase.Checked)
|
|
{
|
|
//Groß-/Kleinschreibung ignorieren
|
|
fullText = fullText.ToLower();
|
|
}
|
|
}
|
|
|
|
textBoxReplaceText.Focus();
|
|
}
|
|
|
|
private void SelectSearchString(int position, int length)
|
|
{
|
|
if (position != -1)
|
|
{
|
|
textBoxText.Focus();
|
|
textBoxText.Select(position, length);
|
|
textBoxText.Refresh();
|
|
}
|
|
}
|
|
|
|
private void textBoxSearchstring_KeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.KeyCode == Keys.Enter)
|
|
{
|
|
Search();
|
|
|
|
|
|
//TODO: Test
|
|
var suche = new Search(textBoxText.Text, textBoxSearchstring.Text, checkBoxCase.Checked, checkBoxFullword.Checked);
|
|
string pos = "Es wurden " + suche.AllPositions.Count + " Übereinstimmungen\r\nan diesen Positionen gefunden:";
|
|
foreach (var item in suche.AllPositions)
|
|
{
|
|
pos = String.Concat(pos, "\r\n", item);
|
|
}
|
|
pos = String.Concat(pos, "\r\nAktuelle Cursor Position: ", suche.CurrentCursorPosition, "\r\nEs ist ein Fehler passiert: ", suche.Error.ToString());
|
|
MessageBox.Show(pos);
|
|
for (int i = 0; i < 6; i++)
|
|
{
|
|
suche.NextPosition();
|
|
MessageBox.Show(pos + "\r\n\r\nIndex erhöhen\r\nIndex: " + suche.Index + "\r\nCursorposition: " + suche.CurrentCursorPosition);
|
|
}
|
|
for (int i = 0; i < 8; i++)
|
|
{
|
|
suche.PreviousPosition();
|
|
MessageBox.Show(pos + "\r\n\r\nIndex veringern\r\nIndex: " + suche.Index + "\r\nCursorposition: " + suche.CurrentCursorPosition);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void textBoxSearchstring_TextChanged(object sender, EventArgs e)
|
|
{
|
|
AllPositions.Clear();
|
|
}
|
|
|
|
private void checkBoxCase_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
if (!String.IsNullOrEmpty(textBoxSearchstring.Text))
|
|
{
|
|
Search();
|
|
}
|
|
}
|
|
|
|
private void checkBoxFullword_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
if (!String.IsNullOrEmpty(textBoxSearchstring.Text))
|
|
{
|
|
Search();
|
|
}
|
|
}
|
|
|
|
private void buttonPrevious_Click(object sender, EventArgs e)
|
|
{
|
|
Previous();
|
|
}
|
|
|
|
private void buttonNext_Click(object sender, EventArgs e)
|
|
{
|
|
Next();
|
|
}
|
|
|
|
private void textBoxReplaceText_KeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.KeyCode == Keys.Enter)
|
|
{
|
|
textBoxText.Focus();
|
|
}
|
|
}
|
|
|
|
private void buttonReplace_Click(object sender, EventArgs e)
|
|
{
|
|
Replace();
|
|
}
|
|
|
|
private void buttonReplaceAll_Click(object sender, EventArgs e)
|
|
{
|
|
ReplaceAll();
|
|
}
|
|
}
|
|
}
|