Improve newmemberbutton

This commit is contained in:
2023-03-15 00:28:06 +01:00
parent 2e14b4fcce
commit 4af3563a6e

View File

@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using ModernWpf.Controls;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
@ -16,6 +17,10 @@ namespace WGneu.Windows {
private bool IsCreating = false; private bool IsCreating = false;
private List<string> TextFilter = new(); private List<string> TextFilter = new();
private readonly Control[] ExemptInputs; private readonly Control[] ExemptInputs;
private IEnumerable<TextBox> TextBoxInputs = Array.Empty<TextBox>();
private IEnumerable<ComboBox> ComboBoxInputs = Array.Empty<ComboBox>();
private IEnumerable<CheckBox> CheckBoxInputs = Array.Empty<CheckBox>();
private IEnumerable<RadioButton> RadioButtonInputs = Array.Empty<RadioButton>();
private readonly Dictionary<Control, bool> Valid = new(); private readonly Dictionary<Control, bool> Valid = new();
private readonly Dictionary<Control, object?> OriginalValues = new(); private readonly Dictionary<Control, object?> OriginalValues = new();
private readonly RoutedCommand CtrlF = new(); private readonly RoutedCommand CtrlF = new();
@ -30,14 +35,18 @@ namespace WGneu.Windows {
NewMemberButton, EditMemberButton, DeleteMemberButton, NewMemberButton, EditMemberButton, DeleteMemberButton,
ResetButton, SaveButton, CancelButton ResetButton, SaveButton, CancelButton
}; };
foreach (var tb in Utils.FindVisualChilds<TextBox>(this, ExemptInputs))
Valid[tb] = true;
} }
private void Window_Loaded(object sender, RoutedEventArgs evt) { private void Window_Loaded(object sender, RoutedEventArgs evt) {
ActiveMemberInput.IsChecked = true; ActiveMemberInput.IsChecked = true;
BranchInput.ItemsSource = Context.Branches.OrderBy(b => b.Name).ToList(); BranchInput.ItemsSource = Context.Branches.OrderBy(b => b.Name).ToList();
DefaultKgInput.ItemsSource = Context.WbKgs.Select(k => k.Kg).OrderBy(k => k.Name).ToList(); DefaultKgInput.ItemsSource = Context.WbKgs.Select(k => k.Kg).OrderBy(k => k.Name).ToList();
TextBoxInputs = Utils.FindVisualChilds<TextBox>(this, ExemptInputs).ToList();
ComboBoxInputs = Utils.FindVisualChilds<ComboBox>(this, ExemptInputs).ToList();
CheckBoxInputs = Utils.FindVisualChilds<CheckBox>(this, ExemptInputs).ToList();
RadioButtonInputs = Utils.FindVisualChilds<RadioButton>(this, ExemptInputs).ToList();
foreach (var tb in TextBoxInputs)
Valid[tb] = true;
} }
protected override void OnClosing(CancelEventArgs evt) { protected override void OnClosing(CancelEventArgs evt) {
@ -84,13 +93,13 @@ namespace WGneu.Windows {
} }
private void ClearInputStates() { private void ClearInputStates() {
foreach (var tb in Utils.FindVisualChilds<TextBox>(this, ExemptInputs)) foreach (var tb in TextBoxInputs)
Utils.ClearInputState(tb); Utils.ClearInputState(tb);
foreach (var cb in Utils.FindVisualChilds<ComboBox>(this, ExemptInputs)) foreach (var cb in ComboBoxInputs)
Utils.ClearInputState(cb); Utils.ClearInputState(cb);
foreach (var cb in Utils.FindVisualChilds<CheckBox>(this, ExemptInputs)) foreach (var cb in CheckBoxInputs)
Utils.ClearInputState(cb); Utils.ClearInputState(cb);
foreach (var rb in Utils.FindVisualChilds<RadioButton>(this, ExemptInputs)) foreach (var rb in RadioButtonInputs)
Utils.ClearInputState(rb); Utils.ClearInputState(rb);
} }
@ -111,7 +120,6 @@ namespace WGneu.Windows {
} }
private void InitInputs() { private void InitInputs() {
ClearInputs();
MgNrInput.Text = Utils.NextMgNr(Context).ToString(); MgNrInput.Text = Utils.NextMgNr(Context).ToString();
EntryDateInput.Text = DateTime.Now.ToString("dd.MM.yyyy"); EntryDateInput.Text = DateTime.Now.ToString("dd.MM.yyyy");
if (Context.Branches.Count() == 1) if (Context.Branches.Count() == 1)
@ -317,24 +325,24 @@ namespace WGneu.Windows {
} }
private void LockInputs() { private void LockInputs() {
foreach (var tb in Utils.FindVisualChilds<TextBox>(this, ExemptInputs)) foreach (var tb in TextBoxInputs)
tb.IsReadOnly = true; tb.IsReadOnly = true;
foreach (var cb in Utils.FindVisualChilds<ComboBox>(this, ExemptInputs)) foreach (var cb in ComboBoxInputs)
cb.IsEnabled = false; cb.IsEnabled = false;
foreach (var cb in Utils.FindVisualChilds<CheckBox>(this, ExemptInputs)) foreach (var cb in CheckBoxInputs)
cb.IsEnabled = false; cb.IsEnabled = false;
foreach (var rb in Utils.FindVisualChilds<RadioButton>(this, ExemptInputs)) foreach (var rb in RadioButtonInputs)
rb.IsEnabled = false; rb.IsEnabled = false;
} }
private void UnlockInputs() { private void UnlockInputs() {
foreach (var tb in Utils.FindVisualChilds<TextBox>(this, ExemptInputs)) foreach (var tb in TextBoxInputs)
tb.IsReadOnly = false; tb.IsReadOnly = false;
foreach (var cb in Utils.FindVisualChilds<ComboBox>(this, ExemptInputs)) foreach (var cb in ComboBoxInputs)
cb.IsEnabled = true; cb.IsEnabled = true;
foreach (var cb in Utils.FindVisualChilds<CheckBox>(this, ExemptInputs)) foreach (var cb in CheckBoxInputs)
cb.IsEnabled = true; cb.IsEnabled = true;
foreach (var rb in Utils.FindVisualChilds<RadioButton>(this, ExemptInputs)) foreach (var rb in RadioButtonInputs)
rb.IsEnabled = true; rb.IsEnabled = true;
} }
@ -407,13 +415,13 @@ namespace WGneu.Windows {
} }
private void FillOriginalValues() { private void FillOriginalValues() {
foreach (var tb in Utils.FindVisualChilds<TextBox>(this, ExemptInputs)) foreach (var tb in TextBoxInputs)
OriginalValues[tb] = tb.Text; OriginalValues[tb] = tb.Text;
foreach (var cb in Utils.FindVisualChilds<ComboBox>(this, ExemptInputs)) foreach (var cb in ComboBoxInputs)
OriginalValues[cb] = cb.SelectedItem; OriginalValues[cb] = cb.SelectedItem;
foreach (var cb in Utils.FindVisualChilds<CheckBox>(this, ExemptInputs)) foreach (var cb in CheckBoxInputs)
OriginalValues[cb] = (cb.IsChecked ?? false) ? bool.TrueString : null; OriginalValues[cb] = (cb.IsChecked ?? false) ? bool.TrueString : null;
foreach (var rb in Utils.FindVisualChilds<RadioButton>(this, ExemptInputs)) foreach (var rb in RadioButtonInputs)
OriginalValues[rb] = (rb.IsChecked ?? false) ? bool.TrueString : null; OriginalValues[rb] = (rb.IsChecked ?? false) ? bool.TrueString : null;
} }
@ -421,24 +429,23 @@ namespace WGneu.Windows {
Menu_Member_SendEmail.IsEnabled = false; Menu_Member_SendEmail.IsEnabled = false;
AreaCommitment.Text = "- m²"; AreaCommitment.Text = "- m²";
OriginalValues.Clear(); OriginalValues.Clear();
foreach (var tb in Utils.FindVisualChilds<TextBox>(this, ExemptInputs)) { foreach (var tb in TextBoxInputs) {
tb.Text = " "; tb.Text = " ";
tb.Text = ""; tb.Text = "";
} }
foreach (var cb in Utils.FindVisualChilds<ComboBox>(this, ExemptInputs)) { foreach (var cb in ComboBoxInputs) {
cb.SelectedItem = null; cb.SelectedItem = null;
if (cb.ItemsSource != null) if (cb.ItemsSource != null)
Utils.SetInputInvalid(cb); Utils.SetInputInvalid(cb);
} }
foreach (var cb in Utils.FindVisualChilds<CheckBox>(this, ExemptInputs)) foreach (var cb in CheckBoxInputs)
cb.IsChecked = false; cb.IsChecked = false;
foreach (var rb in Utils.FindVisualChilds<RadioButton>(this, ExemptInputs)) foreach (var rb in RadioButtonInputs)
rb.IsChecked = false; rb.IsChecked = false;
} }
private bool IsValid() { private bool IsValid() {
return Valid.All(kv => kv.Value) && return Valid.All(kv => kv.Value) && ComboBoxInputs.All(cb => cb.ItemsSource == null || cb.SelectedItem != null);
Utils.FindVisualChilds<ComboBox>(this, ExemptInputs).All(cb => cb.ItemsSource == null || cb.SelectedItem != null);
} }
private void UpdateButtons() { private void UpdateButtons() {
@ -466,10 +473,10 @@ namespace WGneu.Windows {
private bool HasChanged() { private bool HasChanged() {
return !IsValid() || return !IsValid() ||
Utils.FindVisualChilds<TextBox>(this, ExemptInputs).Any(InputHasChanged) || TextBoxInputs.Any(InputHasChanged) ||
Utils.FindVisualChilds<ComboBox>(this, ExemptInputs).Any(InputHasChanged) || ComboBoxInputs.Any(InputHasChanged) ||
Utils.FindVisualChilds<CheckBox>(this, ExemptInputs).Any(InputHasChanged) || CheckBoxInputs.Any(InputHasChanged) ||
Utils.FindVisualChilds<RadioButton>(this, ExemptInputs).Any(InputHasChanged); RadioButtonInputs.Any(InputHasChanged);
} }
private void UpdatePlz(TextBox plzInput, ComboBox ortInput, bool optional) { private void UpdatePlz(TextBox plzInput, ComboBox ortInput, bool optional) {