-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsFormsUtility.cs
More file actions
159 lines (145 loc) · 5.22 KB
/
WindowsFormsUtility.cs
File metadata and controls
159 lines (145 loc) · 5.22 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
using System.Data;
namespace CPUWindowsFormsFramework
{
public class WindowsFormsUtility
{
public static void SetListBinding(ComboBox lst, DataTable dtSource, DataTable? dtTarget, string tableName)
{
lst.DataBindings.Clear();
lst.DataSource = dtSource;
lst.ValueMember = tableName + "Id";
lst.DisplayMember = lst.Name.Substring(3);
if (dtTarget != null)
{
lst.DataBindings.Add("SelectedValue", dtTarget, lst.ValueMember, false, DataSourceUpdateMode.OnPropertyChanged);
}
}
public static void SetControlBinding(Control ctrl, BindingSource bindSource)
{
string propertyName = "";
string controlName = ctrl.Name.ToLower();
string controlType = controlName.Substring(0, 3);
string columnName = controlName.Substring(3);
propertyName = controlType switch
{
"lbl" => "Text",
"txt" => "Text",
"dtp" => "Value",
"cbx" => "Checked"
};
if (!string.IsNullOrEmpty(propertyName) && !string.IsNullOrEmpty(columnName))
{
ctrl.DataBindings.Clear();
ctrl.DataBindings.Add(propertyName, bindSource, columnName, true, DataSourceUpdateMode.OnPropertyChanged);
}
}
public static void FormatGridForSearchResults(DataGridView grid)
{
grid.AllowUserToAddRows = false;
grid.ReadOnly = true;
grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
DoFormatGrid(grid);
}
public static void FormatGridForEdit(DataGridView grid)
{
grid.EditMode = DataGridViewEditMode.EditOnEnter;
DoFormatGrid(grid);
}
private static void DoFormatGrid(DataGridView grid)
{
grid.BackgroundColor = Control.DefaultBackColor;
grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
grid.RowHeadersWidth = 25;
foreach (DataGridViewColumn col in grid.Columns)
{
if (col.Name.EndsWith("Id"))
{
col.Visible = false;
}
}
}
public static int GetPkIdFromGrid(DataGridView grid, string pkCol, int rowIndex)
{
int id = 0;
if (rowIndex < grid.Rows.Count && grid.Columns.Contains(pkCol) && grid.Rows[rowIndex].Cells[pkCol].Value != DBNull.Value)
{
if (grid.Rows[rowIndex].Cells[pkCol].Value is int)
{
id = (int)grid.Rows[rowIndex].Cells[pkCol].Value;
}
}
return id;
}
public static int GetPkIdFromComboBox(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.Insert(grid.ColumnCount, new DataGridViewButtonColumn()
{
Text = "X",
HeaderText = "Delete",
Name = deleteColName,
UseColumnTextForButtonValue = true
});
}
public static bool FormIsOpenAlredy(Type frmType, int pkValue = 0)
{
foreach (Form frm in Application.OpenForms)
{
int frmPkValue = 0;
if (frm.Tag is int)
{
frmPkValue = (int)frm.Tag;
}
if (frm.GetType() == frmType && pkValue == frmPkValue)
{
frm.Activate();
return true;
}
}
return false;
}
public static void SetUpNav(ToolStrip ts)
{
ts.Items.Clear();
foreach (Form f in Application.OpenForms)
{
if (!f.IsMdiContainer)
{
ToolStripButton btn = new() { Text = f.Text, 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 is ToolStripButton)
{
ToolStripButton btn = (ToolStripButton)sender;
if (btn.Tag != null && btn.Tag is Form)
{
((Form)btn.Tag).Activate();
}
}
}
}
}