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 = "", bool QuestionCheckboxState = false ); private InputDialog() { this.InitializeComponent(); } public static InputResult Show(Options options) { 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; inputDialog.checkBoxQuestion.Checked = options.QuestionCheckboxState; // Ensure, that the text box is focused on load: inputDialog.textBoxInput.Select(); return new InputResult( inputDialog.ShowDialog(), inputDialog.textBoxInput.Text, inputDialog.checkBoxQuestion.Checked ); } 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(this.DesignMode) return; if (!string.IsNullOrWhiteSpace(this.textBoxInput.Text)) { this.DialogResult = DialogResult.OK; this.Close(); } } public readonly record struct InputResult(DialogResult DialogResult, string Text, bool AnswerToQuestion); }