From af47eb606c75a0092a4cd74a495fbb963da900ee Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Wed, 20 Jul 2022 20:51:12 +0200 Subject: [PATCH] Added event when a text element gets selected --- .../Processor/TextElementProcessor.cs | 9 +++++++ I18N Commander/UI WinForms/AppEvents.cs | 8 ++++++ .../Components/TextElements.Designer.cs | 1 + .../UI WinForms/Components/TextElements.cs | 26 ++++++++++++++++--- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/I18N Commander/Processor/TextElementProcessor.cs b/I18N Commander/Processor/TextElementProcessor.cs index c8e304b..a2ea7e0 100644 --- a/I18N Commander/Processor/TextElementProcessor.cs +++ b/I18N Commander/Processor/TextElementProcessor.cs @@ -7,12 +7,21 @@ namespace Processor; public static class TextElementProcessor { + // Load all text elements for one particular section: public static async Task> GetTextElements(Section section) { await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); return await db.TextElements.Where(n => n.Section == section).OrderBy(n => n.Name).ThenBy(n => n.Id).ToListAsync(); } + + // Load one text element by id: + public static async Task LoadTextElement(int id) + { + await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); + return await db.TextElements.FirstAsync(n => n.Id == id); + } + // Adds a new text element: public static async Task> AddTextElement(string? currentSectionKey, string elementName) { await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); diff --git a/I18N Commander/UI WinForms/AppEvents.cs b/I18N Commander/UI WinForms/AppEvents.cs index 41191ae..f4ae16a 100644 --- a/I18N Commander/UI WinForms/AppEvents.cs +++ b/I18N Commander/UI WinForms/AppEvents.cs @@ -4,7 +4,15 @@ namespace UI_WinForms; internal static class AppEvents { + // Section changed event which can be subscribed: internal static event EventHandler
WhenSectionChanged; + // Method to raise the section changed event: internal static void SectionChanged(Section section) => AppEvents.WhenSectionChanged?.Invoke(null, section); + + // Text element changed event which can be subscribed: + internal static event EventHandler WhenTextElementChanged; + + // Method to raise the text element changed event: + internal static void TextElementChanged(TextElement textElement) => AppEvents.WhenTextElementChanged?.Invoke(null, textElement); } \ No newline at end of file diff --git a/I18N Commander/UI WinForms/Components/TextElements.Designer.cs b/I18N Commander/UI WinForms/Components/TextElements.Designer.cs index 316636e..dd7196d 100644 --- a/I18N Commander/UI WinForms/Components/TextElements.Designer.cs +++ b/I18N Commander/UI WinForms/Components/TextElements.Designer.cs @@ -169,6 +169,7 @@ this.listTextElements.TabIndex = 5; this.listTextElements.UseCompatibleStateImageBehavior = false; this.listTextElements.View = System.Windows.Forms.View.Details; + this.listTextElements.ItemSelectionChanged += new System.Windows.Forms.ListViewItemSelectionChangedEventHandler(this.listTextElements_ItemSelectionChanged); // // column // diff --git a/I18N Commander/UI WinForms/Components/TextElements.cs b/I18N Commander/UI WinForms/Components/TextElements.cs index 649aded..aec4ea3 100644 --- a/I18N Commander/UI WinForms/Components/TextElements.cs +++ b/I18N Commander/UI WinForms/Components/TextElements.cs @@ -8,6 +8,7 @@ namespace UI_WinForms.Components; public partial class TextElements : UserControl { private Section? currentSection; + private TextElement? currentTextElement; public TextElements() { @@ -30,15 +31,24 @@ public partial class TextElements : UserControl AppEvents.WhenSectionChanged += async (sender, section) => { this.currentSection = section; + this.currentTextElement = null; this.buttonAdd.Enabled = this.currentSection is not null; + this.buttonRename.Enabled = this.buttonRemove.Enabled = false; - // Update the path: if (this.currentSection is null) return; + // Update the path: this.labelSectionPath.Text = await SectionProcessor.GetSectionPath(this.currentSection.DataKey); this.LoadTextElements(); }; + + // When the text element is changed, update the button states: + AppEvents.WhenTextElementChanged += (sender, textElement) => + { + this.currentTextElement = textElement; + this.buttonRename.Enabled = this.buttonRemove.Enabled = this.currentTextElement is not null; + }; } // Loads all the text elements for the current section. @@ -55,7 +65,7 @@ public partial class TextElements : UserControl foreach (var textElement in textElements) this.listTextElements.Items.Add(new ListViewItem(textElement.Name, 0) { - Tag = textElement.Code, + Tag = textElement.Id, }); this.column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); @@ -86,7 +96,7 @@ public partial class TextElements : UserControl // Add the text element to the list: this.listTextElements.Items.Add(new ListViewItem(newTextElement.Result!.Name, 0) { - Tag = newTextElement.Result.Code, + Tag = newTextElement.Result.Id, }); this.column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); @@ -101,4 +111,14 @@ public partial class TextElements : UserControl { } + + private async void listTextElements_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) + { + // Load the text element: + var selectedTextElementId = (int)e.Item.Tag; + this.currentTextElement = await TextElementProcessor.LoadTextElement(selectedTextElementId); + + // Fire the event: + AppEvents.TextElementChanged(this.currentTextElement); + } } \ No newline at end of file