205 lines
6.2 KiB
C#
205 lines
6.2 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// Ist die Übersetzung in FallbackLanguage oder nicht.
|
|
/// </summary>
|
|
private bool isFallback = false;
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Aufrufen der CustomMessageBox. Besser .Show verwenden.
|
|
/// </summary>
|
|
/// <param name="title"></param>
|
|
/// <param name="caption"></param>
|
|
/// <param name="todo"></param>
|
|
/// <param name="buttons"></param>
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Aufrufen der CustomMessageBox.
|
|
/// </summary>
|
|
/// <param name="title">Key von FormTitel</param>
|
|
/// <param name="caption">Key von Message-Text</param>
|
|
/// <param name="todo">Key von ToDo</param>
|
|
/// <param name="buttons">Buttonnamen [0]=Button OK, [1]=Button Abbrechen, [2]=Button Alternative (werden nachträglich in Key generiert)</param>
|
|
/// <param name="inputs">Infotexte die in der Message angezeigt werden sollen. Nur wenn todo = NULL</param>
|
|
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();
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Beim Laden der Box wird die Übersetzung geholt sowie die Texte in den Controls gesetzt und die Buttons aktualisiert.
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
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();
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Die Buttons werden entsprechend mit übersetztem Text und AccessibleName aktualisiert.
|
|
/// </summary>
|
|
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];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Die strings werden aus der translation.json ausgelesen und übersetzt.
|
|
/// </summary>
|
|
/// <param name="title"></param>
|
|
/// <param name="caption"></param>
|
|
/// <param name="todo"></param>
|
|
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);
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Wenn Button geklickt wird, wird nach AccessibleName der DialogStatus gesetzt. Danach wird die Box geschlossen.
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
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();
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Tastendruck wird erkannt und verarbeitet.
|
|
/// </summary>
|
|
/// <param name="keyData"></param>
|
|
/// <returns></returns>
|
|
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);
|
|
}
|
|
|
|
}
|
|
}
|