-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
67 lines (55 loc) · 2.36 KB
/
Extensions.cs
File metadata and controls
67 lines (55 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
namespace slidegrid;
public static class Extensions
{
/// <summary>
/// Allows 0 through 9, a single decimal, and backspace only.
/// </summary>
public static void ValidateKeyPressDouble(this TextBox txt, KeyPressEventArgs e)
=> ValidateKeyPressNumeric(txt, e);
/// <summary>
/// Allows 0 through 9 and backspace only, and optionally a negative prefix.
/// </summary>
public static void ValidateKeyPressInt(this TextBox txt, KeyPressEventArgs e, bool allowNegative = false)
=> ValidateKeyPressNumeric(txt, e, AllowDecimal: false, AllowNegative: allowNegative);
/// <summary>
/// Swaps two List entries by index.
/// </summary>
public static void Swap<T>(this IList<T> list, int indexA, int indexB)
=> (list[indexA], list[indexB]) = (list[indexB], list[indexA]);
/// <summary>
/// Swaps two ListBox entries by index.
/// </summary>
public static void Swap(this ListBox list, int indexA, int indexB)
=> (list.Items[indexA], list.Items[indexB]) = (list.Items[indexB], list.Items[indexA]);
/// <summary>
/// Determines if the filename portion of the pathname contains wildcards.
/// </summary>
public static bool IsWildcardPath(this string pathname)
{
string fileName = Path.GetFileName(pathname);
return fileName.EndsWith('*');
}
/// <summary>
/// Determines if the pathname represents a supported image file type.
/// </summary>
public static bool IsSupportedFileType(this string pathname)
=> ".jpg|.jpeg|.png|.bmp".Contains(Path.GetExtension(pathname), StringComparison.InvariantCultureIgnoreCase);
////////////////////////////////////////////////////////////////////////////////////////////////
private static void ValidateKeyPressNumeric(TextBox txt, KeyPressEventArgs e, bool AllowDecimal = true, bool AllowNegative = false)
{
bool isValid = Char.IsNumber(e.KeyChar);
if (AllowDecimal && e.KeyChar == '.')
{
isValid = !txt.Text.Contains('.');
}
if (AllowNegative && e.KeyChar == '-')
{
isValid = !txt.Text.Contains('-') && txt.SelectionStart == 0;
}
// Allow backspace, tab
isValid = isValid || e.KeyChar == '\b';
// Stop the character from being entered into the control
if (!isValid) e.Handled = true;
}
}