-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsFormsUtility.cs
More file actions
121 lines (113 loc) · 3.99 KB
/
WindowsFormsUtility.cs
File metadata and controls
121 lines (113 loc) · 3.99 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
using System;
using System.Collections.Generic;
using System.Data;
using System.DirectoryServices.ActiveDirectory;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CPUWindowsFormsFramework
{
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);
lst.DataBindings.Add("SelectedValue", targetdt, 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);
switch (controltype)
{
case "txt":
case "lbl":
propertyname = "Text";
break;
case "dtp":
propertyname = "Value";
break;
}
if (controlname.StartsWith("txt") || controlname.StartsWith("lbl"))
{
columnname = controlname.Substring(3);
}
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;
string pkname = tablename + "Id";
if (grid.Columns.Contains(pkname))
{
grid.Columns[pkname].Visible = false;
}
}
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();
}
}
}
}
}