forked from KevinSchott/SecondSight
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGuiEventView.cs
More file actions
214 lines (193 loc) · 8.42 KB
/
GuiEventView.cs
File metadata and controls
214 lines (193 loc) · 8.42 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2009, 2010, 2011 Kevin Schott
// This file is part of SecondSight.
// SecondSight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// SecondSight is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with SecondSight. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
namespace SecondSight
{
public partial class MainForm
{
private enum SearchFieldType { INT, FLOAT, DATE };
/// <summary>
/// Click event for the Search By Field button
/// </summary>
/// <remarks>
/// Finds the nearest entry in the data grid view that corresponds to the search parameters
/// and sorts the DGV by the column by which the user searches.
/// </remarks>
private void btn_V_SearchByFieldField_Click(object sender, EventArgs e)
{
SearchFieldType sft;
int position = 0; //Position of the nearest match
string colval = cb_V_SearchByField.SelectedValue.ToString();
//Determine the type of search field we're checking. SKU and axis are integers, others are floats, except for dates
if (cb_V_SearchByField.SelectedIndex == 0 || cb_V_SearchByField.SelectedIndex == 3 || cb_V_SearchByField.SelectedIndex == 7)
{ //SKU or OD/OS Axis selected
sft = SearchFieldType.INT;
}
else if (cb_V_SearchByField.SelectedIndex < 9)
{ //Any other non-date selection
sft = SearchFieldType.FLOAT;
}
else
{ //Dates
sft = SearchFieldType.DATE;
}
//Search routine, separate branch for INT, FLOAT and DATE
if (sft == SearchFieldType.INT)
{ //Search for INT types
int searchval = 0;
try
{
searchval = Convert.ToInt16(tb_V_SearchByField.Text);
}
catch
{
MessageBox.Show("Search value must be a positive whole number.", "Invalid Search Value", MessageBoxButtons.OK, MessageBoxIcon.Error);
tb_V_SearchByField.SelectAll();
tb_V_SearchByField.Focus();
return;
}
//Sort the list in ascending order by column to be searched
dgv_V_InventoryView.Sort(dgv_V_InventoryView.Columns[colval], ListSortDirection.Ascending);
//Loop through each row, break if we find a value greater or equal to the target value.
foreach (DataRowView drv in bs_V_InventorySource)
{
if (Convert.ToInt16(drv[colval]) >= searchval)
{
break;
}
position++;
}
}
else if (sft == SearchFieldType.FLOAT)
{ //Search for FLOAT types
float searchval = 0;
try
{
searchval = Convert.ToSingle(tb_V_SearchByField.Text);
}
catch
{
MessageBox.Show("Search value must be a real number.", "Invalid Search Value", MessageBoxButtons.OK, MessageBoxIcon.Error);
tb_V_SearchByField.SelectAll();
tb_V_SearchByField.Focus();
return;
}
if (searchval < 0)
{
//Sort the list in descending order by column to be searched
dgv_V_InventoryView.Sort(dgv_V_InventoryView.Columns[colval], ListSortDirection.Descending);
//Loop through each row, break if we find a value greater or equal to the target value.
foreach (DataRowView drv in bs_V_InventorySource)
{
if (Convert.ToSingle(drv[colval]) <= searchval)
{
break;
}
position++;
}
}
else
{
//Sort the list in ascending order by column to be searched
dgv_V_InventoryView.Sort(dgv_V_InventoryView.Columns[colval], ListSortDirection.Ascending);
//Loop through each row, break if we find a value greater or equal to the target value.
foreach (DataRowView drv in bs_V_InventorySource)
{
if (Convert.ToSingle(drv[colval]) >= searchval)
{
break;
}
position++;
}
}
}
else
{ //Search for DATE types
DateTime searchval = dtp_V_SearchByDate.Value.Date;
dgv_V_InventoryView.Sort(dgv_V_InventoryView.Columns[colval], ListSortDirection.Ascending);
//Loop through each row, break if we find a value greater or equal to the target value.
foreach (DataRowView drv in bs_V_InventorySource)
{
if (Convert.ToDateTime(drv[colval]).Date >= searchval)
{
break;
}
position++;
}
}
//Jump to the area in the datagridview that contains the search results
dgv_V_InventoryView.ClearSelection();
if (position > 1)
{
dgv_V_InventoryView.FirstDisplayedScrollingRowIndex = position - 2;
}
else
{
dgv_V_InventoryView.FirstDisplayedScrollingRowIndex = position;
}
tb_V_SearchByField.Focus();
}
/// <summary>
/// SelectedIndexChanged event for cb_V_SearchIn
/// </summary>
/// <remarks>
/// Changes inventory table displayed by the datagridview in the View Inventory tab, either Current Inventory or Dispensed Inventory.
/// Adds or removes the "Dispensed Date" option in the cb_V_SearchByField combobox as appropriate.
/// </remarks>
private void cb_V_SearchIn_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (cb_V_SearchIn.SelectedIndex == 0)
{ //Current Inventory selected
dgv_V_InventoryView.Columns["DateDispensed"].Visible = false;
bs_V_InventorySource.DataSource = Mydb.InvResults;
bs_V_SearchByField.RemoveAt(10);
}
else
{ //Dispensed Inventory selected
dgv_V_InventoryView.Columns["DateDispensed"].Visible = true;
bs_V_InventorySource.DataSource = dt_V_DispensedTable;
bs_V_SearchByField.Add(new KeyValuePair<string, string>("Date Dispensed", "DateDispensed"));
}
}
catch { }
}
/// <summary>
/// SelectedIndexChanged event for the cb_V_SearchByField
/// </summary>
/// <remarks>
/// Alters the visibility and position of controls based on whether a date or non-date field is selected.
/// If a date field is selected, the DateTimePicker is shown, otherwise the TextBox is shown.
/// </remarks>
private void cb_V_SearchByField_SelectedIndexChangd(object sender, EventArgs e)
{
if (cb_V_SearchByField.SelectedIndex > 8)
{
panel_V_SearchBy.Width = 274;
dtp_V_SearchByDate.Visible = true;
tb_V_SearchByField.Visible = false;
}
else
{
panel_V_SearchBy.Width = 175;
dtp_V_SearchByDate.Visible = false;
tb_V_SearchByField.Visible = true;
}
}
}
}