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
///
/// Contains the name of the program file
///
public static string DllName { set; get; }
///
/// Contains the version of the program file
///
public static string DllVersion { set; get; }
///
/// Contains the copyright notice
///
public static string Copyright { set; get; }
private void SetDllInfo()
{
//Name, Version und Copyright setzen
DllName = dllName;
DllVersion = dllVersion;
Copyright = copyright;
}
#endregion
///
/// Replaces a string with another
///
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; }
///
/// Contains the current cursor position
///
protected int CurrentCursorPosition { set; get; }
///
/// Contains a list with all found positions
///
protected List AllPositions { set; get; }
///
/// String with which the search string should be replaced.
///
protected string SubstituteString { set; get; }
///
/// Tells you if there are still matches available
///
protected static bool MoreMatch { set; get; }
/////
///// Is true if an error has occurred
/////
//public bool Error { private set; get; }
#endregion
#region Public Methodes
///
/// Sets the text to be searched.
///
/// A valid string.
public void SetSearchText(string searchText)
{
SearchText = searchText;
}
///
/// Get the text to be searched.
///
/// Returns a string.
public string GetSearchText()
{
return SearchText;
}
///
/// Sets the search string.
///
/// A valid string.
public void SetSearchString(string searchString)
{
SearchString = searchString;
}
///
/// Get the search string.
///
/// Returns a string.
public string GetSearchString()
{
return SearchString;
}
///
/// Sets the Search string length.
///
/// A valid int value.
public void SetSearchStringLength(int searchStringLength)
{
SearchStringLength = searchStringLength;
}
///
/// Get the Search string length.
///
/// Returns a int value.
public int GetSearchStringLength()
{
return SearchStringLength;
}
///
/// Sets the position for a found value in the array
///
/// A valid int value. Must be smaller / equal to the maximum number of elements.
public void SetIndex(int index)
{
Index = index;
}
///
/// Get the position for a found value in the array
///
/// Returns a int value.
public int GetIndex()
{
return Index;
}
///
/// Sets the current cursor position in the search text.
///
/// A valid int value.
public void SetCurrentCursorPosition(int currentCursorPosition)
{
CurrentCursorPosition = currentCursorPosition;
}
///
/// Get the current cursor position in the search text.
///
/// Returns a int value.
public int GetCurrentCursorPosition()
{
return CurrentCursorPosition;
}
///
/// Returns all positions where the search string was found in the search text.
///
/// Returns a int list.
public List GetAllPositions()
{
return AllPositions;
}
///
/// Sets the substitute string.
///
/// A valid string.
public void SetSubstituteString(string substituteString)
{
SubstituteString = substituteString;
}
///
/// Get the substitute string.
///
/// Returns a string.
public string GetSubstituteString()
{
return SubstituteString;
}
///
/// Sets whether there is no further hit.
///
/// Is true or false.
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 newList = new List();
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();
}
}
}