using Deckungsbeitrag.AA_Klassen; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Forms; using VirtualKeyboard; namespace Deckungsbeitrag.AA_Forms { public partial class CustomMessageBox : Form { public DialogResult result; public string buttontext; private string title; private string caption; private string todo; private string[] buttons; private static string[] Inputs; private static string Title; private static string Caption; private static string Todo; private static string[] Buttons; /// /// Ist die Übersetzung in FallbackLanguage oder nicht. /// private bool isFallback = false; /// /// Aufrufen der CustomMessageBox. Besser .Show verwenden. /// /// /// /// /// public CustomMessageBox(string title, string caption, string todo, string[] buttons) { InitializeComponent(); ControlBox = false; this.title = title; this.caption = caption; this.todo = todo; this.buttons = buttons; } /// /// Aufrufen der CustomMessageBox. /// /// Key von FormTitel /// Key von Message-Text /// Key von ToDo /// Buttonnamen [0]=Button OK, [1]=Button Abbrechen, [2]=Button Alternative (werden nachträglich in Key generiert) /// Infotexte die in der Message angezeigt werden sollen. Nur wenn todo = NULL public static DialogResult Show(string title, string caption, string todo, string[] buttons, string[] inputs = null) { if (inputs != null) Inputs = inputs; CustomMessageBox messageBox = new CustomMessageBox(title, caption, todo, buttons); return messageBox.ShowDialog(); } /// /// Beim Laden der Box wird die Übersetzung geholt sowie die Texte in den Controls gesetzt und die Buttons aktualisiert. /// /// /// private void CustomMessageBox_Load(object sender, EventArgs e) { GetTranslation(title, caption, todo); Buttons = this.buttons; this.labelText.Text = Caption; this.Text = Title; if (Todo == null) { if (Inputs != null) { this.labelToDo.Text = string.Empty; foreach (string s in Inputs) this.labelToDo.Text += s + "\n"; } else this.labelToDo.Visible = false; } else this.labelToDo.Text = Todo; GetButtons(); } /// /// Die Buttons werden entsprechend mit übersetztem Text und AccessibleName aktualisiert. /// private void GetButtons() { if (Buttons.Length == 1) { this.buttonAbbrechen.Visible = false; this.buttonOK.Text = TranslationService.T($"Button.{Buttons[0]}"); this.buttonOK.AccessibleName = Buttons[0]; } if (Buttons.Length == 2) { this.buttonAbbrechen.Visible = true; this.buttonOK.Text = TranslationService.T($"Button.{Buttons[0]}"); this.buttonAbbrechen.Text = TranslationService.T($"Button.{Buttons[1]}"); this.buttonOK.AccessibleName = Buttons[0]; this.buttonAbbrechen.AccessibleName = Buttons[1]; } if (Buttons.Length == 3) { this.buttonAlternativ.Visible = true; this.buttonOK.Text = TranslationService.T($"Button.{Buttons[0]}"); this.buttonAbbrechen.Text = TranslationService.T($"Button.{Buttons[1]}"); this.buttonAlternativ.Text = TranslationService.T($"Button.{Buttons[2]}"); this.buttonOK.AccessibleName = Buttons[0]; this.buttonAbbrechen.AccessibleName = Buttons[1]; this.buttonAlternativ.AccessibleName = Buttons[2]; } } /// /// Die strings werden aus der translation.json ausgelesen und übersetzt. /// /// /// /// private static void GetTranslation(string title, string caption, string todo) { Title = TranslationService.T(title); Caption = TranslationService.T(caption); if (todo != null) Todo = TranslationService.T(todo); } /// /// Wenn Button geklickt wird, wird nach AccessibleName der DialogStatus gesetzt. Danach wird die Box geschlossen. /// /// /// private void Button_Click(object sender, EventArgs e) { Button btn = sender as Button; switch (btn.AccessibleName.ToLower()) { case var t when t == "ok": this.DialogResult = DialogResult.OK; break; case var t when t == "ja": this.DialogResult = DialogResult.Yes; break; case var t when t == "abbrechen": this.DialogResult = DialogResult.Cancel; break; case var t when t == "nein": this.DialogResult = DialogResult.No; break; default: this.buttontext = btn.AccessibleName; this.DialogResult = DialogResult.Ignore; break; } this.Close(); } /// /// Tastendruck wird erkannt und verarbeitet. /// /// /// protected override bool ProcessDialogKey(Keys keyData) { // Taste T löst Übersetzung in FallbackLanguage aus bzw. setzt die Übersetzung wieder auf CurrentLanguage zurück. if (Form.ModifierKeys == Keys.None && keyData == Keys.T) { if (!isFallback) TranslationService.SetLanguage(TranslationService.FallbackLanguage); else TranslationService.SetLanguage(Program._settings.Culture); isFallback = !isFallback; CustomMessageBox_Load(this, null); return true; } return base.ProcessDialogKey(keyData); } } }