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 ModernWpf.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
@ -16,6 +17,10 @@ namespace WGneu.Windows {
private bool IsCreating = false;
private List<string> TextFilter = new();
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, object?> 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<TextBox>(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<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) {
@ -84,13 +93,13 @@ namespace WGneu.Windows {
}
private void ClearInputStates() {
foreach (var tb in Utils.FindVisualChilds<TextBox>(this, ExemptInputs))
foreach (var tb in TextBoxInputs)
Utils.ClearInputState(tb);
foreach (var cb in Utils.FindVisualChilds<ComboBox>(this, ExemptInputs))
foreach (var cb in ComboBoxInputs)
Utils.ClearInputState(cb);
foreach (var cb in Utils.FindVisualChilds<CheckBox>(this, ExemptInputs))
foreach (var cb in CheckBoxInputs)
Utils.ClearInputState(cb);
foreach (var rb in Utils.FindVisualChilds<RadioButton>(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<TextBox>(this, ExemptInputs))
foreach (var tb in TextBoxInputs)
tb.IsReadOnly = true;
foreach (var cb in Utils.FindVisualChilds<ComboBox>(this, ExemptInputs))
foreach (var cb in ComboBoxInputs)
cb.IsEnabled = false;
foreach (var cb in Utils.FindVisualChilds<CheckBox>(this, ExemptInputs))
foreach (var cb in CheckBoxInputs)
cb.IsEnabled = false;
foreach (var rb in Utils.FindVisualChilds<RadioButton>(this, ExemptInputs))
foreach (var rb in RadioButtonInputs)
rb.IsEnabled = false;
}
private void UnlockInputs() {
foreach (var tb in Utils.FindVisualChilds<TextBox>(this, ExemptInputs))
foreach (var tb in TextBoxInputs)
tb.IsReadOnly = false;
foreach (var cb in Utils.FindVisualChilds<ComboBox>(this, ExemptInputs))
foreach (var cb in ComboBoxInputs)
cb.IsEnabled = true;
foreach (var cb in Utils.FindVisualChilds<CheckBox>(this, ExemptInputs))
foreach (var cb in CheckBoxInputs)
cb.IsEnabled = true;
foreach (var rb in Utils.FindVisualChilds<RadioButton>(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<TextBox>(this, ExemptInputs))
foreach (var tb in TextBoxInputs)
OriginalValues[tb] = tb.Text;
foreach (var cb in Utils.FindVisualChilds<ComboBox>(this, ExemptInputs))
foreach (var cb in ComboBoxInputs)
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;
foreach (var rb in Utils.FindVisualChilds<RadioButton>(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<TextBox>(this, ExemptInputs)) {
foreach (var tb in TextBoxInputs) {
tb.Text = " ";
tb.Text = "";
}
foreach (var cb in Utils.FindVisualChilds<ComboBox>(this, ExemptInputs)) {
foreach (var cb in ComboBoxInputs) {
cb.SelectedItem = null;
if (cb.ItemsSource != null)
Utils.SetInputInvalid(cb);
}
foreach (var cb in Utils.FindVisualChilds<CheckBox>(this, ExemptInputs))
foreach (var cb in CheckBoxInputs)
cb.IsChecked = false;
foreach (var rb in Utils.FindVisualChilds<RadioButton>(this, ExemptInputs))
foreach (var rb in RadioButtonInputs)
rb.IsChecked = false;
}
private bool IsValid() {
return Valid.All(kv => kv.Value) &&
Utils.FindVisualChilds<ComboBox>(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<TextBox>(this, ExemptInputs).Any(InputHasChanged) ||
Utils.FindVisualChilds<ComboBox>(this, ExemptInputs).Any(InputHasChanged) ||
Utils.FindVisualChilds<CheckBox>(this, ExemptInputs).Any(InputHasChanged) ||
Utils.FindVisualChilds<RadioButton>(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) {