/
PageControl.cs
248 lines (213 loc) · 7.13 KB
/
PageControl.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
using System;
using System.Collections.ObjectModel;
using DevExpress.XtraEditors;
using Insight.Base.BaseForm.Entities;
namespace Insight.Base.BaseForm.Controls
{
public partial class PageControl : XtraUserControl
{
/// <summary>
/// 每页行数选项集合
/// </summary>
private Collection<string> pageSizes = new Collection<string> {"20", "40", "60", "80", "100"};
/// <summary>
/// 总行数
/// </summary>
private int rows;
/// <summary>
/// 当前行(0 - rows)
/// </summary>
private int currentRow;
/// <summary>
/// 总页数
/// </summary>
private int pages => size > 0 ? ((rows - 1) / size) + 1 : 1;
/// <summary>
/// 当前页(0 - pages)
/// </summary>
private int currentPage;
/// <summary>
/// 表示将处理当前焦点行数据发生改变事件的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public delegate void DataChangedHandle(object sender, RowHandleEventArgs e);
/// <summary>
/// 表示将处理列表数据需重新加载事件的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public delegate void ReloadPageHandle(object sender, ReloadPageEventArgs e);
/// <summary>
/// 当前焦点行数据发生改变,通知刷新列表数据
/// </summary>
public event DataChangedHandle dataChanged;
/// <summary>
/// 当前页需要重新加载,通知重新加载列表数据
/// </summary>
public event ReloadPageHandle reloadPage;
/// <summary>
/// 每页行数下拉列表选项
/// </summary>
public Collection<string> pageSizeItems
{
get => pageSizes;
set
{
pageSizes = value;
cbeRows.Properties.Items.AddRange(value);
cbeRows.SelectedIndex = 0;
size = int.Parse(pageSizes[0]);
}
}
/// <summary>
/// 排序方式
/// </summary>
public OrderBy orderBy { get; set; } = OrderBy.POSITIVE;
/// <summary>
/// 页码(1 - pages)
/// </summary>
public int page => currentPage + 1;
/// <summary>
/// 当前每页行数
/// </summary>
public int size { get; private set; }
/// <summary>
/// 当前选中行(0 - size)
/// </summary>
public int rowHandle
{
private get => rows > 0 ? currentRow % size : -1;
set => currentRow = currentPage * size + value;
}
/// <summary>
/// 总行数
/// </summary>
public int totalRows
{
set
{
rows = value;
refresh();
}
}
/// <summary>
/// 构造方法
/// </summary>
public PageControl()
{
InitializeComponent();
// 每页行数改变事件
cbeRows.EditValueChanged += (sender, args) =>
{
size = int.Parse(cbeRows.Text);
refresh(true);
};
// 导航按钮点击事件
btnFirst.Click += (sender, args) => changePage(1);
btnPrev.Click += (sender, args) => changePage(page - 1);
btnNext.Click += (sender, args) => changePage(page + 1);
btnLast.Click += (sender, args) => changePage(pages);
btnJump.Click += (sender, args) =>
{
txtPage.Visible = true;
txtPage.Select();
};
// 页跳转输入事件
txtPage.Leave += (sender, args) =>
{
txtPage.EditValue = null;
txtPage.Visible = false;
};
txtPage.KeyPress += (sender, args) =>
{
if (args.KeyChar == 27)
{
txtPage.EditValue = null;
txtPage.Visible = false;
return;
}
if (args.KeyChar != 13) return;
if (string.IsNullOrEmpty(txtPage.Text)) return;
var val = int.Parse(txtPage.Text.Trim());
if (val < 1 || val > pages || val == page)
{
txtPage.EditValue = null;
return;
}
txtPage.Visible = false;
btnJump.Text = val.ToString();
changePage(val);
};
}
/// <summary>
/// 增加列表成员
/// </summary>
/// <param name="count">增加数量,默认1个</param>
public void addItems(int count = 1)
{
currentRow = orderBy == OrderBy.POSITIVE ? rows : 0;
rows += count;
refresh();
}
/// <summary>
/// 减少列表成员
/// </summary>
/// <param name="count">减少数量,默认1个</param>
public void removeItems(int count = 1)
{
rows -= count;
if (rows > 0 && currentRow < 0) currentRow = 0;
if (currentRow >= rows) currentRow = rows - 1;
refresh(currentPage < rows / size);
}
/// <summary>
/// 切换当前页
/// </summary>
/// <param name="page">页码</param>
private void changePage(int page)
{
currentRow = (page - 1) * size;
currentPage = page - 1;
refresh(true);
}
/// <summary>
/// 刷新控件
/// </summary>
/// <param name="reload">页数据是否重新加载</param>
private void refresh(bool reload = false)
{
var cp = currentPage;
labRows.Text = $@" 行/页 | 共 {rows} 行 | 分 {pages} 页";
labRows.Refresh();
labRows.Focus();
// 根据当前选中行定位当前页并刷新导航按钮可用状态
currentPage = size == 0 ? 0 : currentRow / size;
if (currentPage < 0) currentPage = 0;
btnFirst.Enabled = currentPage > 0;
btnPrev.Enabled = currentPage > 0;
btnNext.Enabled = currentPage < pages - 1;
btnLast.Enabled = currentPage < pages - 1;
btnJump.Enabled = pages > 3;
btnJump.Width = (int) Math.Log10(page) * 7 + 18;
btnJump.Text = page.ToString();
// 根据是否需要重新加载页面或当前页是否改变,触发重新加载事件。否则刷新数据
if (reload || currentPage != cp)
{
reloadPage?.Invoke(this, new ReloadPageEventArgs(page, size, rowHandle));
}
else
{
dataChanged?.Invoke(this, new RowHandleEventArgs(rowHandle));
}
}
}
/// <summary>
/// 排序方式
/// </summary>
public enum OrderBy
{
POSITIVE,
REVERSE
}
}