-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsFormsUtility.cs
More file actions
162 lines (148 loc) · 5.42 KB
/
WindowsFormsUtility.cs
File metadata and controls
162 lines (148 loc) · 5.42 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
150
151
152
153
154
155
156
157
158
159
160
161
162
using System.Data;
namespace CPUWindowsFormFramework
{
public class WindowsFormsUtility
{
public static void SetListBinding(ComboBox lst, DataTable sourcedt, DataTable? targetdt, string tablename)
{
lst.DataSource = sourcedt;
lst.ValueMember = tablename + "Id";
lst.DisplayMember = lst.Name.Substring(3);
if(targetdt != null)
{
lst.DataBindings.Add("SelectedValue", targetdt, lst.ValueMember, false, DataSourceUpdateMode.OnPropertyChanged);
}
}
public static void SetControlBindings(Control ctrl, BindingSource bindsource)
{
string propertyname = "";
string controlname = ctrl.Name.ToLower();
string controltype = controlname.Substring(0, 3);
string columnname = controlname.Substring(3);
switch (controltype)
{
case "txt":
case "lbl":
propertyname = "Text";
break;
case "dtp":
propertyname = "Value";
break;
}
if (propertyname != "" || columnname != "")
{
ctrl.DataBindings.Add(propertyname, bindsource, columnname, true, DataSourceUpdateMode.OnPropertyChanged);
}
}
public static void FormatGridForSearchResults( DataGridView grid, string tablename)
{
grid.AllowUserToAddRows = false;
grid.ReadOnly = true;
grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
DoFormatGrid(grid, tablename);
}
public static void FormatGridForEdit(DataGridView grid, string tablename)
{
grid.EditMode = DataGridViewEditMode.EditOnEnter;
DoFormatGrid(grid, tablename);
}
private static void DoFormatGrid(DataGridView grid, string tablename)
{
grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
grid.RowHeadersWidth = 25;
foreach(DataGridViewColumn col in grid.Columns)
{
if(col.Name.EndsWith("Id"))
{
col.Visible = false;
}
}
string pkname = tablename + "Id";
if(grid.Columns.Contains(pkname))
{
grid.Columns[pkname].Visible = false;
}
}
public static int GetIdFromGrid(DataGridView grid, int rowindex, string columnname)
{
int id = 0;
if(rowindex < grid.Rows.Count && grid.Columns.Contains(columnname) && grid.Rows[rowindex].Cells[columnname].Value != DBNull.Value)
{
if (grid.Rows[rowindex].Cells[columnname].Value is int)
{
id = (int)grid.Rows[rowindex].Cells[columnname].Value;
}
}
return id;
}
public static int GetIdFromComboBox(ComboBox lst)
{
int value = 0;
if(lst.SelectedValue != null && lst.SelectedValue is int)
{
value = (int)lst.SelectedValue;
}
return value;
}
public static void AddComboBoxToGrid(DataGridView grid, DataTable datasource, string tablename, string displaymember)
{
DataGridViewComboBoxColumn c = new();
c.DataSource = datasource;
c.DisplayMember = displaymember;
c.ValueMember = tablename + "Id";
c.DataPropertyName = c.ValueMember;
c.HeaderText = tablename;
grid.Columns.Insert(0, c);
}
public static void AddDeleteButtonToGrid(DataGridView grid, string deletecolname)
{
grid.Columns.Add(new DataGridViewButtonColumn() { Text = "X", HeaderText = "Delete", Name = deletecolname, UseColumnTextForButtonValue = true });
}
public static bool IsFormOpen(Type formtype, int pkvalue = 0)
{
bool exists = false;
foreach (Form frm in Application.OpenForms)
{
int frmpkvalue = 0;
if(frm.Tag != null && frm.Tag is int)
{
frmpkvalue = (int)frm.Tag;
}
if (frm.GetType() == formtype && frmpkvalue == pkvalue)
{
frm.Activate();
exists = true;
break;
}
}
return exists;
}
public static void SetupNav(ToolStrip ts)
{
ts.Items.Clear();
foreach (Form f in Application.OpenForms)
{
if (f.IsMdiContainer == false)
{
ToolStripButton btn = new();
btn.Text = f.Text;
btn.Tag = f;
btn.Click += Btn_Click;
ts.Items.Add(btn);
ts.Items.Add(new ToolStripSeparator());
}
}
}
private static void Btn_Click(object? sender, EventArgs e)
{
if (sender != null && sender is ToolStripButton)
{
ToolStripButton btn = (ToolStripButton)sender;
if (btn.Tag != null && btn.Tag is Form)
{
((Form)btn.Tag).Activate();
}
}
}
}
}