I18NCommander/I18N Commander/UI WinForms/Dialogs/InputDialog.cs

59 lines
1.9 KiB
C#
Raw Normal View History

2022-07-09 13:07:07 +00:00
namespace UI_WinForms.Dialogs;
public partial class InputDialog : Form
{
public readonly record struct Options(
string Message,
string Title,
string Placeholder = "",
string PreloadedText = "",
string OkButtonText = "Ok",
string CancelButtonText = "Cancel",
bool ShowQuestionCheckbox = false,
string QuestionCheckboxText = ""
);
2022-07-09 13:07:07 +00:00
private InputDialog()
{
this.InitializeComponent();
}
public static InputResult Show(Options options)
2022-07-09 13:07:07 +00:00
{
using var inputDialog = new InputDialog();
inputDialog.labelHead.Text = options.Message;
inputDialog.Text = options.Title;
inputDialog.textBoxInput.PlaceholderText = options.Placeholder;
inputDialog.textBoxInput.Text = options.PreloadedText;
inputDialog.buttonOk.Text = options.OkButtonText;
inputDialog.buttonCancel.Text = options.CancelButtonText;
inputDialog.checkBoxQuestion.Visible = options.ShowQuestionCheckbox;
inputDialog.checkBoxQuestion.Text = options.QuestionCheckboxText;
// Ensure, that the text box is focused on load:
inputDialog.textBoxInput.Select();
2022-07-09 13:07:07 +00:00
return new InputResult(
inputDialog.ShowDialog(),
inputDialog.textBoxInput.Text,
inputDialog.checkBoxQuestion.Checked
);
2022-07-09 13:07:07 +00:00
}
private void textBoxInput_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode is Keys.Enter or Keys.Return)
this.buttonOk.PerformClick();
}
private void buttonOk_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(this.textBoxInput.Text))
{
this.DialogResult = DialogResult.OK;
this.Close();
}
}
public readonly record struct InputResult(DialogResult DialogResult, string Text, bool AnswerToQuestion);
2022-07-09 13:07:07 +00:00
}