MemberListWindow Validator

This commit is contained in:
2023-02-26 00:41:16 +01:00
parent dcfe6169b0
commit 0059f57e5e
4 changed files with 136 additions and 24 deletions

29
WGneu/Utils.cs Normal file
View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows;
namespace WGneu
{
class Utils
{
public static IEnumerable<T> FindVisualChilds<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj == null)
yield return (T)Enumerable.Empty<T>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject ithChild = VisualTreeHelper.GetChild(depObj, i);
if (ithChild == null)
continue;
if (ithChild is T t)
yield return t;
foreach (T childOfChild in FindVisualChilds<T>(ithChild))
yield return childOfChild;
}
}
}
}

72
WGneu/Validator.cs Normal file
View File

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
namespace WGneu
{
static class Validator
{
public static ValidationResult CheckNumericInput(TextBox input)
{
return CheckNumericInput(input, -1);
}
private static ValidationResult CheckNumericInput(TextBox input, int maxLen)
{
string text = "";
int pos = input.CaretIndex;
for (int i = 0; i < input.Text.Length; i++)
{
char ch = input.Text[i];
if (Char.IsDigit(ch))
text += ch;
if (i == input.CaretIndex - 1)
pos = text.Length;
}
input.Text = text;
input.CaretIndex = pos;
if (maxLen >= 0 && input.Text.Length > maxLen)
{
input.Text = input.Text.Substring(0, maxLen);
input.CaretIndex = Math.Min(pos, maxLen);
}
return new ValidationResult(true, "");
}
public static ValidationResult CheckLfbisNr(TextBox input)
{
var res = CheckNumericInput(input, 7);
if (!res.IsValid)
return res;
if (input.Text.Length == 0)
return new ValidationResult(true, "");
if (input.Text.Length != 7)
return new ValidationResult(false, "Betriebsnummer zu kurz");
// TODO
return new ValidationResult(true, "Not implemented yet");
}
public static ValidationResult CheckUstIdInput(TextBox input)
{
return new ValidationResult(false, "Not implemented yet");
}
public static void SetInputInvalid(TextBox input)
{
input.BorderBrush = System.Windows.Media.Brushes.Red;
}
public static void SetInputValid(TextBox input)
{
input.ClearValue(TextBox.BorderBrushProperty);
}
}
}

View File

@ -171,6 +171,7 @@
<Label Content="BetriebsNr.:" HorizontalAlignment="Left" Margin="10,42,0,0" VerticalAlignment="Top" Padding="2"/>
<TextBox x:Name="LfbisNrInput" IsReadOnly="True"
TextChanged="LfbisNrInput_TextChanged" LostFocus="LfbisNrInput_LostFocus"
Margin="0,40,10,0" VerticalAlignment="Top" FontSize="14" Padding="2" Grid.Column="1" Height="25" Width="64" HorizontalAlignment="Left"/>
<CheckBox x:Name="BuchführendInput" Content="Buchführend" IsEnabled="False"

View File

@ -294,26 +294,26 @@ namespace WGneu.Windows
private void LockInputs()
{
MgNrInput.IsReadOnly = true;
GivenNameInput.IsReadOnly = true;
FamilyNameInput.IsReadOnly = true;
AddressInput.IsReadOnly = true;
PlzInput.IsReadOnly = true;
OrtInput.IsEnabled = false;
BranchInput.IsEnabled = false;
DefaultKgInput.IsEnabled = false;
foreach (var tb in Utils.FindVisualChilds<TextBox>(this))
if (tb.Name != "SearchInput") tb.IsReadOnly = true;
foreach (var cb in Utils.FindVisualChilds<ComboBox>(this))
cb.IsEnabled = false;
foreach (var cb in Utils.FindVisualChilds<CheckBox>(this))
cb.IsEnabled = false;
foreach (var rb in Utils.FindVisualChilds<RadioButton>(this))
rb.IsEnabled = false;
}
private void UnlockInputs()
{
MgNrInput.IsReadOnly = false;
GivenNameInput.IsReadOnly = false;
FamilyNameInput.IsReadOnly = false;
AddressInput.IsReadOnly = false;
PlzInput.IsReadOnly = false;
OrtInput.IsEnabled = true;
BranchInput.IsEnabled = true;
DefaultKgInput.IsEnabled = true;
foreach (var tb in Utils.FindVisualChilds<TextBox>(this))
if (tb.Name != "SearchInput") tb.IsReadOnly = false;
foreach (var cb in Utils.FindVisualChilds<ComboBox>(this))
cb.IsEnabled = true;
foreach (var cb in Utils.FindVisualChilds<CheckBox>(this))
cb.IsEnabled = true;
foreach (var rb in Utils.FindVisualChilds<RadioButton>(this))
rb.IsEnabled = true;
}
private void FillInputs(Member m)
@ -341,14 +341,24 @@ namespace WGneu.Windows
private void ClearInputs()
{
MgNrInput.Text = "";
GivenNameInput.Text = "";
FamilyNameInput.Text = "";
BranchInput.SelectedItem = null;
PlzInput.Text = "";
OrtInput.SelectedItem = null;
AddressInput.Text = "";
DefaultKgInput.SelectedItem = null;
foreach (var tb in Utils.FindVisualChilds<TextBox>(this))
if (tb.Name != "SearchInput") tb.Text = "";
foreach (var cb in Utils.FindVisualChilds<ComboBox>(this))
cb.SelectedItem = null;
}
private void LfbisNrInput_TextChanged(object sender, RoutedEventArgs e)
{
var res = Validator.CheckLfbisNr(LfbisNrInput);
if (res.IsValid) Validator.SetInputValid(LfbisNrInput);
else Validator.SetInputInvalid(LfbisNrInput);
}
private void LfbisNrInput_LostFocus(object sender, RoutedEventArgs e)
{
var res = Validator.CheckLfbisNr(LfbisNrInput);
if (!res.IsValid)
MessageBox.Show(res.ErrorContent.ToString(), "Betriebsnummer ungültig", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
}