Refactored input component to handle checkbox optionally

closes #15
This commit is contained in:
Thorsten Sommer 2022-07-09 15:29:22 +02:00
parent 91fabb658d
commit 97b7f823c8
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
3 changed files with 48 additions and 23 deletions

View File

@ -110,15 +110,24 @@ public partial class SectionTree : UserControl
private async void buttonAdd_Click(object sender, EventArgs e)
{
var result = InputDialog.Show("Please type the desired section name.", "Add a section", "My next section");
var result = InputDialog.Show(new InputDialog.Options(
Message: "Please type the desired section name.",
Title: "Add a section",
Placeholder: "My next section",
ShowQuestionCheckbox: true,
QuestionCheckboxText: "Add a root node (i.e. ignoring the selected node)"
));
if(result.DialogResult == DialogResult.Cancel)
return;
var addRootNode = result.AnswerToQuestion;
// Get the currently selected section as parent:
var selectedNode = this.treeView.SelectedNode;
// Add the new section to the database:
var addedSection = await SectionProcessor.AddSection(this.db, result.Text, result.IsRoot ? null : selectedNode?.Name);
var addedSection = await SectionProcessor.AddSection(this.db, result.Text, addRootNode ? null : selectedNode?.Name);
// Add the new section to the tree control:
var node = new TreeNode
@ -128,7 +137,7 @@ public partial class SectionTree : UserControl
StateImageIndex = 1,
};
if(!result.IsRoot && selectedNode is not null)
if(!addRootNode && selectedNode is not null)
selectedNode.Nodes.Add(node);
else
this.treeView.Nodes.Add(node);

View File

@ -34,7 +34,7 @@
this.buttonOk = new System.Windows.Forms.Button();
this.buttonCancel = new System.Windows.Forms.Button();
this.textBoxInput = new System.Windows.Forms.TextBox();
this.checkBoxIsRoot = new System.Windows.Forms.CheckBox();
this.checkBoxQuestion = new System.Windows.Forms.CheckBox();
this.tableLayout.SuspendLayout();
this.flowLayoutBottom.SuspendLayout();
this.SuspendLayout();
@ -72,7 +72,7 @@
//
this.flowLayoutBottom.Controls.Add(this.buttonOk);
this.flowLayoutBottom.Controls.Add(this.buttonCancel);
this.flowLayoutBottom.Controls.Add(this.checkBoxIsRoot);
this.flowLayoutBottom.Controls.Add(this.checkBoxQuestion);
this.flowLayoutBottom.Dock = System.Windows.Forms.DockStyle.Fill;
this.flowLayoutBottom.Location = new System.Drawing.Point(3, 68);
this.flowLayoutBottom.Margin = new System.Windows.Forms.Padding(3, 0, 0, 0);
@ -120,14 +120,13 @@
//
// checkBoxIsRoot
//
this.checkBoxIsRoot.AutoSize = true;
this.checkBoxIsRoot.Dock = System.Windows.Forms.DockStyle.Left;
this.checkBoxIsRoot.Location = new System.Drawing.Point(253, 3);
this.checkBoxIsRoot.Name = "checkBoxIsRoot";
this.checkBoxIsRoot.Size = new System.Drawing.Size(458, 60);
this.checkBoxIsRoot.TabIndex = 3;
this.checkBoxIsRoot.Text = "Add a root node (i.e. ignoring the selected node)";
this.checkBoxIsRoot.UseVisualStyleBackColor = true;
this.checkBoxQuestion.AutoSize = true;
this.checkBoxQuestion.Dock = System.Windows.Forms.DockStyle.Left;
this.checkBoxQuestion.Location = new System.Drawing.Point(253, 3);
this.checkBoxQuestion.Name = "checkBoxQuestion";
this.checkBoxQuestion.Size = new System.Drawing.Size(458, 60);
this.checkBoxQuestion.TabIndex = 3;
this.checkBoxQuestion.UseVisualStyleBackColor = true;
//
// InputDialog
//
@ -161,6 +160,6 @@
private TextBox textBoxInput;
private Button buttonOk;
private Button buttonCancel;
private CheckBox checkBoxIsRoot;
private CheckBox checkBoxQuestion;
}
}

View File

@ -2,22 +2,39 @@
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 = ""
);
private InputDialog()
{
this.InitializeComponent();
}
public static InputResult Show(string message, string title, string placeholder = "", string preloadedText = "", string okButtonText = "Ok", string cancelButtonText = "Cancel")
public static InputResult Show(Options options)
{
using var inputDialog = new InputDialog();
inputDialog.labelHead.Text = message;
inputDialog.Text = title;
inputDialog.textBoxInput.PlaceholderText = placeholder;
inputDialog.textBoxInput.Text = preloadedText;
inputDialog.buttonOk.Text = okButtonText;
inputDialog.buttonCancel.Text = cancelButtonText;
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;
return new InputResult(inputDialog.ShowDialog(), inputDialog.textBoxInput.Text, inputDialog.checkBoxIsRoot.Checked);
return new InputResult(
inputDialog.ShowDialog(),
inputDialog.textBoxInput.Text,
inputDialog.checkBoxQuestion.Checked
);
}
private void textBoxInput_KeyUp(object sender, KeyEventArgs e)
@ -35,5 +52,5 @@ public partial class InputDialog : Form
}
}
public readonly record struct InputResult(DialogResult DialogResult, string Text, bool IsRoot);
public readonly record struct InputResult(DialogResult DialogResult, string Text, bool AnswerToQuestion);
}