Added event when a text element gets selected

This commit is contained in:
Thorsten Sommer 2022-07-20 20:51:12 +02:00
parent 0e4a99332b
commit af47eb606c
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
4 changed files with 41 additions and 3 deletions

View File

@ -7,12 +7,21 @@ namespace Processor;
public static class TextElementProcessor public static class TextElementProcessor
{ {
// Load all text elements for one particular section:
public static async Task<List<TextElement>> GetTextElements(Section section) public static async Task<List<TextElement>> GetTextElements(Section section)
{ {
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>(); await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
return await db.TextElements.Where(n => n.Section == section).OrderBy(n => n.Name).ThenBy(n => n.Id).ToListAsync(); 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<TextElement> LoadTextElement(int id)
{
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
return await db.TextElements.FirstAsync(n => n.Id == id);
}
// Adds a new text element:
public static async Task<ProcessorResult<TextElement>> AddTextElement(string? currentSectionKey, string elementName) public static async Task<ProcessorResult<TextElement>> AddTextElement(string? currentSectionKey, string elementName)
{ {
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>(); await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();

View File

@ -4,7 +4,15 @@ namespace UI_WinForms;
internal static class AppEvents internal static class AppEvents
{ {
// Section changed event which can be subscribed:
internal static event EventHandler<Section> WhenSectionChanged; internal static event EventHandler<Section> WhenSectionChanged;
// Method to raise the section changed event:
internal static void SectionChanged(Section section) => AppEvents.WhenSectionChanged?.Invoke(null, section); internal static void SectionChanged(Section section) => AppEvents.WhenSectionChanged?.Invoke(null, section);
// Text element changed event which can be subscribed:
internal static event EventHandler<TextElement> WhenTextElementChanged;
// Method to raise the text element changed event:
internal static void TextElementChanged(TextElement textElement) => AppEvents.WhenTextElementChanged?.Invoke(null, textElement);
} }

View File

@ -169,6 +169,7 @@
this.listTextElements.TabIndex = 5; this.listTextElements.TabIndex = 5;
this.listTextElements.UseCompatibleStateImageBehavior = false; this.listTextElements.UseCompatibleStateImageBehavior = false;
this.listTextElements.View = System.Windows.Forms.View.Details; this.listTextElements.View = System.Windows.Forms.View.Details;
this.listTextElements.ItemSelectionChanged += new System.Windows.Forms.ListViewItemSelectionChangedEventHandler(this.listTextElements_ItemSelectionChanged);
// //
// column // column
// //

View File

@ -8,6 +8,7 @@ namespace UI_WinForms.Components;
public partial class TextElements : UserControl public partial class TextElements : UserControl
{ {
private Section? currentSection; private Section? currentSection;
private TextElement? currentTextElement;
public TextElements() public TextElements()
{ {
@ -30,15 +31,24 @@ public partial class TextElements : UserControl
AppEvents.WhenSectionChanged += async (sender, section) => AppEvents.WhenSectionChanged += async (sender, section) =>
{ {
this.currentSection = section; this.currentSection = section;
this.currentTextElement = null;
this.buttonAdd.Enabled = this.currentSection is not null; this.buttonAdd.Enabled = this.currentSection is not null;
this.buttonRename.Enabled = this.buttonRemove.Enabled = false;
// Update the path:
if (this.currentSection is null) if (this.currentSection is null)
return; return;
// Update the path:
this.labelSectionPath.Text = await SectionProcessor.GetSectionPath(this.currentSection.DataKey); this.labelSectionPath.Text = await SectionProcessor.GetSectionPath(this.currentSection.DataKey);
this.LoadTextElements(); 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. // Loads all the text elements for the current section.
@ -55,7 +65,7 @@ public partial class TextElements : UserControl
foreach (var textElement in textElements) foreach (var textElement in textElements)
this.listTextElements.Items.Add(new ListViewItem(textElement.Name, 0) this.listTextElements.Items.Add(new ListViewItem(textElement.Name, 0)
{ {
Tag = textElement.Code, Tag = textElement.Id,
}); });
this.column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); this.column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
@ -86,7 +96,7 @@ public partial class TextElements : UserControl
// Add the text element to the list: // Add the text element to the list:
this.listTextElements.Items.Add(new ListViewItem(newTextElement.Result!.Name, 0) this.listTextElements.Items.Add(new ListViewItem(newTextElement.Result!.Name, 0)
{ {
Tag = newTextElement.Result.Code, Tag = newTextElement.Result.Id,
}); });
this.column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); 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);
}
} }