-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormUtilities.cs
More file actions
200 lines (174 loc) · 6.66 KB
/
FormUtilities.cs
File metadata and controls
200 lines (174 loc) · 6.66 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RugFactory
{
static class FormUtilities
{
public static object cboCrudSearchColumn { get; private set; }
public static bool CheckEnterKeyPressed(KeyEventArgs e)
{
// Enter key pressed or not
return (e.KeyCode == Keys.Enter);
}
public static void ShowDialog(Form parent, Form child)
{
child.ShowDialog(parent);
}
public static void TuggleButtonText(Button target, string toTuggle)
{
target.Text = toTuggle;
}
public static void CleanAllTextBoxes(Form targetToClean)
{
var textBoxes = targetToClean.Controls.OfType<TextBox>();
foreach(TextBox t in textBoxes)
{
t.Text = string.Empty;
t.BackColor = Color.White;
}
}
public static void CleanErrorProviderMessage(ErrorProvider provider, Form formToClean)
{
foreach (Control c in formToClean.Controls)
{
provider.SetError(c, null);
}
}
public static void PrepeareDataGridViewForSearch(DataGridView dataGridView)
{
dataGridView.CurrentCell = null;
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView.ReadOnly = true;
dataGridView.AllowUserToAddRows = false;
dataGridView.ClearSelection();
}
public static void SearchAGivenDataGridView(DataGridView dataGridView, string query)
{
try
{
foreach (DataGridViewRow row in dataGridView.Rows)
{
row.Visible = false;
foreach (DataGridViewCell cell in row.Cells)
{
if ( cell.FormattedValue.ToString().Contains(query))
{
row.Visible = true;
break;
}
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
public static void ResetDataGridView(DataGridView target)
{
foreach (DataGridViewRow row in target.Rows)
{
row.Visible = true;
}
}
public static Int32 GetIdFromAutoCompleteToolTip(string s)
{
//String will come in this format : "Name :{0}", ID; And we want to extract the ID
int indexOfSeperator = s.IndexOf(',') ;
if (indexOfSeperator == -1) return -1;
return Int32.Parse(s.Substring(indexOfSeperator + 1) );
}
public static string GetNameFromAutoCompleteToolTip(string s)
{
//String will come in this format : "Name :{0}", ID; And we want to extract the ID
int indexOfSeperator = s.IndexOf(',');
if (indexOfSeperator == -1) return string.Empty;
return s.Substring(0, indexOfSeperator);
}
public static void MakeRedIFChecked(CheckBox c)
{
c.ForeColor = c.Checked ? Color.Red : Color.White;
}
public static void BoldME(Control sender, bool tuggle)
{
sender.Font = new Font(sender.Font.FontFamily,
tuggle? sender.Font.Size +3: sender.Font.Size -3,
tuggle? FontStyle.Bold: FontStyle.Regular);
sender.Location = new Point(sender.Location.X,
tuggle? sender.Location.Y -3 : sender.Location.Y +3
);
}
public static void AllowOnlyDigit( KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) ) e.Handled = true;
}
public static void AllowOnlyString( KeyPressEventArgs e)
{
if (!char.IsLetter(e.KeyChar) && !char.IsControl(e.KeyChar)) e.Handled = true;
}
/// <summary>
/// Eanable all Controls Except the provided excepted
/// </summary>
/// <param name="target"></param>
/// <param name="exceptThis"></param>
public static void EnableControlers (Form target, Control exceptThis)
{
foreach (Control item in target.Controls)
{
item.Enabled = true;
if (item == exceptThis) item.Enabled = false;
}
}
/// <summary>
/// Tuggle Enable/Disable the List of given Controls
/// </summary>
/// <param name="controls"></param>
public static void EnableControlers(List<Control> controls, bool status)
{
foreach (Control item in controls)
{
item.Enabled = status;
}
}
public static void DisabaleControles( Form target,Control exceptThis)
{
foreach (Control item in target.Controls)
{
item.Enabled = false;
if (item == exceptThis || item.GetType() ==typeof(Label)) item.Enabled = true;
}
}
public static List<CheckBox> GetChecBoxes(Form target,string tag=null)
{
List<CheckBox> checkBoxes = new List<CheckBox>();
bool test = false;
foreach (var item in target.Controls)
{
//Optioanl Tag :
if (item is CheckBox && (tag==null || ((CheckBox)item).Tag.Equals(tag)))
{
checkBoxes.Add((CheckBox)item);
}
}
return checkBoxes;
}
public static List<Int32> GetChecedBoxesId(List<CheckBox> checkBoxes)
{
List<Int32> ids = new List<int>();
foreach (CheckBox item in checkBoxes)
{
if (item.Checked)
{
Int32 id = Int32.Parse(item.Name);
ids.Add(id);
}
}
return ids;
}
}
}