-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinFormsExtensions.cs
More file actions
149 lines (133 loc) · 5.19 KB
/
WinFormsExtensions.cs
File metadata and controls
149 lines (133 loc) · 5.19 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#if NETFRAMEWORK
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Windows.Forms;
namespace SharedTools
{
public static class WinFormsExtensions
{
public static void AppendLine(this TextBox textBox, string line)
{
textBox.AppendText(line + Environment.NewLine);
}
public static int PreferredWidth(this ListBox listBox)
{
int größteBreite = 0;
using (Graphics g = listBox.CreateGraphics())
{
foreach (object item in listBox.Items)
{
int breite = (int)Math.Ceiling(g.MeasureString(item.ToString(), listBox.Font).Width);
if (breite > größteBreite) größteBreite = breite;
}
}
return größteBreite + listBox.Margin.Horizontal;
}
public static void FillFromGrouping(this TreeView treeView, IEnumerable<IGrouping<string, string>> groupedList)
{
foreach (IGrouping<string, string> klasse in groupedList)
{
TreeNode root = new TreeNode(klasse.Key);
foreach (string item in klasse) root.Nodes.Add(item);
treeView.Nodes.Add(root);
}
}
public static void FillFromGrouping<T>(this TreeView treeView, IEnumerable<IGrouping<string, T>> groupedList, Func<T, string> valueSelector)
{
foreach (IGrouping<string, T> klasse in groupedList)
{
TreeNode root = new TreeNode(klasse.Key);
foreach (T elem in klasse) root.Nodes.Add(valueSelector(elem));
treeView.Nodes.Add(root);
}
}
public static void FillFromGrouping(this ListView listView, IEnumerable<IGrouping<string, string>> groupedList)
{
foreach (IGrouping<string, string> klasse in groupedList)
{
ListViewGroup root = new ListViewGroup(klasse.Key);
listView.Groups.Add(root);
foreach (string elem in klasse)
{
ListViewItem lvi = new ListViewItem(elem);
lvi.Group = root;
listView.Items.Add(lvi);
}
}
}
public static void FillFromGrouping<T>(this ListView listView, IEnumerable<IGrouping<string, T>> groupedList, Func<T, string> valueSelector)
{
foreach (IGrouping<string, T> klasse in groupedList)
{
ListViewGroup root = new ListViewGroup(klasse.Key);
listView.Groups.Add(root);
foreach (T elem in klasse)
{
ListViewItem lvi = new ListViewItem(valueSelector(elem));
lvi.Group = root;
listView.Items.Add(lvi);
}
}
}
private delegate void SetPropertyThreadSafeDelegate<TControl, TResult>(TControl control, Expression<Func<TControl, TResult>> property, TResult value);
public static void SetPropertyThreadSafe<TControl, TResult>(this TControl control, Expression<Func<TControl, TResult>> property, TResult value) where TControl : Control
{
if (control.IsDisposed) return;
MemberExpression memberExpression = (MemberExpression)property.Body;
if (memberExpression.NodeType != ExpressionType.Parameter && memberExpression.NodeType != ExpressionType.MemberAccess) throw new ArgumentException("property");
PropertyInfo info = (PropertyInfo)memberExpression.Member;
if (control.InvokeRequired)
{
try
{
control.Invoke(new SetPropertyThreadSafeDelegate<TControl, TResult>(SetPropertyThreadSafe), new object[] { control, property, value });
}
catch (ObjectDisposedException) { }
}
else
{
info.SetValue(control, value, null);
}
}
public static void SetFullscreenMode(this Form fenster, bool vollbild)
{
if (vollbild)
{
fenster.WindowState = FormWindowState.Normal;
fenster.FormBorderStyle = FormBorderStyle.None;
fenster.Bounds = Screen.GetBounds(fenster);
}
else
{
fenster.WindowState = FormWindowState.Maximized;
fenster.FormBorderStyle = FormBorderStyle.Sizable;
}
}
public static void InvokeGeneric(this Control c, Action func)
{
if (c.InvokeRequired)
c.Invoke(func);
else
func();
}
public static T InvokeGeneric<T>(this Control c, Func<T> func)
{
if (c.InvokeRequired)
return (T)c.Invoke(func);
else
return func();
}
public static void BeginInvokeGeneric(this Control c, Action func)
{
if (c.InvokeRequired)
c.BeginInvoke(func);
else
func();
}
}
}
#endif