265 lines
9.6 KiB
C#
265 lines
9.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Media;
|
|
|
|
namespace PEP_Tool
|
|
{
|
|
public static class Helper
|
|
{
|
|
|
|
public static Parent FindParent<Parent>(DependencyObject child)
|
|
where Parent : DependencyObject
|
|
{
|
|
DependencyObject parentObject = child;
|
|
|
|
//We are not dealing with Visual, so either we need to fnd parent or
|
|
//get Visual to get parent from Parent Heirarchy.
|
|
while (!((parentObject is System.Windows.Media.Visual)
|
|
|| (parentObject is System.Windows.Media.Media3D.Visual3D)))
|
|
{
|
|
if (parentObject is Parent || parentObject == null)
|
|
{
|
|
return parentObject as Parent;
|
|
}
|
|
else
|
|
{
|
|
parentObject = (parentObject as FrameworkContentElement).Parent;
|
|
}
|
|
}
|
|
|
|
//We have not found parent yet , and we have now visual to work with.
|
|
parentObject = VisualTreeHelper.GetParent(parentObject);
|
|
|
|
//check if the parent matches the type we're looking for
|
|
if (parentObject is Parent || parentObject == null)
|
|
{
|
|
return parentObject as Parent;
|
|
}
|
|
else
|
|
{
|
|
//use recursion to proceed with next level
|
|
return FindParent<Parent>(parentObject);
|
|
}
|
|
}
|
|
|
|
public static string RemoveSpecialCharacters(string str)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
foreach (char c in str)
|
|
{
|
|
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_')
|
|
{
|
|
sb.Append(c);
|
|
}
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
public static bool keyIsSpecialKey(System.Windows.Input.Key key)
|
|
{
|
|
var b = false;
|
|
|
|
if (key == System.Windows.Input.Key.Escape || key == System.Windows.Input.Key.Tab ||
|
|
key == System.Windows.Input.Key.CapsLock || key == System.Windows.Input.Key.LWin ||
|
|
key == System.Windows.Input.Key.Left || key == System.Windows.Input.Key.Up ||
|
|
key == System.Windows.Input.Key.Down || key == System.Windows.Input.Key.Right ||
|
|
key == System.Windows.Input.Key.End || key == System.Windows.Input.Key.Home ||
|
|
key == System.Windows.Input.Key.PageUp || key == System.Windows.Input.Key.PageDown ||
|
|
key == System.Windows.Input.Key.PrintScreen || key == System.Windows.Input.Key.Print ||
|
|
(key >= System.Windows.Input.Key.F1 && key <= System.Windows.Input.Key.LaunchApplication2) ||
|
|
key == System.Windows.Input.Key.System || key == System.Windows.Input.Key.Play)
|
|
b = true;
|
|
else
|
|
b = false; // the key will suppressed
|
|
|
|
return b;
|
|
}
|
|
|
|
public static bool isTelNumber(System.Windows.Input.Key Char)
|
|
{
|
|
return ((Char >= System.Windows.Input.Key.D0 && Char <= System.Windows.Input.Key.D9) ||
|
|
(Char >= System.Windows.Input.Key.NumPad0 && Char <= System.Windows.Input.Key.NumPad9) ||
|
|
(Char == System.Windows.Input.Key.Delete ||
|
|
Char == System.Windows.Input.Key.Divide ||
|
|
Char == System.Windows.Input.Key.Space ||
|
|
Char == System.Windows.Input.Key.Back ||
|
|
Char == System.Windows.Input.Key.Add));
|
|
}
|
|
|
|
|
|
|
|
public static bool IsBase64String(this string s)
|
|
{
|
|
s = s.Trim();
|
|
return (s.Length % 4 == 0) && System.Text.RegularExpressions.Regex.IsMatch(s, @"^[a-zA-Z0-9\+/]*={0,3}$", System.Text.RegularExpressions.RegexOptions.None);
|
|
}
|
|
|
|
|
|
public static T FindVisualChild<T>(System.Windows.DependencyObject depObj) where T : System.Windows.DependencyObject
|
|
{
|
|
if (depObj != null)
|
|
{
|
|
for (int i = 0; i < System.Windows.Media.VisualTreeHelper.GetChildrenCount(depObj); i++)
|
|
{
|
|
System.Windows.DependencyObject child = System.Windows.Media.VisualTreeHelper.GetChild(depObj, i);
|
|
if (child != null && child is T)
|
|
{
|
|
return (T)child;
|
|
}
|
|
|
|
T childItem = FindVisualChild<T>(child);
|
|
if (childItem != null) return childItem;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
public static int IndexOfNth(this string str, string value, int nth = 1)
|
|
{
|
|
if (nth <= 0)
|
|
throw new ArgumentException("Can not find the zeroth index of substring in string. Must start with 1");
|
|
int offset = str.IndexOf(value);
|
|
for (int i = 1; i < nth; i++)
|
|
{
|
|
if (offset == -1) return -1;
|
|
offset = str.IndexOf(value, offset + 1);
|
|
}
|
|
return offset;
|
|
}
|
|
|
|
public static int CountSubstring(this string text, string value)
|
|
{
|
|
int count = 0, minIndex = text.IndexOf(value, 0);
|
|
while (minIndex != -1)
|
|
{
|
|
minIndex = text.IndexOf(value, minIndex + value.Length);
|
|
count++;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
public static string AddLineNumbers(this string text, int startLine = 1)
|
|
{
|
|
var i = text.CountSubstring("\n");
|
|
var strArr = text.Split("\n".ToCharArray());
|
|
|
|
for (var j = startLine; j <= i; j++)
|
|
{
|
|
strArr[j] = $"{j - startLine + 1}. " + strArr[j].TrimStart("1234567890. ".ToCharArray());
|
|
}
|
|
|
|
text = string.Join("\n", strArr);
|
|
return text;
|
|
}
|
|
|
|
public static List<string> DataTableToListString(System.Data.DataTable dataTable, int Columns = 1, int startColumn = 0)
|
|
{
|
|
switch (Columns)
|
|
{
|
|
case 1:
|
|
return (from System.Data.DataRow dr in dataTable.Rows select dr[startColumn + 0].ToString()).ToList();
|
|
break;
|
|
case 2:
|
|
return (from System.Data.DataRow dr in dataTable.Rows select dr[startColumn + 0].ToString() + " " + dr[startColumn + 1].ToString()).ToList();
|
|
break;
|
|
|
|
|
|
default:
|
|
return (from System.Data.DataRow dr in dataTable.Rows select dr[0].ToString()).ToList();
|
|
}
|
|
}
|
|
|
|
|
|
public static List<Gewerk> DataTableToGewerk(System.Data.DataTable dataTable)
|
|
{
|
|
List<Gewerk> x = new List<Gewerk>();
|
|
|
|
foreach (DataRow dr in dataTable.Rows)
|
|
{
|
|
x.Add(new Gewerk()
|
|
{
|
|
Abteilung = dr[0].ToString(),
|
|
MAVerfuegbar = dr[1].ToString(),
|
|
MAAnwesend = dr[2].ToString(),
|
|
MAVerplant = dr[3].ToString()
|
|
});
|
|
}
|
|
|
|
return x;
|
|
}
|
|
|
|
|
|
public static int returnIndexForString(string input)
|
|
{
|
|
if (input.Contains("0") && !Regex.IsMatch(input, "[1-9]")) return 0;
|
|
else if (input.Contains("110")) return 1;
|
|
else if (input.Contains("670")) return 2;
|
|
else if (input.Contains("15")) return 3;
|
|
else if (input.Contains("un")) return 4;
|
|
else return -1;
|
|
}
|
|
|
|
|
|
public static IEnumerable<System.Windows.Controls.DataGridRow> GetDataGridRows(System.Windows.Controls.DataGrid grid)
|
|
{
|
|
var itemsSource = grid.ItemsSource;
|
|
if (null == itemsSource) { yield break; }
|
|
foreach (var item in itemsSource)
|
|
{
|
|
var row = grid.ItemContainerGenerator.ContainerFromItem(item) as System.Windows.Controls.DataGridRow;
|
|
if (null != row) yield return row;
|
|
}
|
|
}
|
|
|
|
|
|
public static bool IsWindowOpen<T>(string name = "") where T : System.Windows.Window
|
|
{
|
|
return string.IsNullOrEmpty(name)
|
|
? System.Windows.Application.Current.Windows.OfType<T>().Any()
|
|
: System.Windows.Application.Current.Windows.OfType<T>().Any(w => w.Title.Equals(name));
|
|
}
|
|
}
|
|
|
|
public class ReplaceDotConverter : System.Windows.Data.IValueConverter
|
|
{
|
|
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
|
|
{
|
|
if (value != null)
|
|
{
|
|
string original = value.ToString();
|
|
return original.Replace("l, ", "");
|
|
}
|
|
else return "";
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
|
|
public class IndexConverter : System.Windows.Data.IValueConverter
|
|
{
|
|
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
|
|
{
|
|
if (value != null)
|
|
{
|
|
return ((int)value + 1).ToString().Replace("0", "--");
|
|
}
|
|
else return "";
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
}
|