-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataWindow.cs
250 lines (228 loc) · 7.65 KB
/
DataWindow.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 巧用_ToolStripControlHost_自定义WinForm右键菜单
{
public partial class DataWindow : ComboBox
{
private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
public event AfterSelectorEventHandler AfterSelector;
private bool m_blDropShow = false;
private int m_SelectedIndex = -1;
private bool m_blPopupAutoSize = true;
private ToolStripControlHost dataGridViewHost;
private ToolStripControlHost textBoxHost;
private ToolStripDropDown dropDown;
/// <summary>
/// 保存选择的id
/// </summary>
public object value { get; set; }
public DataWindow()
{
DrawDataGridView();
}
/// <summary>
/// 设置数据源
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
public void setDataSource<T>(List<T> list)
{
DataGridView.Columns.Clear();
//DataGridView.AutoGenerateColumns = false;
DataGridView.DataSource = list;
}
object _DataSource;
public new object DataSource
{
get
{
return DataGridView.DataSource;
}
set
{
DataGridView.DataSource = value;
_DataSource = value;
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
ShowDropDown();
}
/// <summary>
/// 取值字段
/// </summary>
public string sValueMember { get; set; }
/// <summary>
/// 下拉表格显示列,空为显示所有列!
/// </summary>
public string sDisplayField { get; set; }
/// <summary>
/// 查询字段
/// </summary>
public string sKeyWords { get; set; }
private void ShowDropDown()
{
if (dropDown != null)
{
if (DataSource != null)
{
//DataView.RowFilter = "";
TextBox.Text = "";
textBoxHost.Width = DropDownWidth;
dataGridViewHost.AutoSize = true;
dataGridViewHost.Size = new Size(DropDownWidth - 2, DropDownHeight);
dropDown.Show(this, 0, this.Height);
TextBox.Focus();
}
}
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar) || char.IsLetter(e.KeyChar))
{
this.TextBox.Text = e.KeyChar.ToString();
this.TextBox.SelectionStart = this.TextBox.Text.Length;
e.Handled = true;
}
base.OnKeyPress(e);
}
private void DrawDataGridView()
{
DataGridView dataGridView = new DataGridView();
dataGridView.ScrollBars = ScrollBars.Both;
dataGridView.AutoSize = true;
dataGridView.AllowUserToAddRows = false;
dataGridView.AllowUserToDeleteRows = false;
dataGridView.AllowUserToResizeRows = false;
dataGridView.Dock = DockStyle.Fill;
dataGridView.BackgroundColor = SystemColors.Control;
dataGridView.BorderStyle = BorderStyle.None;
dataGridView.ReadOnly = true;
dataGridView.AllowUserToAddRows = false;
dataGridView.RowHeadersVisible = false;
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView.DoubleClick += new EventHandler(dataGridView_DoubleClick);
dataGridView.KeyDown += new KeyEventHandler(dataGridView_KeyDown);
//设置DataGridView的数据源
Form frmDataSource = new Form();
frmDataSource.Controls.Add(dataGridView);
frmDataSource.SuspendLayout();
dataGridViewHost = new ToolStripControlHost(dataGridView);
dataGridViewHost.AutoSize = m_blPopupAutoSize;
TextBox textBox = new TextBox();
textBox.BackColor = Color.Red;
textBox.Width = dataGridView.Width;
textBox.TextChanged += new EventHandler(textBox_TextChanged);
textBox.KeyDown += new KeyEventHandler(textBox_KeyDown);
textBoxHost = new ToolStripControlHost(textBox);
textBoxHost.AutoSize = false;
dropDown = new ToolStripDropDown();
dropDown.Items.Add(textBoxHost);
dropDown.Items.Add(dataGridViewHost);
}
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode== Keys.Enter)
{
PopupGridView(e);
}
}
private void textBox_TextChanged(object sender, EventArgs e)
{
//throw new NotImplementedException();
}
private void dataGridView_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
PopupGridView(e);
}
}
private void dataGridView_DoubleClick(object sender, EventArgs e)
{
PopupGridView(e);
}
protected void OnAfterSelector(object sender, AfterSelectorEventArgs e)
{
if (AfterSelector != null)
{
AfterSelector(sender, e);
}
}
protected virtual void RaiseAfterSelector(object sender, AfterSelectorEventArgs e)
{
OnAfterSelector(sender, e);
}
private void PopupGridView(EventArgs e)
{
if (DataGridView.SelectedRows.Count > 0)
{
DataGridViewRow dgvRow = DataGridView.CurrentRow;
m_SelectedIndex = DataGridView.CurrentRow.Index;
this.value = dgvRow.Cells[0].Value;//此处默认第1列为 key
this.Text = dgvRow.Cells[1].Value?.ToString(); //此处默认第2列为 文本
RaiseAfterSelector(this, new AfterSelectorEventArgs(-1, -1, dgvRow));
dropDown.Close();
m_blDropShow = false;
}
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN)
{
if (m_blDropShow)
{
m_blDropShow = false;
}
else
{
m_blDropShow = true;
}
if (m_blDropShow)
{
ShowDropDown();
}
else
{
dropDown.Close();
}
return;
}
base.WndProc(ref m);
}
public DataGridView DataGridView
{
get
{
return dataGridViewHost.Control as DataGridView;
}
}
public TextBox TextBox
{
get
{
return textBoxHost.Control as TextBox;
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (dropDown != null)
{
dropDown.Dispose();
dropDown = null;
}
}
base.Dispose(disposing);
}
}
}