using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Reflection; using AppRes = Eugen.ESystem.Windows.Forms.Properties.AppResources; using System.Xml.Linq; namespace Eugen.ESystem.Windows.Forms { public partial class TripleValueControl : UserControl { #region Version und Copyright // Keys und Copyright static string dllName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen static string dllVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false); static 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 dll file. /// public static string DllName { private set; get; } /// /// Contains the version of the dll file. /// public static string DllVersion { private set; get; } /// /// Contains the copyright notice. /// public static string Copyright { private set; get; } /// /// Contains the name-, version- and copyright-information of the dll file. /// public static string[] DllInfo { private set; get; } private static void SetDllInfo() { //Name, Keys und Copyright setzen DllName = dllName; DllVersion = dllVersion; Copyright = copyright; } #endregion //Prüft ob der Initialisierungsprozess läuft. //Nach der Initialisierung wird der Wert auf 'true' gesetzt. bool initialized = false; static TripleValueControl() { SetDllInfo(); //Name, Keys und Copyright der DLL setzen } /// /// Creates a control with which the values in an XML file can be edited /// public TripleValueControl() { InitializeComponent(); //Ereignisse abbonieren controlPanelControl.ButtonUpClick += DoMoveUp; controlPanelControl.ButtonDownClick += DoMoveDown; controlPanelControl.ButtonAddSeparatorClick += DoAddSeperator; controlPanelControl.CheckboxEncryptCheck += DoEncrypt; controlPanelControl.CheckboxDefaultCheck += DoDefault; //Das Aussehen des Controlls setzen InitializeUserControl(); initialized = true; //Initialisierungsroutine wurde durchlaufen InitialDirectory = @"C:\"; Topic = "Topic"; StandardValue = 0; } #region Public values /// /// The password used for encryption. An empty value disables the Encrypt values switch /// public string Password { get { return password; } set { password = value; controlPanelControl.CheckBoxEncryptStatusSelected = !String.IsNullOrEmpty(Password); } } /// /// Defines or read the initial directory /// public string InitialDirectory { set; get; } /// /// XML file name. Must be specified mandatory /// public string XMLfileName { set; get; } /// /// Adjusts the labeling of the values control. An empty value forces the default labeling /// public string ValueControlName { get { return groupBoxValueControl.Text; } set { groupBoxValueControl.Text = value; groupBoxValueControl.Refresh(); } } /// /// Defines the Topic. An empty value forces the default labeling /// public string Topic { set; get; } /// /// Adjusts the labeling of the group designation. An empty value forces the default labeling /// public string GroupName { get { return labelGroup.Text; } set { labelGroup.Text = value; } } /// /// Adjusts the name of the group attribute used in the XML file. Required by the Load function /// public string Group { set; get; } /// /// Adjusts the name of the key attribute used in the XML file. Required by the Load function /// public string Key { set; get; } /// /// Adjusts the labeling of the table header designation for the key. An empty value forces the default labeling /// public string TableKeyName { get { return dataGridViewValues.Columns[0].HeaderText; } set { dataGridViewValues.Columns[0].HeaderText = value; } } /// /// Adjusts the labeling of the table header designation for the value. An empty value forces the default labeling /// public string TableValueName { get { return dataGridViewValues.Columns[1].HeaderText; } set { dataGridViewValues.Columns[1].HeaderText = value; } } /// /// Adjusts the labeling of the table header designation for the attribut. An empty value forces the default labeling /// public string TableAttributeName { get { return dataGridViewValues.Columns[2].HeaderText; } set { dataGridViewValues.Columns[2].HeaderText = value; } } /// /// Adjusts the width of the control. An empty value forces the default value /// public int DialogWidth { set; get; } /// /// Adjusts the height of the control. An empty value forces the default value /// public int DialogHeight { set; get; } /// /// Adjusts the width of the name column of the table. An empty value forces the default value /// public int TableKeyNameWidth { set; get; } #endregion #region Private values private string password; private bool alwaysEncrypted = false; private bool standardValueSet = false; private int selectedRow = 0; private int rowIndexColored = 0; private bool onlyRefresh = false; private int StandardValue { get; set; } #endregion private void InitializeUserControl() { //Erzeugt das erste Erscheinungsbild des Controlls controlPanelControl.ButtonUpAvailability = Availability.Enabled; //Button Up ist aktiviert controlPanelControl.ButtonUpVisibility = Visibility.Visible; //Button Up ist Sichtbar controlPanelControl.ButtonUpUseIcon = Figure.Used; //Für Button Up wird ein Bild anstelle des Textes verwendet controlPanelControl.ButtonDownAvailability = Availability.Enabled; //Button Down ist aktiviert controlPanelControl.ButtonDownVisibility = Visibility.Visible; //Button Down ist Sichtbar controlPanelControl.ButtonDownUseIcon = Figure.Used; //Für Button Down wird ein Bild anstelle des Textes verwendet controlPanelControl.ButtonAddSeparatorAvailability = Availability.Disabled; //Button Separator ist deaktiviert controlPanelControl.ButtonAddSeparatorVisibility = Visibility.Invisible; //Button Separator ist nicht Sichtbar controlPanelControl.ButtonAddSeparatorUseIcon = Figure.Used; //Für Button Separator wird ein Bild anstelle des Textes verwendet controlPanelControl.CheckBoxEncryptAvailability = Availability.Disabled; //CheckBox Encrypt ist deaktiviert controlPanelControl.CheckBoxEncryptVisibility = Visibility.Visible; //CheckBox Encrypt ist Sichtbar controlPanelControl.CheckBoxEncryptUseIcon = Figure.Used; //Für CheckBox Encrypt wird ein Bild anstelle des Textes verwendet controlPanelControl.CheckBoxEncryptStatusSelected = (bool)false; //Die CheckBox Encrypt ist nicht ausgewählt controlPanelControl.CheckBoxDefaultAvailability = Availability.Disabled; //CheckBox Default ist deaktiviert controlPanelControl.CheckBoxDefaultVisibility = Visibility.Invisible; //CheckBox Default ist Sichtbar controlPanelControl.CheckBoxDefaultUseIcon = Figure.Used; //Für CheckBox Default wird ein Bild anstelle des Textes verwendet controlPanelControl.CheckBoxDefaultStatusSelected = (bool)false; //Die CheckBox Default ist nicht ausgewählt controlPanelControl.SizeX = 90; //Größe des Controlls in X-Richtung controlPanelControl.SizeY = 127; //Größe des Controlls in Y-Richtung controlPanelControl.SetVisibility(); //Setzt die getroffenen Einstellungen } private void textBoxGroup_TextChanged(object sender, EventArgs e) { Group = textBoxGroup.Text; buttonSave.Enabled = (!String.IsNullOrEmpty(textBoxGroup.Text)); } private void textBoxGroup_Leave(object sender, EventArgs e) { if (!String.IsNullOrEmpty(textBoxGroup.Text)) { if (File.Exists(XMLfileName)) { LoadXML(); controlPanelControl.CheckBoxEncryptAvailability = Availability.Enabled; controlPanelControl.CheckBoxDefaultAvailability = Availability.Enabled; controlPanelControl.SetVisibility(); } else { MessageBox.Show(AppRes.msgBoxTxt001, AppRes.msgBoxCapt001, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } } private void buttonSave_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(XMLfileName)) { saveFileDialog.Title = AppRes.dlgSaveTitle; saveFileDialog.InitialDirectory = InitialDirectory; saveFileDialog.Filter = AppRes.dlgFilter; //"xml files (*.xml)|*.xml|All files (*.*)|*.*" saveFileDialog.FilterIndex = 1; saveFileDialog.RestoreDirectory = true; if (saveFileDialog.ShowDialog() == DialogResult.OK) { SaveXML(); } } else { SaveXML(); } } private void dataGridViewValues_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) { if (dataGridViewValues.Rows.Count != 0) { controlPanelControl.CheckBoxDefaultAvailability = Availability.Enabled; //Standardwert freischalten, sobald es einen Wert gibt } } private void dataGridViewValues_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Delete) { selectedRow = dataGridViewValues.SelectedCells[0].RowIndex + 1; //Wenn die Zeile gelöscht wird, dann die Zeilennummer merken } } private void dataGridViewValues_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e) { //Wenn die gelöschte Zeile den Standardwert enthielt, dann die WErte zurücksetzten, weil es keinen Standardwert mehr gibt if (selectedRow == rowIndexColored) { StandardValue = 0; //Es gibt keine ausgewählte Standardzeile rowIndexColored = 0; //Es gibt keine umgefärbte Zeile } } private void dataGridViewValues_RowEnter(object sender, DataGridViewCellEventArgs e) { try { if (StandardValue == dataGridViewValues.SelectedCells[0].RowIndex + 1) { //Die vorher ausgewählte Zeile abwählen controlPanelControl.CheckBoxDefaultStatusSelected = false; //Wenn die ausgewählte Zeile keinen Standardwert enthält, dann die CheckBox abwählen dataGridViewValues.Rows[rowIndexColored + 1].DefaultCellStyle.ForeColor = Color.Empty; //Die ausgewählte Zeile anwählen controlPanelControl.CheckBoxDefaultStatusSelected = true; //Wenn die ausgewählte Zeile den Standardwert enthält, dann die CheckBox anwählen controlPanelControl.SetVisibility(); dataGridViewValues.Rows[dataGridViewValues.SelectedCells[0].RowIndex].DefaultCellStyle.ForeColor = Color.LightSeaGreen; onlyRefresh = true; //Das Aussehen soll nur aktualisiert werden } else { controlPanelControl.CheckBoxDefaultStatusSelected = false; //Wenn die ausgewählte Zeile keinen Standardwert enthält, dann die CheckBox abwählen controlPanelControl.SetVisibility(); onlyRefresh = false; //Das Aussehen soll aktualisiert und der Standard neu gesewtzt werden } } catch { //Nur die falsche Reihe ausgewählt, z.B. die Kopfzeile oder die erste Zeile wurde gelöscht StandardValue = 0; //Wenn die erste Zeile der Standardwert war und gelöscht wurde, dann den Wert auf 0 setzen controlPanelControl.CheckBoxDefaultStatusSelected = false; //Wenn die ausgewählte Zeile keinen Standardwert enthält, dann die CheckBox abwählen controlPanelControl.SetVisibility(); } } public void SetBasicValues() { if (!String.IsNullOrEmpty(Group)) { textBoxGroup.Text = Group; } } private void SetDefault() { try { if (StandardValue == dataGridViewValues.SelectedCells[0].RowIndex + 1) { controlPanelControl.CheckBoxDefaultStatusSelected = true; //Wenn die ausgewählte Zeile den Standardwert enthält, dann die CheckBox anwählen controlPanelControl.SetVisibility(); dataGridViewValues.Rows[dataGridViewValues.SelectedCells[0].RowIndex].DefaultCellStyle.ForeColor = Color.LightSeaGreen; } else { controlPanelControl.CheckBoxDefaultStatusSelected = false; //Wenn die ausgewählte Zeile keinen Standardwert enthält, dann die CheckBox abwählen controlPanelControl.SetVisibility(); dataGridViewValues.Rows[rowIndexColored + 1].DefaultCellStyle.ForeColor = Color.Empty; } } catch (Exception) { //Nur die falsche Reihe ausgewählt, z.B. die Kopfzeile } } private void SetDialogAppearance() { if (DialogWidth != 0) { SetDialogWidth(DialogWidth); } if (DialogHeight != 0) { SetDialogHeight(DialogHeight); } if (TableKeyNameWidth != 0) { SetTableKeyNameWidth(TableKeyNameWidth); } } private void LoadXML() { //controlPanelControl.CheckBoxEncryptStatusSelected = false; //var loadXML = new LoadXML(); //loadXML.FileName = XMLfileName; //loadXML.Load(); //string[] values = new string[loadXML.Values.Length]; //values = loadXML.Values; //string[] attributes = new string[loadXML.Attributes.Length]; //attributes = loadXML.Attributes; //if (!String.IsNullOrEmpty(Group)) //{ // loadXML.Group = Group; //} //for (int i = 0; i < values.Length; i++) //{ // if (values[i].StartsWith("$$$$")) // { // values[i] = Decrypt(values[i], Password); // controlPanelControl.CheckBoxEncryptStatusSelected = true; //Wenn ein verschlüsseltes Element gefunden wurde, dann setzen // } // if (attributes[i].StartsWith("$$$$")) // { // attributes[i] = Decrypt(attributes[i], Password); // controlPanelControl.CheckBoxEncryptStatusSelected = true; //Wenn ein verschlüsseltes Element gefunden wurde, dann setzen // } // dataGridViewValues.Rows.Add(loadXML.Keys[i], values[i], attributes[i]); //} //dataGridViewValues.Refresh(); //textBoxGroup.Text = loadXML.Group; //Gruppe setzen controlPanelControl.CheckBoxEncryptStatusSelected = false; var loadXML = new LoadXML(); loadXML.FileName = XMLfileName; loadXML.Load(); string[] values = new string[loadXML.Values.Length]; values = loadXML.Values; string[] attributes = new string[loadXML.Attributes.Length]; attributes = loadXML.Attributes; if (!String.IsNullOrEmpty(Group)) { loadXML.Group = Group; } for (int i = 0; i < values.Length; i++) { if (values[i].StartsWith("$$$$")) { values[i] = Decrypt(values[i], Password); controlPanelControl.CheckBoxEncryptStatusSelected = true; //Wenn ein verschlüsseltes Element gefunden wurde, dann setzen } if (attributes[i].StartsWith("$$$$")) { attributes[i] = Decrypt(attributes[i], Password); controlPanelControl.CheckBoxEncryptStatusSelected = true; //Wenn ein verschlüsseltes Element gefunden wurde, dann setzen } dataGridViewValues.Rows.Add(loadXML.Keys[i], values[i], attributes[i]); } dataGridViewValues.Refresh(); textBoxGroup.Text = loadXML.Group; //Gruppe setzen } private void SaveXML() { if (!Directory.Exists(Path.GetDirectoryName(XMLfileName))) { Directory.CreateDirectory(Path.GetDirectoryName(XMLfileName)); //Wenn das Verzeichnis nicht existiert, dann erzeugen } int dgvLength = dataGridViewValues.RowCount - 1; string[] keys = new string[dgvLength]; string value = ""; string[] values = new string[dgvLength]; string attribute = ""; string[] attributes = new string[dgvLength]; for (int i = 0; i < dgvLength; i++) { if (controlPanelControl.CheckBoxEncryptStatusSelected) { value = Encrypt(dataGridViewValues[1, i].Value.ToString(), Password); //Verschlüsselt if (dataGridViewValues[2, i].Value == null) { attribute = Encrypt("", Password); //Verschlüsselt } else { attribute = Encrypt(dataGridViewValues[2, i].Value.ToString(), Password); //Verschlüsselt } } else { value = dataGridViewValues[1, i].Value.ToString(); //Unverschlüsselt if (dataGridViewValues[2, i].Value == null) { attribute = ""; } else { attribute = dataGridViewValues[2, i].Value.ToString(); //Unverschlüsselt } } keys[i] = dataGridViewValues[0, i].Value.ToString(); ; values[i] = value; attributes[i] = attribute; } //Speichern var saveXML = new SaveXML(); saveXML.FileName = XMLfileName; saveXML.Topic = "NX-Portal"; //z.B. "NX-Portal" saveXML.Group = textBoxGroup.Text; //z.B. "Tools" saveXML.Keys = keys; //z.B. "CleanUp" saveXML.Values = values; //z.B. "M:\ug\Tools\CleanUp\CleanUp.exe" saveXML.Attributes = attributes; //z.B. "-h" saveXML.Save(); } private string Encrypt(string clearText, string password) { var crypt = new Cryption(); return String.Concat("$$$$", crypt.EncryptString(clearText, password)); } private string Decrypt(string encryptedText, string password) { var crypt = new Cryption(); return crypt.DecryptString(encryptedText.Replace("$$$$", ""), password); } public void SetDialogWidth(int width) { if (width >= this.MinimumSize.Width && width <= this.MaximumSize.Width) { this.Width = width; int dif = width - 316; if (dif > 0) { dataGridViewValues.Columns[0].Width += dif / 2; } } } private void SetDialogHeight(int height) { if (height >= this.MinimumSize.Height && height <= this.MaximumSize.Height) { this.Height = height; } } private void SetTableKeyNameWidth(int width) { if (width >= ColumnName.MinimumWidth && width <= (this.Width - 151 - ColumnValue.MinimumWidth)) { ColumnName.Width = width; } } LoadXML loadXML = new LoadXML(); /// /// Gets the values stored in the XML file /// /// An string array public string[] GetValues() { controlPanelControl.CheckBoxEncryptStatusSelected = false; //var loadXML = new LoadXML(); loadXML.FileName = XMLfileName; loadXML.Topic = Topic; //loadXML.Key = Key; loadXML.Group = Group; loadXML.Load(); if (loadXML.Values == null) { return null; } else { string[] values = new string[loadXML.Values.Length]; values = loadXML.Values; for (int i = 0; i < values.Length; i++) { if (values[i] != null) { if (values[i].StartsWith("$$$$")) { values[i] = Decrypt(values[i], Password); } } } return values; } } #region Ereignisse //Die Ereignisse werden nach betätigen eines Elementes aufgerufen private void DoMoveUp(object source, EventArgs e) { //Weil sich die Zustände der Elemente während der Initialisierung ändern, //wird die Ausführung der folgenden Aktionen unterdrückt if (initialized) { if (dataGridViewValues.SelectedCells.Count > 0) { int index = dataGridViewValues.SelectedCells[0].OwningRow.Index; if (index > 0) { int col = dataGridViewValues.SelectedCells[0].OwningColumn.Index; DataGridViewRowCollection rows = dataGridViewValues.Rows; DataGridViewRow row = rows[index]; rows.Remove(row); rows.Insert(index - 1, row); dataGridViewValues.ClearSelection(); dataGridViewValues.Rows[index - 1].Cells[col].Selected = true; } } } } private void DoMoveDown(object source, EventArgs e) { //Weil sich die Zustände der Elemente während der Initialisierung ändern, //wird die Ausführung der folgenden Aktionen unterdrückt if (initialized) { if (dataGridViewValues.SelectedCells.Count > 0) { int index = dataGridViewValues.SelectedCells[0].OwningRow.Index; if (index < dataGridViewValues.Rows.Count - 2) { int col = dataGridViewValues.SelectedCells[0].OwningColumn.Index; DataGridViewRowCollection rows = dataGridViewValues.Rows; DataGridViewRow row = rows[index]; rows.Remove(row); rows.Insert(index + 1, row); dataGridViewValues.ClearSelection(); dataGridViewValues.Rows[index + 1].Cells[col].Selected = true; } } } } private void DoAddSeperator(object source, EventArgs e) { //Weil sich die Zustände der Elemente während der Initialisierung ändern, //wird die Ausführung der folgenden Aktionen unterdrückt if (initialized) { //Hat in diesem Programm keine Funktion } } private void DoEncrypt(object source, EventArgs e) { //Weil sich die Zustände der Elemente während der Initialisierung ändern, //wird die Ausführung der folgenden Aktionen unterdrückt if (initialized) { if (controlPanelControl.CheckBoxEncryptStatusSelected == false) { controlPanelControl.CheckBoxEncryptStatusSelected = true; } else { controlPanelControl.CheckBoxEncryptStatusSelected = false; } } } private void DoDefault(object source, EventArgs e) { //Weil sich die Zustände der Elemente während der Initialisierung ändern, wird die Ausführung der folgenden Aktionen unterdrückt //Nach der Initialisierung wird Alles durchlaufen if (initialized) { if (onlyRefresh) { try { //Wenn nur die Zeile ausgewählt wurde, dann nur die Sichtbarkeit aktualisieren if (StandardValue == dataGridViewValues.SelectedCells[0].RowIndex + 1) { controlPanelControl.CheckBoxDefaultStatusSelected = true; //Wenn die ausgewählte Zeile den Standardwert enthält, dann die CheckBox anwählen controlPanelControl.SetVisibility(); } else { controlPanelControl.CheckBoxDefaultStatusSelected = false; //Wenn die ausgewählte Zeile nicht den Standardwert enthält, dann die CheckBox abwählen controlPanelControl.SetVisibility(); } onlyRefresh = false; } catch (Exception) { //Nichts tun, wird ausgelöst, weil es die Zeile nicht mehr gibt } } else { //Wenn die CheckBox ausgewählt wurde, dann die ursprünliche Standardzeile zurückfärben, die aktuelle Zeil selektiern und umfärben, sowie 'StandardValue' setzen //Nur ausführen wenn es schon eine Standardzeile gibt if (rowIndexColored != 0) { dataGridViewValues.Rows[rowIndexColored - 1].DefaultCellStyle.ForeColor = Color.Empty; //Die ursprünglich ausgewählte Standardzeile zurückfärben StandardValue = dataGridViewValues.SelectedCells[0].RowIndex + 1; //Zeile die als Standard ausgewählt wurde dataGridViewValues.Rows[dataGridViewValues.SelectedCells[0].RowIndex].DefaultCellStyle.ForeColor = Color.LightSeaGreen; controlPanelControl.CheckBoxDefaultStatusSelected = true; //Wenn die ausgewählte Zeile den Standardwert enthält, dann die CheckBox anwählen controlPanelControl.SetVisibility(); rowIndexColored = dataGridViewValues.SelectedCells[0].RowIndex + 1; //Die Zeile merken die umgefärbt wurde } } } } #endregion } }