From 4af3563a6ea2ebdcd61792965a6ed3488732f8dc Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Wed, 15 Mar 2023 00:28:06 +0100 Subject: [PATCH] Improve newmemberbutton --- WGneu/Windows/MemberListWindow.xaml.cs | 65 ++++++++++++++------------ 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/WGneu/Windows/MemberListWindow.xaml.cs b/WGneu/Windows/MemberListWindow.xaml.cs index 2247762..a0c8daf 100644 --- a/WGneu/Windows/MemberListWindow.xaml.cs +++ b/WGneu/Windows/MemberListWindow.xaml.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using ModernWpf.Controls; using System; using System.Collections.Generic; using System.ComponentModel; @@ -16,6 +17,10 @@ namespace WGneu.Windows { private bool IsCreating = false; private List TextFilter = new(); private readonly Control[] ExemptInputs; + private IEnumerable TextBoxInputs = Array.Empty(); + private IEnumerable ComboBoxInputs = Array.Empty(); + private IEnumerable CheckBoxInputs = Array.Empty(); + private IEnumerable RadioButtonInputs = Array.Empty(); private readonly Dictionary Valid = new(); private readonly Dictionary OriginalValues = new(); private readonly RoutedCommand CtrlF = new(); @@ -30,14 +35,18 @@ namespace WGneu.Windows { NewMemberButton, EditMemberButton, DeleteMemberButton, ResetButton, SaveButton, CancelButton }; - foreach (var tb in Utils.FindVisualChilds(this, ExemptInputs)) - Valid[tb] = true; } private void Window_Loaded(object sender, RoutedEventArgs evt) { ActiveMemberInput.IsChecked = true; BranchInput.ItemsSource = Context.Branches.OrderBy(b => b.Name).ToList(); DefaultKgInput.ItemsSource = Context.WbKgs.Select(k => k.Kg).OrderBy(k => k.Name).ToList(); + TextBoxInputs = Utils.FindVisualChilds(this, ExemptInputs).ToList(); + ComboBoxInputs = Utils.FindVisualChilds(this, ExemptInputs).ToList(); + CheckBoxInputs = Utils.FindVisualChilds(this, ExemptInputs).ToList(); + RadioButtonInputs = Utils.FindVisualChilds(this, ExemptInputs).ToList(); + foreach (var tb in TextBoxInputs) + Valid[tb] = true; } protected override void OnClosing(CancelEventArgs evt) { @@ -84,13 +93,13 @@ namespace WGneu.Windows { } private void ClearInputStates() { - foreach (var tb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var tb in TextBoxInputs) Utils.ClearInputState(tb); - foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var cb in ComboBoxInputs) Utils.ClearInputState(cb); - foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var cb in CheckBoxInputs) Utils.ClearInputState(cb); - foreach (var rb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var rb in RadioButtonInputs) Utils.ClearInputState(rb); } @@ -111,7 +120,6 @@ namespace WGneu.Windows { } private void InitInputs() { - ClearInputs(); MgNrInput.Text = Utils.NextMgNr(Context).ToString(); EntryDateInput.Text = DateTime.Now.ToString("dd.MM.yyyy"); if (Context.Branches.Count() == 1) @@ -317,24 +325,24 @@ namespace WGneu.Windows { } private void LockInputs() { - foreach (var tb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var tb in TextBoxInputs) tb.IsReadOnly = true; - foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var cb in ComboBoxInputs) cb.IsEnabled = false; - foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var cb in CheckBoxInputs) cb.IsEnabled = false; - foreach (var rb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var rb in RadioButtonInputs) rb.IsEnabled = false; } private void UnlockInputs() { - foreach (var tb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var tb in TextBoxInputs) tb.IsReadOnly = false; - foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var cb in ComboBoxInputs) cb.IsEnabled = true; - foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var cb in CheckBoxInputs) cb.IsEnabled = true; - foreach (var rb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var rb in RadioButtonInputs) rb.IsEnabled = true; } @@ -407,13 +415,13 @@ namespace WGneu.Windows { } private void FillOriginalValues() { - foreach (var tb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var tb in TextBoxInputs) OriginalValues[tb] = tb.Text; - foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var cb in ComboBoxInputs) OriginalValues[cb] = cb.SelectedItem; - foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var cb in CheckBoxInputs) OriginalValues[cb] = (cb.IsChecked ?? false) ? bool.TrueString : null; - foreach (var rb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var rb in RadioButtonInputs) OriginalValues[rb] = (rb.IsChecked ?? false) ? bool.TrueString : null; } @@ -421,24 +429,23 @@ namespace WGneu.Windows { Menu_Member_SendEmail.IsEnabled = false; AreaCommitment.Text = "- m²"; OriginalValues.Clear(); - foreach (var tb in Utils.FindVisualChilds(this, ExemptInputs)) { + foreach (var tb in TextBoxInputs) { tb.Text = " "; tb.Text = ""; } - foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) { + foreach (var cb in ComboBoxInputs) { cb.SelectedItem = null; if (cb.ItemsSource != null) Utils.SetInputInvalid(cb); } - foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var cb in CheckBoxInputs) cb.IsChecked = false; - foreach (var rb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var rb in RadioButtonInputs) rb.IsChecked = false; } private bool IsValid() { - return Valid.All(kv => kv.Value) && - Utils.FindVisualChilds(this, ExemptInputs).All(cb => cb.ItemsSource == null || cb.SelectedItem != null); + return Valid.All(kv => kv.Value) && ComboBoxInputs.All(cb => cb.ItemsSource == null || cb.SelectedItem != null); } private void UpdateButtons() { @@ -466,10 +473,10 @@ namespace WGneu.Windows { private bool HasChanged() { return !IsValid() || - Utils.FindVisualChilds(this, ExemptInputs).Any(InputHasChanged) || - Utils.FindVisualChilds(this, ExemptInputs).Any(InputHasChanged) || - Utils.FindVisualChilds(this, ExemptInputs).Any(InputHasChanged) || - Utils.FindVisualChilds(this, ExemptInputs).Any(InputHasChanged); + TextBoxInputs.Any(InputHasChanged) || + ComboBoxInputs.Any(InputHasChanged) || + CheckBoxInputs.Any(InputHasChanged) || + RadioButtonInputs.Any(InputHasChanged); } private void UpdatePlz(TextBox plzInput, ComboBox ortInput, bool optional) {