Implemented the culture delete function

This commit is contained in:
Thorsten Sommer 2022-08-02 20:43:58 +02:00
parent fee963f8bd
commit 0c53c97a48
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
7 changed files with 94 additions and 0 deletions

View File

@ -385,6 +385,29 @@ public static class AppSettings
#endregion
#region Delete a culture
public static async Task DeleteCulture(int index)
{
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Check, if the setting is already set:
if (await db.Settings.FirstOrDefaultAsync(n => n.Code.StartsWith(SettingNames.CULTURE) && n.IntegerValue == index) is {} existingSetting)
{
db.Settings.Remove(existingSetting);
await db.SaveChangesAsync();
}
// Update the list of cultures indices:
CACHE_CULTURES_INDICES.Remove(index);
// Update the cache:
CACHE_CULTURES.Remove(index);
}
#endregion
#region Get a list of cultures
public readonly record struct CultureInfo(string Code, int Index);

View File

@ -35,6 +35,8 @@
this.panelSettings = new System.Windows.Forms.Panel();
this.flowLayoutToolbar = new System.Windows.Forms.FlowLayoutPanel();
this.buttonAddCulture = new System.Windows.Forms.Button();
this.buttonDeleteCulture = new System.Windows.Forms.Button();
this.contextMenuDelete = new System.Windows.Forms.ContextMenuStrip(this.components);
this.toolTip = new System.Windows.Forms.ToolTip(this.components);
this.tableLayout.SuspendLayout();
this.flowLayoutToolbar.SuspendLayout();
@ -97,6 +99,7 @@
//
this.tableLayout.SetColumnSpan(this.flowLayoutToolbar, 2);
this.flowLayoutToolbar.Controls.Add(this.buttonAddCulture);
this.flowLayoutToolbar.Controls.Add(this.buttonDeleteCulture);
this.flowLayoutToolbar.Dock = System.Windows.Forms.DockStyle.Fill;
this.flowLayoutToolbar.Location = new System.Drawing.Point(0, 315);
this.flowLayoutToolbar.Margin = new System.Windows.Forms.Padding(0);
@ -117,6 +120,27 @@
this.buttonAddCulture.UseVisualStyleBackColor = true;
this.buttonAddCulture.Click += new System.EventHandler(this.buttonAddCulture_Click);
//
// buttonDeleteCulture
//
this.buttonDeleteCulture.ContextMenuStrip = this.contextMenuDelete;
this.buttonDeleteCulture.FlatAppearance.BorderSize = 0;
this.buttonDeleteCulture.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.buttonDeleteCulture.Image = global::UI_WinForms.Resources.Icons.icons8_trash_can_512;
this.buttonDeleteCulture.Location = new System.Drawing.Point(69, 3);
this.buttonDeleteCulture.Name = "buttonDeleteCulture";
this.buttonDeleteCulture.Size = new System.Drawing.Size(60, 60);
this.buttonDeleteCulture.TabIndex = 0;
this.toolTip.SetToolTip(this.buttonDeleteCulture, "Delete a culture");
this.buttonDeleteCulture.UseVisualStyleBackColor = true;
this.buttonDeleteCulture.Click += new System.EventHandler(this.buttonDeleteCulture_Click);
//
// contextMenuDelete
//
this.contextMenuDelete.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.contextMenuDelete.ImageScalingSize = new System.Drawing.Size(20, 20);
this.contextMenuDelete.Name = "contextMenuDelete";
this.contextMenuDelete.Size = new System.Drawing.Size(211, 32);
//
// toolTip
//
this.toolTip.AutoPopDelay = 30000;
@ -149,5 +173,7 @@
private FlowLayoutPanel flowLayoutToolbar;
private ToolTip toolTip;
private Button buttonAddCulture;
private Button buttonDeleteCulture;
private ContextMenuStrip contextMenuDelete;
}
}

View File

@ -28,4 +28,33 @@ public partial class Settings : UserControl
// Reload all settings:
await this.LoadAllSettings();
}
private async void buttonDeleteCulture_Click(object sender, EventArgs e)
{
// Read all cultures:
var cultures = await AppSettings.GetCultureInfos();
// Populate the delete button's menu strip with all cultures:
this.contextMenuDelete.Items.Clear();
foreach (var culture in cultures)
{
var item = new ToolStripMenuItem($"{culture.Index}. Culture: {culture.Code}");
item.Tag = culture.Index;
item.Click += async (senderObj, args) =>
{
if (senderObj is not ToolStripMenuItem toolStripMenuItem)
return;
// Delete the culture:
await AppSettings.DeleteCulture((int)toolStripMenuItem.Tag);
// Reload all settings:
await this.LoadAllSettings();
};
this.contextMenuDelete.Items.Add(item);
}
this.contextMenuDelete.Show((Button)sender, new Point(0, (this.contextMenuDelete.Height + this.buttonDeleteCulture.Height / 2) * -1));
}
}

View File

@ -60,4 +60,7 @@
<metadata name="toolTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="contextMenuDelete.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>122, 17</value>
</metadata>
</root>

View File

@ -259,5 +259,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_trash_can_512 {
get {
object obj = ResourceManager.GetObject("icons8_trash_can_512", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
}
}

View File

@ -178,4 +178,7 @@
<data name="icons8_settings_svg" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>icons8-settings.svg.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="icons8_trash_can_512" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>icons8-trash-can-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