-
Notifications
You must be signed in to change notification settings - Fork 17
/
Form1.cs
349 lines (314 loc) · 9.96 KB
/
Form1.cs
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace DarkModeForms
{
public partial class Form1 : Form
{
private bool _isDarkMode = false;
private bool mCloseAutorized = false;
private BindingList<ExampleDataSource> DS = null;
private DarkModeCS dm = null;
public Form1()
{
InitializeComponent();
dm = new DarkModeCS(this)
{
// Choose your preferred mode here:
ColorMode = DarkModeCS.DisplayMode.SystemDefault
};
}
private void Form1_Load(object sender, EventArgs e)
{
// Prepare a Datasource for the GridView control
DS = new BindingList<ExampleDataSource>
{
new ExampleDataSource()
{
Sequence = 1,
Name = "Philip J. Fry",
IsAlive = true,
Points = 20000,
Observations = "Missing in Action"
},
new ExampleDataSource()
{
Sequence = 2,
Name = "Turanga Leela",
IsAlive = true,
Points = 23000,
Observations = "on Duty"
},
new ExampleDataSource()
{
Sequence = 3,
Name = "Prof. Hubert Farnsworth",
IsAlive = false,
Points = 0,
Observations = "RIP"
},
new ExampleDataSource()
{
Sequence = 4,
Name = "Zapp Brannigan",
IsAlive = true,
Points = 7000,
Observations = "on Duty"
},
};
dataGridView1.AutoGenerateColumns = true;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.DataSource = DS;
if (dataGridView1.Rows.Count > 0)
{
dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[1];
}
comboBox1.Items.Clear();
comboBox1.ValueMember = "Sequence";
comboBox1.DisplayMember = "Name";
comboBox1.DataSource = DS;
comboBox2.Items.Clear();
comboBox2.ValueMember = "Sequence";
comboBox2.DisplayMember = "Name";
comboBox2.DataSource = DS;
listView2.Items.Clear();
listView2.Columns.Add("Name", 200);
foreach (var item in DS)
{
listView2.Items.Add(item.Name);
}
treeView1.Nodes[0].Expand();
tabControl1.SelectTab(1);
//Manually Re-Coloring Images on the ListView Control, or any other Control
//treeView1.ImageList = null;
//int index = 0;
//foreach (Image image in imageList2.Images)
//{
// var coloredImage = DarkModeCS.ChangeToColor(image, DM.OScolors.TextInactive);
// imageList2.Images.RemoveAt(index);
// imageList2.Images.Add(coloredImage);
// index++;
//}
//treeView1.ImageList = imageList2;
string myDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
ListDirectory(this.treeView1, myDocumentsPath, "*.*");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//Al intentar cerrar la ventana se minimiza en la bandeja 'SysTray'
if (e.CloseReason == CloseReason.UserClosing)
{
//if (!this.mCloseAutorized)
//{
// e.Cancel = true;
// base.WindowState = FormWindowState.Minimized;
//}
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//Al Cerrar definitivamente el Formulario elimina el icono del SysTray
this.notifyIcon1.Dispose();
}
private void Form1_Resize(object sender, EventArgs e)
{
//Al Minimizar la Ventana, se oculta y se minimiza en la bandeja 'SysTray'
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
//Muestra una Notificacion en la Bandeja del Sistema:
this.notifyIcon1.Visible = true;
this.notifyIcon1.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
this.notifyIcon1.BalloonTipTitle = "DarkMode Forms";
this.notifyIcon1.BalloonTipText = "The App will be running hidden";
this.notifyIcon1.ShowBalloonTip(5000); //<- Ocultar tras 5 segundos.
}
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
//Doble Click en el Icono del SysTray para Mostrar la Ventana
this.Show();
base.WindowState = FormWindowState.Normal;
this.BringToFront();
}
private int CtrlCounter = 0;
// Adds a new Control to the Form, to test if Darkmode is applied to dynamically added controls, it does!
private void button1_Click(object sender, EventArgs e)
{
var newT = new TextBox()
{
Name = "",
Text = "New TextBox",
Location = new Point((this.ClientSize.Width / 2) + CtrlCounter, (this.ClientSize.Height / 2) + CtrlCounter)
};
this.Controls.Add(newT);
newT.BringToFront();
newT.Focus();
CtrlCounter = CtrlCounter + 10;
//comboBox1.Enabled = true;
}
// Default MessageBox, for comparing, and Custom Error showing
private void button2_Click(object sender, EventArgs e)
{
try
{
MessageBox.Show("Sadly its not possible to change\r\nthe default MessageBoxes :(", "Hello World!",
MessageBoxButtons.OK, MessageBoxIcon.Information);
throw new Exception("Hello!");
}
catch (Exception ex)
{
Messenger.MessageBox(ex);
}
}
// Example of the Custom InputBox
private void button3_Click(object sender, EventArgs e)
{
// Definition of a Single Field:
var BooleanControl = new KeyValue("Boolean", "true", KeyValue.ValueTypes.Boolean);
// Can Validate User Inputs on each Field:
BooleanControl.Validate += (object _control, KeyValue.ValidateEventArgs _e) =>
{
string OldValue = _e.OldValue;
if (_e.NewValue == "False")
{
//_e.Cancel = true; //<- CAN CANCEL THE MODIFICATION
_e.ErrorText = "No puede ser Falso!";
}
};
// Custom Types for 'Dynamic' Fields:
List<KeyValue> Dtypes = new List<KeyValue>
{
new KeyValue("RichText Format", "0"),
new KeyValue("Plain Text", "1"),
new KeyValue("AccountManager", "2")
};
// Definition of Multiple Fields:
List<KeyValue> _Fields = new List<KeyValue>
{
new KeyValue("String", "String", KeyValue.ValueTypes.String),
new KeyValue("Password","Password", KeyValue.ValueTypes.Password),
new KeyValue("Integer", "1000", KeyValue.ValueTypes.Integer),
new KeyValue("Decimal", "3,141638", KeyValue.ValueTypes.Decimal),
BooleanControl,
new KeyValue("Dynamic", "1", KeyValue.ValueTypes.Dynamic, Dtypes),
};
// Dialog Show:
if (Messenger.InputBox("Custom InputBox", "Please Fill the Form:", ref _Fields,
MsgIcon.Edit, MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Debug.WriteLine(string.Format("The New Password is: '{0}'", _Fields[1].Value));
}
}
// Example of a Login Form with Password Validation:
private void button4_Click(object sender, EventArgs e)
{
List<KeyValue> _Fields = new List<KeyValue>
{
new KeyValue("User Name", "user", KeyValue.ValueTypes.String),
new KeyValue("Password", string.Empty, KeyValue.ValueTypes.Password)
};
// Validate All Controls before Closing the Dialog:
Messenger.ValidateControls += (object Sender, KeyValue.ValidateEventArgs E) =>
{
string _userName = _Fields[0].Value;
string _password = _Fields[1].Value;
bool _isValid = false;
//TODO: Here you should send the User/Password to your BackEnd for Validation
_isValid = _password == "password";
if (_isValid)
{
_Fields[1].ErrorText = "Incorrect Password!";
E.Cancel = true; //<- Prevents the Dialog to be closed until is valid
}
};
// Dialog Show:
if (Messenger.InputBox("Login", "Please Input your Credentials:", ref _Fields,
MsgIcon.Lock, MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Messenger.MessageBox(string.Format("The User '{0}' is Logged!", _Fields[0].Value), "Login Correct!",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
// Example of the Custom MessageBox
private void button5_Click(object sender, EventArgs e)
{
if (Messenger.MessageBox("Hello World!", "You got a Message:",
MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
{
//Do Something here.
}
}
private void radioButtonSystem_CheckedChanged(object sender, EventArgs e)
{
if (radioButtonSystem.Checked)
{
//dm.DarkModePolicy = DarkModePolicy.FollowSystemTheme;
dm.ColorMode = DarkModeCS.DisplayMode.ClearMode;
_isDarkMode = dm.isDarkMode();
}
else if (radioButtonDark.Checked)
{
//dm.DarkModePolicy = DarkModePolicy.ForceDarkTheme;
dm.ColorMode = DarkModeCS.DisplayMode.DarkMode;
_isDarkMode = true;
}
else if (radioButtonLight.Checked)
{
//dm.DarkModePolicy = DarkModePolicy.ForceLightTheme;
dm.ColorMode = DarkModeCS.DisplayMode.ClearMode;
_isDarkMode = false;
}
dm.ApplyTheme(_isDarkMode);
}
private void mnuSalir_Click(object sender, EventArgs e)
{
//Esta es la forma correcta de Cerrar la Aplicacion, aparte de Apagar el PC
if (Messenger.MessageBox("Desea Cerrar este programa?", "Confirmar Salida", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
this.mCloseAutorized = true;
this.Close();
}
}
//Agregar estructura de archivos y directorios recursivamente:
private void ListDirectory(TreeView treeView, string path, string filter)//<- filter = "*.exe;*.jpg"
{
treeView.Nodes.Clear();
DirectoryInfo rootDirectoryInfo = new DirectoryInfo(path);
treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo, filter));
}
private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo, string filter)
{
TreeNode directoryNode = new TreeNode(directoryInfo.Name);
foreach (DirectoryInfo directory in directoryInfo.GetDirectories())
{
try
{
directoryNode.Nodes.Add(CreateDirectoryNode(directory, filter));
}
catch { }
}
foreach (FileInfo file in directoryInfo.GetFiles(filter))
{
try
{
directoryNode.Nodes.Add(new TreeNode(file.Name));
}
catch { }
}
return directoryNode;
}
}
internal class ExampleDataSource
{
public int Sequence { get; set; } = 0;
public string Name { get; set; } = string.Empty;
public bool IsAlive { get; set; } = false;
public decimal Points { get; set; } = 0;
public string Observations { get; set; } = string.Empty;
}
}