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
///
/// Contains the name of the program file
///
public static string ProgramName { set; get; }
///
/// Contains the version of the program file
///
public static string ProgramVersion { set; get; }
///
/// Contains the copyright notice
///
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;
}
Search search = new Search();
private void SearchAndSelect()
{
try
{
search.StartSearch(textBoxText.Text, textBoxSearchstring.Text, checkBoxCase.Checked, checkBoxFullword.Checked);
if (!search.Error)
{
SelectSearchString(search.CurrentCursorPosition, textBoxSearchstring.Text.Length);
}
}
catch (StringNotMatchException)
{
MessageBox.Show("Suchstring wurde nicht gefunden!");
}
catch (Exception)
{
MessageBox.Show("Unbekannter Fehler passiert!");
}
}
private void SelectSearchString(int position, int length)
{
textBoxText.Focus();
textBoxText.Select(position, length);
textBoxText.Refresh();
}
private void NextPosition()
{
search.NextPosition();
SelectSearchString(search.CurrentCursorPosition, textBoxSearchstring.Text.Length);
}
private void PreviousPosition()
{
search.PreviousPosition();
SelectSearchString(search.CurrentCursorPosition, textBoxSearchstring.Text.Length);
}
private void SelectAll()
{
//TODO: Select All
}
Replace replace = new Replace();
private void ReplaceString()
{
textBoxText.Text = replace.ReplaceString(textBoxText.Text, textBoxSearchstring.Text, textBoxReplacementString.Text, search.CurrentCursorPosition);
textBoxText.Refresh();
SearchAndSelect();
}
private void ReplaceAll()
{
List newList = new List();
newList = search.AllPositions;
newList.Reverse();
foreach (var item in newList)
{
textBoxText.Text = replace.ReplaceString(textBoxText.Text, textBoxSearchstring.Text, textBoxReplacementString.Text, item);
textBoxText.Refresh();
}
}
private void textBoxSearchstring_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SearchAndSelect();
}
}
private void textBoxSearchstring_TextChanged(object sender, EventArgs e)
{
//TODO: Nichts tun
}
private void checkBoxCase_CheckedChanged(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(textBoxSearchstring.Text))
{
SearchAndSelect();
}
}
private void checkBoxFullword_CheckedChanged(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(textBoxSearchstring.Text))
{
SearchAndSelect();
}
}
private void buttonPrevious_Click(object sender, EventArgs e)
{
PreviousPosition();
}
private void buttonNext_Click(object sender, EventArgs e)
{
NextPosition();
}
private void buttonSelectAll_Click(object sender, EventArgs e)
{
SelectAll();
}
private void textBoxReplaceText_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
textBoxText.Focus();
}
}
private void buttonReplace_Click(object sender, EventArgs e)
{
ReplaceString();
}
private void buttonReplaceAll_Click(object sender, EventArgs e)
{
ReplaceAll();
}
}
}