Programm_Wirl/AA-Forms/CustomInputBox.cs

194 lines
6.0 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.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VirtualKeyboard;
namespace Deckungsbeitrag.AA_Forms
{
public partial class CustomInputBox : Form
{
private QwertzKeyboard keyboard;
private readonly bool isNumeric;
private bool isFallback = false;
private string title;
private string caption;
private string todo;
private string eingabe;
private string numeric;
public string text;
private static string Title;
private static string Caption;
private static string Todo;
private static string Eingabe;
public CustomInputBox(string numeric)
{
InitializeComponent();
ControlBox = false;
if (numeric == "isNumeric") { this.textBoxText.AccessibleName = "isNumeric"; this.isNumeric = true; }
if (numeric == "isAlpha") { this.textBoxText.AccessibleName = "isAlpha"; this.isNumeric = false; }
if (numeric == "isCalc") { this.textBoxText.AccessibleName = "isClac"; this.isNumeric = true; }
}
public CustomInputBox(string frage, string title, string eingabe, string numeric) : this(numeric)
{
Caption = frage;
Eingabe = eingabe;
Title = title;
}
/// <summary>
/// Aufrufen der CustomInputBox. Besser .Show verwenden.
/// </summary>
/// <param name="title">Key von BoxTitel</param>
/// <param name="caption">Key von Message-Text</param>
/// <param name="todo">Key von ToDo</param>
/// <param name="eingabe">Hinweis welche Eingabe erwartet wird</param>
/// <param name="numeric">Eingabeformat für VirtuelKeyboard (isNumeric, isCalc oder isAlpha)</param>
public CustomInputBox(string title, string caption, string todo, string eingabe, string Numeric) : this(Numeric)
{
this.title = title;
this.caption = caption;
this.todo = todo;
this.eingabe = eingabe;
}
/// <summary>
/// Aufrufen der CustomInputBox.
/// </summary>
/// <param name="title">Key von BoxTitel</param>
/// <param name="caption">Key von Message-Text</param>
/// <param name="todo">Key von ToDo</param>
/// <param name="eingabe">Hinweis welche Eingabe erwartet wird</param>
/// <param name="numeric">Eingabeformat für VirtuelKeyboard (isNumeric, isCalc oder isAlpha)</param>
public static DialogResult Show(string title, string caption, string todo, string eingabe, string numeric)
{
CustomInputBox inputBox = new CustomInputBox(title, caption, todo, eingabe, numeric);
return inputBox.ShowDialog();
}
private void CustomInputBox_Load(object sender, EventArgs e)
{
GetTranslation(title, caption, todo, eingabe);
this.buttonOK.Text = TranslationService.T("Button.Ok");
this.buttonAbbrechen.Text = TranslationService.T("Button.Abbrechen");
this.Text = Title;
this.labelFrage.Text = Caption + " ";
this.labelFrage.Text += !string.IsNullOrWhiteSpace(Todo) ? Todo : string.Empty;
this.labelEingabe.Text = !string.IsNullOrWhiteSpace(Eingabe) ? Eingabe + ":" : string.Empty;
if (isNumeric) this.textBoxText.Text = "1";
else this.textBoxText.Text = string.Empty;
}
/// <summary>
/// Die strings werden aus der translation.json ausgelesen und übersetzt.
/// </summary>
/// <param name="title"></param>
/// <param name="caption"></param>
/// <param name="todo"></param>
/// <param name="eingabe"></param>
private static void GetTranslation(string title, string caption, string todo, string eingabe)
{
if (title != null) Title = TranslationService.T(title);
if (caption != null) Caption = TranslationService.T(caption);
if (todo != null) Todo = TranslationService.T(todo);
if (eingabe != null) Eingabe = TranslationService.T(eingabe);
}
private void TextBoxText_Click(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
tb.Clear();
keyboard = Funktionen.OpenVirtualKeyboard(sender);
}
private void TextBoxText_Enter(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
keyboard = Funktionen.OpenVirtualKeyboard(sender);
this.BeginInvoke(new Action(() =>
{
if (!tb.IsDisposed && tb.CanFocus)
{
tb.Focus();
tb.SelectAll();
}
}));
}
private void ButtonAbbrechen_Click(object sender, EventArgs e)
{
if (keyboard != null) keyboard.Close();
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void ButtonOK_Click(object sender, EventArgs e)
{
if (keyboard != null) keyboard.Close();
this.DialogResult = DialogResult.OK;
this.Close();
}
private void TextBoxText_Validating(object sender, CancelEventArgs e)
{
if (buttonAbbrechen.Focused)
return;
if (isNumeric)
{
if (int.TryParse(textBoxText.Text, out int value)) this.text = textBoxText.Text;
else
{
CustomMessageBox.Show("Titel.Fehler", "Text.Fehler.NurZahlen", null, new string[] { "Ok" });
e.Cancel = true;
this.textBoxText.Clear();
}
}
else
{
this.text = textBoxText.Text;
}
}
/// <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;
CustomInputBox_Load(this, null);
return true;
}
return base.ProcessDialogKey(keyData);
}
}
}