-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFormSeraph.cs
147 lines (132 loc) · 6.21 KB
/
FormSeraph.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
namespace Seraph
{
public partial class FormSeraph : Form
{
List<Handler> m_handlers = new List<Handler>();
ListSortDirection m_direction = ListSortDirection.Ascending;
DatagridViewCheckBoxHeaderCell m_cbHeader;
public FormSeraph()
{
InitializeComponent();
m_tbName.Text = ""; // Trigger Text Changed
m_dgvHandler.DataSource = m_handlers; // Initialize the grid
// Fill the last column to match the resize of the form
m_dgvHandler.Columns[m_dgvHandler.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
// The grid is readonly except for the 1st column
foreach (DataGridViewColumn col in m_dgvHandler.Columns)
col.ReadOnly = true;
DataGridViewColumn selectedCol = m_dgvHandler.Columns[0];
selectedCol.ReadOnly = false;
selectedCol.Width = 50;
// Add an event on the header cell to sort the grid
m_dgvHandler.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(m_dgvHandler_ColumnHeaderMouseClick);
// CheckBox event handler walkaround https://stackoverflow.com/questions/11843488/datagridview-checkbox-event
m_dgvHandler.CellContentClick += new DataGridViewCellEventHandler(m_dgvHandler_CellContentClick);
m_dgvHandler.CellValueChanged += new DataGridViewCellEventHandler(m_dgvHandler_CellValueChanged);
}
private void FormSeraph_Load(object sender, EventArgs e)
{
m_dgvHandler.DataSource = m_handlers;
}
private void m_bHandle_Click(object sender, EventArgs e)
{
// Find handle for the entered name
m_handlers = Command.Handle(m_tbName.Text.Trim()).ToList();
m_dgvHandler.DataSource = m_handlers; // Update the DataSource
}
private void m_tbName_TextChanged(object sender, EventArgs e)
{
// Disable handle button is there is no name
m_bHandle.Enabled = !string.IsNullOrWhiteSpace(m_tbName.Text);
}
private void m_bClose_Click(object sender, EventArgs e)
{
// Close selected handle
foreach(Handler handler in m_handlers)
{
if (handler.Selected)
Command.Close(handler);
}
m_bHandle_Click(sender, e); // Callback Handle command to see if there is still handle remaining
}
// Called whenever the DataSource is changed
private void m_dgvHandler_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
// Remove the text header of the first column and replace it by a "(Un)Select All" checkbox
m_cbHeader = new DatagridViewCheckBoxHeaderCell();
m_cbHeader.Value = "";
m_cbHeader.OnCheckBoxClicked += new CheckBoxClickedHandler(m_cbHeader_CheckBoxClicked);
m_dgvHandler.Columns[0].HeaderCell = m_cbHeader;
m_dgvHandler.ClearSelection();
}
public void m_cbHeader_CheckBoxClicked(bool bState)
{
// Propagate the state of the checbox header to all checkbox in the column
m_dgvHandler.ClearSelection();
foreach (Handler handler in m_handlers)
handler.Selected = bState;
m_dgvHandler.Update();
m_dgvHandler.Refresh();
m_bClose.Select(); // Set the focus on Close button
}
private void m_dgvHandler_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0) // Only for selected column
m_dgvHandler.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void m_dgvHandler_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
m_cbHeader.IsDirty = true; // Mix between selected and unselected
m_dgvHandler.Invalidate(); // Trigger the Paint() event
}
private void m_dgvHandler_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
// Sort by column, depending which column was clicked
if (e.ColumnIndex == 1)
{
if (m_direction == ListSortDirection.Ascending)
m_handlers = m_handlers.OrderBy(h => h.Process).ToList();
else
m_handlers = m_handlers.OrderByDescending(h => h.Process).ToList();
}
else if (e.ColumnIndex == 2)
{
if (m_direction == ListSortDirection.Ascending)
m_handlers = m_handlers.OrderBy(h => h.Pid).ToList();
else
m_handlers = m_handlers.OrderByDescending(h => h.Pid).ToList();
}
else if (e.ColumnIndex == 3)
{
if (m_direction == ListSortDirection.Ascending)
m_handlers = m_handlers.OrderBy(h => h.Type).ToList();
else
m_handlers = m_handlers.OrderByDescending(h => h.Pid).ToList();
}
else if (e.ColumnIndex == 4)
{
if (m_direction == ListSortDirection.Ascending)
m_handlers = m_handlers.OrderBy(h => h.Handle).ToList();
else
m_handlers = m_handlers.OrderByDescending(h => h.Handle).ToList();
}
else if (e.ColumnIndex == 5)
{
if (m_direction == ListSortDirection.Ascending)
m_handlers = m_handlers.OrderBy(h => h.Path).ToList();
else
m_handlers = m_handlers.OrderByDescending(h => h.Path).ToList();
}
else
return;
m_direction = m_direction == ListSortDirection.Ascending
? ListSortDirection.Descending : ListSortDirection.Ascending;
m_dgvHandler.DataSource = m_handlers;
}
}
}