507 lines
17 KiB
C#
507 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Drawing;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Eugen.ESystem.Windows.Forms
|
|
{
|
|
public partial class Replace: UserControl
|
|
{
|
|
#region Version und Copyright
|
|
// Version und Copyright
|
|
string dllName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen
|
|
string dllVersion = 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 = "";
|
|
if (((AssemblyCopyrightAttribute)attributes[0]).Copyright.Contains("Copyright © "))
|
|
{
|
|
startYear = ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace("Copyright © ", "");
|
|
while (startYear.StartsWith(" "))
|
|
{
|
|
startYear = startYear.Remove(0, 1);
|
|
}
|
|
startYear = startYear.Remove(startYear.IndexOf(" "));
|
|
return startYear;
|
|
}
|
|
else
|
|
{
|
|
DateTime dtn = DateTime.Now;
|
|
return dtn.Year.ToString();
|
|
}
|
|
}
|
|
|
|
private static string GenerateCopyright()
|
|
{
|
|
string startYear = StartYear();
|
|
string currentYear = CurrentYear();
|
|
|
|
if (startYear == currentYear)
|
|
{
|
|
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
|
|
}
|
|
else
|
|
{
|
|
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace(startYear, String.Concat(startYear, "-", currentYear));
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region DLL-Info
|
|
/// <summary>
|
|
/// Contains the name of the program file
|
|
/// </summary>
|
|
public static string DllName { set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the version of the program file
|
|
/// </summary>
|
|
public static string DllVersion { set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the copyright notice
|
|
/// </summary>
|
|
public static string Copyright { set; get; }
|
|
|
|
private void SetDllInfo()
|
|
{
|
|
//Name, Version und Copyright setzen
|
|
DllName = dllName;
|
|
DllVersion = dllVersion;
|
|
Copyright = copyright;
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Replaces a string with another
|
|
/// </summary>
|
|
public Replace()
|
|
{
|
|
InitializeComponent();
|
|
SetDllInfo();
|
|
search.SetSearchString();
|
|
MoreMatch = true;
|
|
}
|
|
|
|
#region GlobaleVariablen
|
|
protected string SearchText { set; get; }
|
|
protected string SearchString { set; get; }
|
|
protected int SearchStringLength { set; get; }
|
|
protected int Index { set; get; }
|
|
/// <summary>
|
|
/// Contains the current cursor position
|
|
/// </summary>
|
|
protected int CurrentCursorPosition { set; get; }
|
|
/// <summary>
|
|
/// Contains a list with all found positions
|
|
/// </summary>
|
|
protected List<int> AllPositions { set; get; }
|
|
/// <summary>
|
|
/// String with which the search string should be replaced.
|
|
/// </summary>
|
|
protected string SubstituteString { set; get; }
|
|
/// <summary>
|
|
/// Tells you if there are still matches available
|
|
/// </summary>
|
|
protected static bool MoreMatch { set; get; }
|
|
///// <summary>
|
|
///// Is true if an error has occurred
|
|
///// </summary>
|
|
//public bool Error { private set; get; }
|
|
#endregion
|
|
|
|
#region Public Methodes
|
|
/// <summary>
|
|
/// Sets the text to be searched.
|
|
/// </summary>
|
|
/// <param name="searchText">A valid string.</param>
|
|
public void SetSearchText(string searchText)
|
|
{
|
|
SearchText = searchText;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the text to be searched.
|
|
/// </summary>
|
|
/// <returns>Returns a string.</returns>
|
|
public string GetSearchText()
|
|
{
|
|
return SearchText;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the search string.
|
|
/// </summary>
|
|
/// <param name="searchString">A valid string.</param>
|
|
public void SetSearchString(string searchString)
|
|
{
|
|
SearchString = searchString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the search string.
|
|
/// </summary>
|
|
/// <returns>Returns a string.</returns>
|
|
public string GetSearchString()
|
|
{
|
|
return SearchString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the Search string length.
|
|
/// </summary>
|
|
/// <param name="searchStringLength">A valid int value.</param>
|
|
public void SetSearchStringLength(int searchStringLength)
|
|
{
|
|
SearchStringLength = searchStringLength;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the Search string length.
|
|
/// </summary>
|
|
/// <returns>Returns a int value.</returns>
|
|
public int GetSearchStringLength()
|
|
{
|
|
return SearchStringLength;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the position for a found value in the array
|
|
/// </summary>
|
|
/// <param name="index">A valid int value. Must be smaller / equal to the maximum number of elements.</param>
|
|
public void SetIndex(int index)
|
|
{
|
|
Index = index;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the position for a found value in the array
|
|
/// </summary>
|
|
/// <returns>Returns a int value.</returns>
|
|
public int GetIndex()
|
|
{
|
|
return Index;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the current cursor position in the search text.
|
|
/// </summary>
|
|
/// <param name="currentCursorPosition">A valid int value.</param>
|
|
public void SetCurrentCursorPosition(int currentCursorPosition)
|
|
{
|
|
CurrentCursorPosition = currentCursorPosition;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the current cursor position in the search text.
|
|
/// </summary>
|
|
/// <returns>Returns a int value.</returns>
|
|
public int GetCurrentCursorPosition()
|
|
{
|
|
return CurrentCursorPosition;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns all positions where the search string was found in the search text.
|
|
/// </summary>
|
|
/// <returns>Returns a int list.</returns>
|
|
public List<int> GetAllPositions()
|
|
{
|
|
return AllPositions;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the substitute string.
|
|
/// </summary>
|
|
/// <param name="substituteString">A valid string.</param>
|
|
public void SetSubstituteString(string substituteString)
|
|
{
|
|
SubstituteString = substituteString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the substitute string.
|
|
/// </summary>
|
|
/// <returns>Returns a string.</returns>
|
|
public string GetSubstituteString()
|
|
{
|
|
return SubstituteString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets whether there is no further hit.
|
|
/// </summary>
|
|
/// <param name="moreMatch">Is true or false.</param>
|
|
public void SetMoreMatch(bool moreMatch)
|
|
{
|
|
MoreMatch = moreMatch;
|
|
}
|
|
#endregion
|
|
|
|
#region Privat Methodes
|
|
private void SetValues()
|
|
{
|
|
SearchText = search.GetSearchText();
|
|
SearchString = search.GetSearchString();
|
|
if (!String.IsNullOrEmpty(SearchString) && search.GetAllPositionsCount() > 0)
|
|
{
|
|
search.SetSearchStringLength(SearchString.Length);
|
|
SearchStringLength = search.GetSearchStringLength();
|
|
}
|
|
Index = search.GetIndex();
|
|
}
|
|
|
|
private void SetValuesNoMatch()
|
|
{
|
|
SearchText = "";
|
|
SearchString = "";
|
|
Index = 0;
|
|
}
|
|
|
|
private bool IsInputOK()
|
|
{
|
|
if (!String.IsNullOrEmpty(search.GetSearchString()) && !String.IsNullOrEmpty(SubstituteString))
|
|
{
|
|
if (SubstituteString != textBoxSubstituteString.Text)
|
|
{
|
|
SubstituteString = textBoxSubstituteString.Text; //Wenn ein Wert in der Textbox steht, aber noch nicht Return/Enter gedrückt wurde
|
|
}
|
|
|
|
if (search.GetAllPositionsCount() > 0)
|
|
{
|
|
return true; //Sind beide Werte vorhanden und gleich, dann ist Alles OK
|
|
}
|
|
else
|
|
{
|
|
return false; //Wenn es keinen Suchstring mehr gibt
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!String.IsNullOrEmpty(search.GetSearchString()) & !String.IsNullOrEmpty(textBoxSubstituteString.Text))
|
|
{
|
|
SubstituteString = textBoxSubstituteString.Text; //Wenn ein Wert in der Textbox steht, aber noch nicht Return/Enter gedrückt wurde
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ReplaceStr()
|
|
{
|
|
if (MoreMatch)
|
|
{
|
|
SetValues();
|
|
}
|
|
else
|
|
{
|
|
SetValuesNoMatch();
|
|
}
|
|
|
|
if (IsInputOK())
|
|
{
|
|
try
|
|
{
|
|
if (MoreMatch)
|
|
{
|
|
//Den Text nach dem Tausch wieder zurückschreiben
|
|
search.SetSearchText(ReplaceString(search.GetSearchText(), search.GetSearchString(), SubstituteString, search.GetCurrentCursorPosition()));
|
|
search.SearchAndFindString();
|
|
OnEvent(search.GetCurrentCursorPosition(), search.GetSearchStringLength()); //Anzeige aktualisieren
|
|
}
|
|
}
|
|
catch (IncompleteInputException)
|
|
{
|
|
MessageBox.Show(AppResources.messagebox001text, AppResources.messagebox001caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
}
|
|
catch (StringNotMatchException)
|
|
{
|
|
search.ClearSearchString();
|
|
OnEvent(0, 0); //Anzeige aktualisieren
|
|
MoreMatch = false;
|
|
MessageBox.Show(AppResources.messagebox002text, AppResources.messagebox002caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (search.GetAllPositionsCount() == 0)
|
|
{
|
|
throw new StringNotMatchException();
|
|
}
|
|
else
|
|
{
|
|
throw new IncompleteInputException();
|
|
}
|
|
}
|
|
}
|
|
|
|
private string ReplaceString(string searchText, string searchString, string textBoxReplacementString, int currentCursorPosition)
|
|
{
|
|
return searchText.Remove(currentCursorPosition, searchString.Length).Insert(currentCursorPosition, textBoxReplacementString);
|
|
}
|
|
|
|
private void ReplaceAll()
|
|
{
|
|
List<int> newList = new List<int>();
|
|
newList = search.GetAllPositions();
|
|
newList.Reverse();
|
|
|
|
if (MoreMatch)
|
|
{
|
|
foreach (var item in newList)
|
|
{
|
|
//Das Ersetzen von hinten nach vorne durchführen. So sind die vorab ermittelten Positionen immer richtig
|
|
search.SetSearchText(ReplaceString(search.GetSearchText(), search.GetSearchString(), SubstituteString, item));
|
|
}
|
|
MoreMatch = false;
|
|
}
|
|
else
|
|
{
|
|
if (search.GetAllPositionsCount() == 0)
|
|
{
|
|
throw new StringNotMatchException();
|
|
}
|
|
else
|
|
{
|
|
throw new IncompleteInputException();
|
|
}
|
|
}
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region Event
|
|
// Der Delegat muß die gleiche Signatur aufweisen wie die Eventhandler-Methode.
|
|
public delegate void EventDelegate(int currentCursorPosition, int searchStringLenght);
|
|
|
|
// Das Event-Objekt ist vom Typ dieses Delegaten.
|
|
public event EventDelegate StringFound;
|
|
|
|
public void OnEvent(int currentCursorPosition, int searchStringLength)
|
|
{
|
|
// Prüft ob das Event überhaupt einen Abonnenten hat.
|
|
StringFound?.Invoke(currentCursorPosition, searchStringLength);
|
|
}
|
|
#endregion
|
|
|
|
Search search = new Search();
|
|
|
|
private void buttonReplace_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
ReplaceStr();
|
|
}
|
|
catch (IncompleteInputException)
|
|
{
|
|
MessageBox.Show(AppResources.messagebox001text, AppResources.messagebox001caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
}
|
|
catch (StringNotMatchException)
|
|
{
|
|
search.ClearSearchString();
|
|
MessageBox.Show(AppResources.messagebox002text, AppResources.messagebox002caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
}
|
|
|
|
private void buttonReplaceAll_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (MoreMatch)
|
|
{
|
|
if (IsInputOK())
|
|
{
|
|
ReplaceAll();
|
|
OnEvent(0, 0); //Anzeige aktualisieren
|
|
}
|
|
else
|
|
{
|
|
if (search.GetAllPositionsCount() == 0)
|
|
{
|
|
throw new StringNotMatchException();
|
|
}
|
|
else
|
|
{
|
|
throw new IncompleteInputException();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw new StringNotMatchException();
|
|
}
|
|
}
|
|
catch (IncompleteInputException)
|
|
{
|
|
MessageBox.Show(AppResources.messagebox001text, AppResources.messagebox001caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
}
|
|
catch (StringNotMatchException)
|
|
{
|
|
search.ClearSearchString();
|
|
MessageBox.Show(AppResources.messagebox002text, AppResources.messagebox002caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
}
|
|
|
|
private void textBoxSubstituteString_KeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.KeyCode == Keys.Enter)
|
|
{
|
|
if (!String.IsNullOrEmpty(search.GetSearchString()))
|
|
{
|
|
MoreMatch = true;
|
|
SetValues();
|
|
|
|
try
|
|
{
|
|
SubstituteString = textBoxSubstituteString.Text;
|
|
search.SearchAndFindString();
|
|
OnEvent(search.GetCurrentCursorPosition(), search.GetSearchStringLength()); //Anzeige aktualisieren
|
|
}
|
|
catch (StringNotMatchException)
|
|
{
|
|
MessageBox.Show(AppResources.messagebox002text, AppResources.messagebox002caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void textBoxSubstituteString_Click(object sender, EventArgs e)
|
|
{
|
|
SetValues();
|
|
OnEvent(CurrentCursorPosition, SearchStringLength);
|
|
textBoxSubstituteString.Focus();
|
|
}
|
|
}
|
|
}
|