405 lines
14 KiB
C#
405 lines
14 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.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Eugen.ESystem.Windows.Forms
|
|
{
|
|
public partial class ExtendedInfoBox : Form
|
|
{
|
|
public ExtendedInfoBox(string windowTitle, Icons icon, string title, string headerRow1, string headerRow2, string headerRow3, string text, bool export)
|
|
{
|
|
InitializeComponent();
|
|
|
|
checkBoxCaseSensitive.Checked = false;
|
|
SetContent(windowTitle, icon, title, headerRow1, headerRow2, headerRow3, text, export); //Fensterinhalt setzen
|
|
}
|
|
|
|
/// <summary>
|
|
/// Available Icons
|
|
/// </summary>
|
|
public enum Icons
|
|
{
|
|
Construction,
|
|
Email,
|
|
Error,
|
|
Information,
|
|
Instruction,
|
|
Question,
|
|
Telephon,
|
|
Tips,
|
|
Warning
|
|
}
|
|
|
|
string findText = "";
|
|
string replaceText = "";
|
|
List<int> index = new List<int>();
|
|
int tempIndex = -1;
|
|
|
|
private void buttonClose1_Click(object sender, EventArgs e)
|
|
{
|
|
this.Close();
|
|
}
|
|
|
|
private void buttonClose2_Click(object sender, EventArgs e)
|
|
{
|
|
this.Close();
|
|
}
|
|
|
|
private void buttonExport_Click(object sender, EventArgs e)
|
|
{
|
|
//Speichert den Inhalt der TextBox oder den selektierten Bereich daraus
|
|
saveFileDialog.Filter = Language.dialog001;
|
|
saveFileDialog.ShowDialog();
|
|
}
|
|
|
|
private void saveFileDialog_FileOk(object sender, CancelEventArgs e)
|
|
{
|
|
string fileName = saveFileDialog.FileName;
|
|
string text = ExportText();
|
|
|
|
try
|
|
{
|
|
File.WriteAllText(fileName, text); //Schreibt den Text in eine Datei
|
|
}
|
|
catch (Exception)
|
|
{
|
|
MessageBox.Show(Language.message001,
|
|
Language.messageHead001,
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error,
|
|
MessageBoxDefaultButton.Button1);
|
|
}
|
|
}
|
|
|
|
private void textBoxFind_KeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.KeyCode == Keys.Return)
|
|
{
|
|
//Wenn 'Return' gedrückt wird die Anzahl der Vorkommen ermitteln
|
|
if (checkBoxCaseSensitive.Checked)
|
|
{
|
|
index = FindPositionsOfText(findText, textBox.Text);
|
|
}
|
|
else
|
|
{
|
|
index = FindPositionsOfText(findText.ToLower(), textBox.Text.ToLower());
|
|
}
|
|
|
|
textBox.Focus(); //Den Focus auf die Textbox setzen
|
|
textBox.Select(index[0], findText.Length); //Das erste Vorkommen selektieren
|
|
}
|
|
}
|
|
|
|
private void textBoxFind_TextChanged(object sender, EventArgs e)
|
|
{
|
|
findText = textBoxFind.Text; //Dieser Text soll gesucht werden
|
|
|
|
//Nur ausführen wenn 'findText' nicht "" ist, weil das Programm sonst in einer Endlosschleife hängt
|
|
if (findText != "")
|
|
{
|
|
if (checkBoxCaseSensitive.Checked)
|
|
{
|
|
index = FindPositionsOfText(findText, textBox.Text);
|
|
}
|
|
else
|
|
{
|
|
index = FindPositionsOfText(findText.ToLower(), textBox.Text.ToLower());
|
|
}
|
|
}
|
|
tempIndex = 0; //Wird gesetzt weil 'Return' gedrückt wurde
|
|
|
|
try
|
|
{
|
|
//Fenster zur selektierten Position scrollen
|
|
textBox.SelectionStart = index[0];
|
|
textBox.ScrollToCaret();
|
|
}
|
|
catch
|
|
{
|
|
textBoxFind.Text = findText.Remove(findText.Length - 1);
|
|
textBoxFind.Refresh();
|
|
}
|
|
}
|
|
|
|
private void buttonPrevious_Click(object sender, EventArgs e)
|
|
{
|
|
if (tempIndex == -1)
|
|
{
|
|
//Wenn bei der nicht 'Return' sondern gleich 'Nächster' gedrückt wurde
|
|
tempIndex = index.Count - 1;
|
|
}
|
|
else
|
|
{
|
|
if (tempIndex > 0)
|
|
{
|
|
tempIndex--;
|
|
|
|
//Fenster zur selektierten Position scrollen
|
|
textBox.SelectionStart = index[tempIndex];
|
|
textBox.ScrollToCaret();
|
|
}
|
|
else
|
|
{
|
|
tempIndex = index.Count - 1;
|
|
}
|
|
}
|
|
|
|
textBox.Focus();
|
|
textBox.DeselectAll();
|
|
if (tempIndex > 0)
|
|
{
|
|
textBox.Select(index[tempIndex], findText.Length);
|
|
}
|
|
}
|
|
|
|
private void buttonNext_Click(object sender, EventArgs e)
|
|
{
|
|
if (tempIndex == -1)
|
|
{
|
|
//Wenn bei der Eingabe nicht 'Return' sondern gleich 'Nächster' gedrückt wurde
|
|
tempIndex = 0;
|
|
}
|
|
else
|
|
{
|
|
if (tempIndex < index.Count - 1)
|
|
{
|
|
tempIndex++;
|
|
|
|
//Fenster zur selektierten Position scrollen
|
|
textBox.SelectionStart = index[tempIndex];
|
|
textBox.ScrollToCaret();
|
|
}
|
|
else
|
|
{
|
|
tempIndex = 0;
|
|
}
|
|
}
|
|
|
|
textBox.Focus();
|
|
textBox.DeselectAll();
|
|
if (tempIndex > 0)
|
|
{
|
|
textBox.Select(index[tempIndex], findText.Length);
|
|
}
|
|
}
|
|
|
|
private void checkBoxCaseSensitive_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
//Die Anzahl der Vorkommen ermitteln
|
|
if (checkBoxCaseSensitive.Checked)
|
|
{
|
|
index = FindPositionsOfText(findText, textBox.Text);
|
|
}
|
|
else
|
|
{
|
|
index = FindPositionsOfText(findText.ToLower(), textBox.Text.ToLower());
|
|
}
|
|
|
|
tempIndex = 0; //Zurücksetzen, weil in diesem Fall nicht 'Return' gedrückt wurde
|
|
}
|
|
|
|
private void textBoxReplace_KeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.KeyCode == Keys.Return)
|
|
{
|
|
//Wenn 'Return' gedrückt wird die Anzahl der Vorkommen ermitteln
|
|
if (checkBoxCaseSensitive.Checked)
|
|
{
|
|
index = FindPositionsOfText(findText, textBox.Text);
|
|
}
|
|
else
|
|
{
|
|
index = FindPositionsOfText(findText.ToLower(), textBox.Text.ToLower());
|
|
}
|
|
|
|
textBox.Focus(); //Den Focus auf die Textbox setzen
|
|
textBox.Select(index[0], findText.Length); //Das erste Vorkommen selektieren
|
|
}
|
|
}
|
|
|
|
private void textBoxReplace_TextChanged(object sender, EventArgs e)
|
|
{
|
|
findText = textBoxFind.Text; //Dieser Text soll gesucht werden
|
|
replaceText = textBoxReplace.Text; //Durch diesen soll ersetzt werden
|
|
|
|
if (checkBoxCaseSensitive.Checked)
|
|
{
|
|
index = FindPositionsOfText(findText, textBox.Text);
|
|
}
|
|
else
|
|
{
|
|
index = FindPositionsOfText(findText.ToLower(), textBox.Text.ToLower());
|
|
}
|
|
tempIndex = 0; //Wird gesetzt weil 'Return' gedrückt wurde
|
|
}
|
|
|
|
private void buttonReplace_Click(object sender, EventArgs e)
|
|
{
|
|
if (index.Count > 0)
|
|
{
|
|
//Nur abarbeiten wenn ein Suchtext gefunden wurde
|
|
textBox.Focus();
|
|
textBox.DeselectAll();
|
|
textBox.Select(index[tempIndex], findText.Length);
|
|
textBox.SelectedText = replaceText;
|
|
|
|
if (checkBoxCaseSensitive.Checked)
|
|
{
|
|
index = FindPositionsOfText(findText, textBox.Text);
|
|
}
|
|
else
|
|
{
|
|
index = FindPositionsOfText(findText.ToLower(), textBox.Text.ToLower());
|
|
}
|
|
tempIndex = 0;
|
|
|
|
textBox.Focus();
|
|
textBox.DeselectAll();
|
|
if (index.Count > 0)
|
|
{
|
|
//Selektieren nur möglich wenn es noch einen Suchtext gibt
|
|
textBox.Select(index[tempIndex], findText.Length);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void buttonReplaceAll_Click(object sender, EventArgs e)
|
|
{
|
|
while (index.Count > 0)
|
|
{
|
|
//Nur abarbeiten wenn ein Suchtext gefunden wurde
|
|
textBox.Focus();
|
|
textBox.DeselectAll();
|
|
textBox.Select(index[tempIndex], findText.Length);
|
|
textBox.SelectedText = replaceText;
|
|
|
|
if (checkBoxCaseSensitive.Checked)
|
|
{
|
|
index = FindPositionsOfText(findText, textBox.Text);
|
|
}
|
|
else
|
|
{
|
|
index = FindPositionsOfText(findText.ToLower(), textBox.Text.ToLower());
|
|
}
|
|
tempIndex = 0;
|
|
|
|
textBox.Focus();
|
|
textBox.DeselectAll();
|
|
if (index.Count > 0)
|
|
{
|
|
//Selektieren nur möglich wenn es noch einen Suchtext gibt
|
|
textBox.Select(index[tempIndex], findText.Length);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SetContent(string windowTitle, Icons icon, string title, string headerRow1, string headerRow2, string headerRow3, string text, bool export)
|
|
{
|
|
//Export Button freischalten oder nicht
|
|
if (export)
|
|
{
|
|
buttonExport.Visible = true;
|
|
buttonClose1.Visible = true;
|
|
buttonClose2.Visible = false;
|
|
}
|
|
else
|
|
{
|
|
buttonExport.Visible = false;
|
|
buttonClose1.Visible = false;
|
|
buttonClose2.Visible = true;
|
|
}
|
|
|
|
//Icon setzen
|
|
switch (icon)
|
|
{
|
|
case Icons.Construction:
|
|
this.pictureBox.Image = global::Eugen.ESystem.Windows.Forms.Pictures.Construction;
|
|
break;
|
|
case Icons.Email:
|
|
this.pictureBox.Image = global::Eugen.ESystem.Windows.Forms.Pictures.Email;
|
|
break;
|
|
case Icons.Error:
|
|
this.pictureBox.Image = global::Eugen.ESystem.Windows.Forms.Pictures.Error;
|
|
break;
|
|
case Icons.Information:
|
|
this.pictureBox.Image = global::Eugen.ESystem.Windows.Forms.Pictures.Information;
|
|
break;
|
|
case Icons.Instruction:
|
|
this.pictureBox.Image = global::Eugen.ESystem.Windows.Forms.Pictures.Instruction;
|
|
break;
|
|
case Icons.Question:
|
|
this.pictureBox.Image = global::Eugen.ESystem.Windows.Forms.Pictures.Question;
|
|
break;
|
|
case Icons.Telephon:
|
|
this.pictureBox.Image = global::Eugen.ESystem.Windows.Forms.Pictures.Telephone;
|
|
break;
|
|
case Icons.Tips:
|
|
this.pictureBox.Image = global::Eugen.ESystem.Windows.Forms.Pictures.Tips;
|
|
break;
|
|
case Icons.Warning:
|
|
this.pictureBox.Image = global::Eugen.ESystem.Windows.Forms.Pictures.Warning;
|
|
break;
|
|
default:
|
|
this.pictureBox.Image = global::Eugen.ESystem.Windows.Forms.Pictures.Information;
|
|
break;
|
|
}
|
|
|
|
this.Text = windowTitle; //Fenster Titel
|
|
labelTitle.Text = title; //Titel schreiben
|
|
labelHeaderRow1.Text = headerRow1; //Header Zeile 1 schreiben
|
|
labelHeaderRow2.Text = headerRow2; //Header Zeile 2 schreiben
|
|
labelHeaderRow3.Text = headerRow3; //Header Zeile 3 schreiben
|
|
textBox.Text = text; //Text schreiben
|
|
}
|
|
|
|
private string ExportText()
|
|
{
|
|
if (textBox.SelectionLength > 0)
|
|
{
|
|
return textBox.SelectedText; //Gibt den selektierten Text zurück
|
|
}
|
|
else
|
|
{
|
|
return FixLineLenght.Format(textBox.Text, 80); //Gibt den ganzen Text formatiert zurück
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets a user specific window size.
|
|
/// </summary>
|
|
/// <param name="width">A value greater than 400.</param>
|
|
/// <param name="height">A value greater than 300.</param>
|
|
public void SetSize(int width, int height)
|
|
{
|
|
this.ClientSize = new System.Drawing.Size(width - 8, height - 31);
|
|
}
|
|
|
|
private List<int> FindPositionsOfText(string textToFind, string textToSearch)
|
|
{
|
|
//Wenn der Suchstring noch leer ist, dann die Methode verlassen, weil sonst endlos gesucht wird
|
|
if (String.IsNullOrEmpty(textToFind))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
List<int> result = new List<int>();
|
|
|
|
int lastIndex = 0;
|
|
while (textToSearch.IndexOf(textToFind, lastIndex) > -1)
|
|
{
|
|
result.Add(textToSearch.IndexOf(textToFind, lastIndex));
|
|
lastIndex = result[result.Count - 1] + textToFind.Length;
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|