Implemented the section's edit function

This commit is contained in:
Thorsten Sommer 2022-07-10 11:20:28 +02:00
parent aa22f7e1ef
commit 2ee393efec
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
6 changed files with 90 additions and 2 deletions

View File

@ -108,4 +108,31 @@ public static class SectionProcessor
return await db.Sections.CountAsync(n => n.Parent == section);
}
public static async Task<Section> RenameSection(DataContext db, string selectedNodeKey, string alteredName)
{
// Read the section from the database:
var section = await db.Sections.FirstOrDefaultAsync(n => n.DataKey == selectedNodeKey);
if (section is null)
throw new ArgumentException($"The section with key {selectedNodeKey} does not exist in the database.");
// Determine the new key:
var newKey = string.Join('_', alteredName.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)).ToUpperInvariant();
// Check, if this key already exists:
if (await db.Sections.AnyAsync(n => n.DataKey == newKey))
{
var rng = new Random();
while (await db.Sections.AnyAsync(n => n.DataKey == newKey))
{
// Add a random number to the end of the key:
newKey += $"_{rng.Next(1, 10_000)}";
}
}
section.Name = alteredName;
section.DataKey = newKey;
await db.SaveChangesAsync();
return section;
}
}

View File

@ -33,6 +33,7 @@
this.flowLayoutBottom = new System.Windows.Forms.FlowLayoutPanel();
this.buttonAdd = new System.Windows.Forms.Button();
this.buttonRemove = new System.Windows.Forms.Button();
this.buttonRename = new System.Windows.Forms.Button();
this.treeView = new System.Windows.Forms.TreeView();
this.toolTip = new System.Windows.Forms.ToolTip(this.components);
this.tableLayout.SuspendLayout();
@ -59,6 +60,7 @@
//
this.flowLayoutBottom.Controls.Add(this.buttonAdd);
this.flowLayoutBottom.Controls.Add(this.buttonRemove);
this.flowLayoutBottom.Controls.Add(this.buttonRename);
this.flowLayoutBottom.Dock = System.Windows.Forms.DockStyle.Fill;
this.flowLayoutBottom.Location = new System.Drawing.Point(0, 445);
this.flowLayoutBottom.Margin = new System.Windows.Forms.Padding(0);
@ -97,6 +99,22 @@
this.buttonRemove.UseVisualStyleBackColor = true;
this.buttonRemove.Click += new System.EventHandler(this.buttonRemove_Click);
//
// buttonRename
//
this.buttonRename.AutoSize = true;
this.buttonRename.Enabled = false;
this.buttonRename.FlatAppearance.BorderSize = 0;
this.buttonRename.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.buttonRename.Image = global::UI_WinForms.Resources.Icons.icons8_rename_512;
this.buttonRename.Location = new System.Drawing.Point(135, 3);
this.buttonRename.Name = "buttonRename";
this.buttonRename.Size = new System.Drawing.Size(60, 60);
this.buttonRename.TabIndex = 2;
this.buttonRename.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
this.toolTip.SetToolTip(this.buttonRename, "Rename the selected section");
this.buttonRename.UseVisualStyleBackColor = true;
this.buttonRename.Click += new System.EventHandler(this.buttonRename_Click);
//
// treeView
//
this.treeView.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
@ -140,5 +158,6 @@
private Button buttonRemove;
private TreeView treeView;
private ToolTip toolTip;
private Button buttonRename;
}
}

View File

@ -178,7 +178,36 @@ public partial class SectionTree : UserControl
// Get the currently selected section:
var selectedNode = this.treeView.SelectedNode;
// If the selected node is not null, enable the remove button:
this.buttonRemove.Enabled = selectedNode is not null;
// If the selected node is not null, enable the remove & edit button:
this.buttonRename.Enabled = this.buttonRemove.Enabled = selectedNode is not null;
}
private async void buttonRename_Click(object sender, EventArgs e)
{
// Ask the user if he really wants to rename the section:
if(MessageBox.Show("Are you sure, you want to rename the selected section? If you are already using this section in your code, you will need to manually refactor your code after renaming it.", "Rename section", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.No)
return;
// Get the currently selected section:
var selectedNode = this.treeView.SelectedNode;
// Ask the user for the new name:
var result = InputDialog.Show(new InputDialog.Options(
Message: "Please edit the section name.",
PreloadedText: selectedNode.Text,
ShowQuestionCheckbox: false,
Title: "Rename section"
));
// If the user canceled, return:
if(result.DialogResult == DialogResult.Cancel)
return;
// Rename the section:
var alteredSection = await SectionProcessor.RenameSection(this.db, selectedNode.Name, result.Text);
// Rename the selected node:
selectedNode.Text = alteredSection.Name;
selectedNode.Name = alteredSection.DataKey; // [sic] name is the key
}
}

View File

@ -159,5 +159,15 @@ namespace UI_WinForms.Resources {
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap icons8_rename_512 {
get {
object obj = ResourceManager.GetObject("icons8_rename_512", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
}
}

View File

@ -148,4 +148,7 @@
<data name="icons8_open_file_under_cursor_512" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>icons8-open-file-under-cursor-512.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="icons8_rename_512" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>icons8-rename-512.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB