1.先看界面效果,有问题,可以留言讨论。下载源码
2.然后看核心代码。
1.继承 ToolStripDropDown 这个类,承载自定义控件,设置双缓存减少闪烁
/// <summary> /// 重写ToolStripDropDown /// 使用双缓存减少闪烁 /// </summary> public class MyToolStripDropDown : ToolStripDropDown { protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x02000000;//双缓存 return cp; } } }
2.自定义搜索控件UCDropDown
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 TianBaoZhiYuan { public partial class UCDropDown : UserControl { public delegate void SelectChangedHandle(string[] data); public event SelectChangedHandle SelectChanged; public event SelectChangedHandle TextBoxKeyPressEnter; public void TextBoxEnterFindDataSourceExsists(string keyword) { DataTable table = DataView.Table; if (table == null) return; DataRow[] rows = table.Select($" {CallBackColumns}='{keyword.Trim()}'"); if(rows == null ||rows.Length==0) return; string selected= rows[0][0].ToString().Trim(); //TextBoxKeyPressEnter?.Invoke(selected); } public UCDropDown() { InitializeComponent(); } public string CallBackColumns { get; set; } = ""; public string KeyWordsFilter { get; set; } = ""; #region 数据过滤 public DataView DataView { get { DataTable dataTable = GetDataTableFromDataSource(); if (dataTable == null) { return null; } return dataTable.DefaultView; } } [Description("设置DataGridView属性"), Browsable(true), Category("Popup")] private DataTable GetDataTableFromDataSource() { object dataSource = Dgv.DataSource; return GetDataTableFromDataSource(dataSource); } /// <summary> /// 从DataGridView 获取数据表 /// </summary> /// <returns></returns> private DataTable GetDataTableFromDataSource(object dataSource) { if (dataSource is DataTable) { return (DataTable)dataSource; } else if (dataSource is DataView) { return ((DataView)dataSource).Table; } else if (dataSource is BindingSource) { object bind = ((BindingSource)dataSource).DataSource; if (bind is DataTable) { return (DataTable)bind; } else { return ((DataView)bind).Table; } } else { return null; } } private string GetRowFilterString(string sText) { if (string.IsNullOrEmpty(sText)) return ""; string sFilter = ""; if (CallBackColumns == String.Empty || CallBackColumns == null) { CallBackColumns = DataView.Table.Columns[0].ColumnName; } if (KeyWordsFilter == String.Empty) { KeyWordsFilter = CallBackColumns; } string[] sColumns = KeyWordsFilter.Split(','); foreach (string sColumn in sColumns) { sFilter += sColumn + " like " + "'%" + sText + "%'" + " or "; } sFilter = sFilter.Trim().TrimEnd("or".ToCharArray()); return sFilter; } private string m_Separator = "|"; private string GetStringArrayValues(DataGridViewRow dgvRow) { string data = ""; if (dgvRow == null || dgvRow.Index == -1) { return data; } string[] sDisplayList = CallBackColumns.Split(','); foreach (string sDisplay in sDisplayList) { if (Dgv.Columns.Contains(sDisplay)) { data += dgvRow.Cells[sDisplay].Value.ToString() + m_Separator; } } return data.Trim('|'); } #endregion #region 数据搜索 public void Query(string keyword) { if (DataView == null) return; DataView.RowFilter = GetRowFilterString(keyword); } #endregion #region 单击,双击,按下 private void Dgv_CellClick(object sender, DataGridViewCellEventArgs e) { CallBack(); } private void Dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { CallBack(); } private void Dgv_KeyDown(object sender, KeyEventArgs e) { if (e.KeyData == Keys.Enter) { CallBack(); } } private void CallBack() { DataGridViewRow dgvRow = Dgv.CurrentRow; string selectedValues = GetStringArrayValues(dgvRow);//获取选定的值 string[] data = selectedValues.Split('|'); SelectChanged?.Invoke(data); } #endregion /// <summary> /// 获取DataView可见列总宽度 /// </summary> public int DataGridViewWidth { get { int DataWidth = 0; foreach (DataGridViewColumn col in Dgv.Columns) { if (col.Visible) { DataWidth += col.Width; } } return DataWidth; } } #region 添加和更新 public delegate void AddDelegate(); public event AddDelegate AddEvent;//添加 private void btnAdd_Click(object sender, EventArgs e) { AddEvent?.Invoke(); } public delegate void UpdateDelegate(); public event UpdateDelegate UpdateEvent;//刷新 private void btnRefresh_Click(object sender, EventArgs e) { UpdateEvent?.Invoke(); } #endregion #region 自适应大小 //自适应 private void panelBottom_SizeChanged(object sender, EventArgs e) { btnAdd.Location = new Point(panelBottom.Location.X + 10, btnAdd.Location.Y); btnRefresh.Location = new Point(panelBottom.Location.X + panelBottom.Width - 10 - btnRefresh.Width, btnRefresh.Location.Y); } //自适应 private void QueryControl_SizeChanged(object sender, EventArgs e) { this.tbLayout.Width = this.Width - 1; this.tbLayout.Height= this.Height - 1; } #endregion } }
3.继承TextBox ,新建类 UCDropDownContainer
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Data; 6 using System.Text; 7 using System.Windows.Forms; 8 using System.Drawing.Design; 9 using System.Drawing.Drawing2D; 10 using System.Runtime.InteropServices; 11 using System.IO; 12 using System.Reflection; 13 14 namespace TianBaoZhiYuan 15 { 16 17 public class UCDropDownContainer : TextBox 18 { 19 #region 成员变量 20 public delegate void CloseOther(); 21 public event CloseOther CloseOtherEvent; 22 23 ToolStripControlHost CtrHost; 24 public MyToolStripDropDown Menu; 25 public delegate void SelectChangedHandle(string data); 26 public event SelectChangedHandle SelectChanged; 27 private string _SeparatorChar = "|"; 28 29 #endregion 30 31 #region 构造函数 32 UCDropDown dropDown; 33 private void DrawDataGridView() 34 { 35 dropDown = new UCDropDown(); 36 dropDown.Width = this.Width; 37 CtrHost = new ToolStripControlHost(dropDown); 38 CtrHost.AutoSize = true; 39 CtrHost.Margin = Padding.Empty; 40 CtrHost.Padding = Padding.Empty; 41 42 Menu = new MyToolStripDropDown(); 43 Menu.Width = this.Width; 44 Menu.AutoClose = false; 45 Menu.Items.Add(CtrHost); 46 // DropDown.Padding =new Padding(1); 47 } 48 private int _ShowOffSetY = 9; 49 [Description("显示弹窗偏移Y!"), Browsable(true), Category("Popup")] 50 public int ShowOffSetY 51 { 52 set 53 { 54 _ShowOffSetY = value; 55 } 56 get 57 { 58 return _ShowOffSetY; 59 } 60 } 61 public Action AfterOpenMenu = null;//打开后操作,列宽只能在显示的时候设置,才有效 62 public void OpenMenu(string keyword = "") 63 { 64 if (Menu != null) 65 { 66 if (DataView != null) 67 { 68 DataView.RowFilter = GetRowFilterString(keyword); 69 CtrHost.AutoSize = false; 70 CtrHost.Width = this.MenuWidth ; 71 Menu.AutoClose = false;//弹窗前设置自动关闭为False. TextBox才可以获得焦点继续输入 72 Menu.Show(this, -2, this.Height+ _ShowOffSetY); 73 Menu.AutoClose= true;//弹窗后,设置为True,才不会TopMost遮挡其他程序 74 AfterOpenMenu?.Invoke();//打开后,设置一下列宽 75 } 76 } 77 } 78 79 80 81 public UCDropDownContainer() 82 { 83 DrawDataGridView(); 84 this.TextChanged += txtKey_TextChanged; 85 this.MouseClick += txtKey_MouseClick; 86 87 } 88 private void txtKey_MouseClick(object sender, MouseEventArgs e) 89 { 90 CloseOtherEvent?.Invoke();//因为设置了AutoClose=false,所以点击的时候,要关闭其他的下拉弹窗 91 OpenMenu(""); 92 } 93 private void txtKey_TextChanged(object sender, EventArgs e) 94 { 95 if (DataView == null) return; 96 DataView.RowFilter = GetRowFilterString(Text); 97 } 98 protected override void OnKeyDown(KeyEventArgs e) 99 { 100 if (e.KeyData == Keys.Enter) 101 { 102 if (string.IsNullOrWhiteSpace(Text.Trim())) 103 { 104 CloseOtherEvent?.Invoke(); 105 OpenMenu(); 106 } 107 }else if (e.KeyData == Keys.Back) 108 { 109 if(DataView!=null) 110 DataView.RowFilter = GetRowFilterString(Text);//后退 111 } 112 else if(e.KeyData == Keys.Down){//向下 113 if (this.Menu.Visible == false) 114 { 115 OpenMenu(); 116 } 117 else 118 { 119 Dgv.Focus(); 120 } 121 } 122 base.OnKeyDown(e); 123 } 124 /// <summary> 125 /// 按关键词搜索 126 /// </summary> 127 /// <param name="keyword"></param> 128 public void SearchByKeyword(string keyword="") 129 { 130 if (DataView == null) return; 131 string key = keyword.Trim(); 132 if (string.IsNullOrEmpty(key)) 133 { 134 DataView.RowFilter = GetRowFilterString(Text); 135 } 136 else 137 { 138 DataView.RowFilter = GetRowFilterString(keyword); 139 } 140 } 141 142 #endregion 143 144 145 public void CloseMenu() 146 { 147 if (Menu != null) 148 { 149 Menu.Close(); 150 } 151 } 152 153 154 #region 提示语 155 /// <summary> 156 /// 提示 157 /// </summary> 158 private string _promptText = string.Empty; 159 /// <summary> 160 /// 提示语字体 161 /// </summary> 162 private Font _promptFont = new Font("微软雅黑", 12f, FontStyle.Regular, GraphicsUnit.Pixel); 163 164 /// <summary> 165 /// 提示语颜色 166 /// </summary> 167 private Color _promptColor = Color.Gray; 168 /// <summary> 169 /// 水印文字 170 /// </summary> 171 /// <value>The prompt text.</value> 172 [Description("水印文字"), Category("自定义")] 173 public string PromptText 174 { 175 get 176 { 177 return this._promptText; 178 } 179 set 180 { 181 this._promptText = value; 182 this.OnPaint(null); 183 } 184 } 185 /// <summary> 186 /// Gets or sets the prompt font. 187 /// </summary> 188 /// <value>The prompt font.</value> 189 [Description("水印字体"), Category("自定义")] 190 public Font PromptFont 191 { 192 get 193 { 194 return this._promptFont; 195 } 196 set 197 { 198 this._promptFont = value; 199 } 200 } 201 /// <summary> 202 /// Gets or sets the color of the prompt. 203 /// </summary> 204 /// <value>The color of the prompt.</value> 205 [Description("水印颜色"), Category("自定义")] 206 public Color PromptColor 207 { 208 get 209 { 210 return this._promptColor; 211 } 212 set 213 { 214 this._promptColor = value; 215 } 216 } 217 #endregion 218 219 220 221 #region 绘制DataGridView以及下拉DataGridView 222 223 public string CallBackColumns 224 { 225 get 226 { 227 return DropDown.CallBackColumns; 228 } 229 set 230 { 231 DropDown.CallBackColumns = value; 232 } 233 } 234 public string KeyWordsFilter 235 { 236 get 237 { 238 return DropDown.KeyWordsFilter; 239 } 240 set 241 { 242 DropDown.KeyWordsFilter = value; 243 } 244 } 245 public UCDropDown DropDown 246 { 247 get 248 { 249 return dropDown; 250 } 251 } 252 public DataGridView Dgv 253 { 254 get 255 { 256 return (CtrHost.Control as UCDropDown).Dgv; 257 } 258 } 259 /// <summary> 260 /// 获取DataView可见列总宽度 261 /// </summary> 262 public int DataGridViewTotalWidth 263 { 264 get 265 { 266 int DataWidth = 0; 267 foreach (DataGridViewColumn col in Dgv.Columns) 268 { 269 if (col.Visible) 270 { 271 DataWidth += col.Width; 272 } 273 } 274 return DataWidth; 275 } 276 } 277 private void DataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e) 278 { 279 int rowIndex = e.RowIndex; 280 if (rowIndex == -1) return; 281 int columnIndex = e.ColumnIndex; 282 283 if (Dgv.SelectedRows.Count > 0) 284 { 285 DataGridViewRow dgvRow = Dgv.CurrentRow; 286 Text = GetStringArrayValues(dgvRow); 287 SelectChanged?.Invoke(Text); 288 } 289 Menu.Close(); 290 291 } 292 293 private string GetStringArrayValues(DataGridViewRow dgvRow) 294 { 295 string data = ""; 296 if (dgvRow == null || dgvRow.Index == -1) 297 { 298 return data; 299 } 300 string[] sDisplayList = CallBackColumns.Split(','); 301 foreach (string sDisplay in sDisplayList) 302 { 303 if (Dgv.Columns.Contains(sDisplay)) 304 { 305 data += dgvRow.Cells[sDisplay].Value.ToString() + _SeparatorChar; 306 } 307 } 308 return data.Trim('|'); 309 } 310 #endregion 311 312 313 #region 属性 314 315 316 [Description("是否显示条件输入窗口!"), Browsable(true), Category("Popup")] 317 public bool RowFilterVisible 318 { 319 set 320 { 321 Menu.Items[0].Visible = value; 322 } 323 get 324 { 325 return Menu.Items[0].Visible; 326 } 327 } 328 329 public DataView DataView 330 { 331 get 332 { 333 DataTable dataTable = GetDataTableFromDataSource(); 334 if (dataTable == null) 335 { 336 return null; 337 } 338 return dataTable.DefaultView; 339 } 340 } 341 342 /// <summary> 343 /// 获取DataView可见列总宽度 344 /// </summary> 345 public int DataGridViewWidth 346 { 347 get 348 { 349 int DataWidth = 0; 350 foreach (DataGridViewColumn col in Dgv.Columns) 351 { 352 if (col.Visible) 353 { 354 DataWidth += col.Width; 355 } 356 } 357 return DataWidth; 358 } 359 } 360 361 362 363 [Description("弹窗宽度"), Browsable(true), Category("Popup")] 364 public int MenuWidth { get; set; } 365 366 [Description("弹窗高度"), Browsable(true), Category("Popup")] 367 public int MenuHeight { get { 368 return DropDown.Height; 369 } set { 370 DropDown.Height= value; 371 } } 372 #endregion 373 374 #region 方法 375 376 377 378 private DataTable GetDataTableFromDataSource() 379 { 380 object dataSource = Dgv.DataSource; 381 return GetDataTableFromDataSource(dataSource); 382 } 383 /// <summary> 384 /// 从DataGridView 获取数据表 385 /// </summary> 386 /// <returns></returns> 387 private DataTable GetDataTableFromDataSource(object dataSource) 388 { 389 if (dataSource is DataTable) 390 { 391 return (DataTable)dataSource; 392 } 393 else if (dataSource is DataView) 394 { 395 return ((DataView)dataSource).Table; 396 } 397 else if (dataSource is BindingSource) 398 { 399 object bind = ((BindingSource)dataSource).DataSource; 400 if (bind is DataTable) 401 { 402 return (DataTable)bind; 403 } 404 else 405 { 406 return ((DataView)bind).Table; 407 } 408 } 409 else 410 { 411 return null; 412 } 413 } 414 415 #region 重写方法 416 private string GetRowFilterString(string sText) 417 { 418 if (string.IsNullOrEmpty(sText) || DataView==null) return ""; 419 string sFilter = ""; 420 if (CallBackColumns == String.Empty || CallBackColumns == null) 421 { 422 CallBackColumns = DataView.Table.Columns[0].ColumnName; 423 } 424 if (KeyWordsFilter == String.Empty) 425 { 426 KeyWordsFilter = CallBackColumns; 427 } 428 string[] sColumns = KeyWordsFilter.Split(','); 429 foreach (string sColumn in sColumns) 430 { 431 sFilter += sColumn + " like " + "'%" + sText + "%'" + " or "; 432 } 433 sFilter = sFilter.Trim().TrimEnd("or".ToCharArray()); 434 return sFilter; 435 } 436 private void InitializeComponent() 437 { 438 this.SuspendLayout(); 439 this.ResumeLayout(false); 440 441 } 442 protected override void Dispose(bool disposing) 443 { 444 if (disposing) 445 { 446 if (Menu != null) 447 { 448 Menu.Dispose(); 449 Menu = null; 450 } 451 } 452 base.Dispose(disposing); 453 } 454 455 #endregion 456 457 #endregion 458 459 460 } 461 /// <summary> 462 /// 重写ToolStripDropDown 463 /// 使用双缓存减少闪烁 464 /// </summary> 465 public class MyToolStripDropDown : ToolStripDropDown 466 { 467 protected override CreateParams CreateParams 468 { 469 get 470 { 471 CreateParams cp = base.CreateParams; 472 cp.ExStyle |= 0x02000000;//双缓存 473 return cp; 474 } 475 } 476 } 477 478 }
4. 搜索控件,可以直接拖的通用控件。
1 using TianBaoZhiYuan; 2 using System; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Windows.Forms; 6 7 namespace TianBaoZhiYuan.UI 8 { 9 public partial class UCSearch : UserControl 10 { 11 public delegate void KeyUpHandle(object sender, KeyEventArgs e); 12 public event KeyUpHandle TextKeyUp; 13 public event EventHandler TextBoxChanged; 14 public event EventHandler SearchClick; 15 16 public Action CloseOtherDropDown = null;//关闭其他弹窗 17 18 public void OpenMenu() 19 { 20 DropDownMenu.OpenMenu(); 21 22 } 23 public void CloseMenu() 24 { 25 DropDownMenu.CloseMenu(); 26 } 27 public UCDropDown DropDown 28 { 29 get 30 { 31 return DropDownMenu.DropDown; 32 } 33 } 34 public DataGridView Dgv 35 { 36 get 37 { 38 return DropDownMenu.Dgv; 39 } 40 } 41 public UCSearch() 42 { 43 InitializeComponent(); 44 LbTips.BringToFront(); 45 } 46 47 private string _datatype = null; 48 [Browsable(true)] 49 public string DataType 50 { 51 get 52 { 53 return _datatype; 54 } 55 set 56 { 57 _datatype = value; 58 } 59 } 60 [Browsable(true)] 61 public char PasswordChar 62 { 63 get 64 { 65 return DropDownMenu.PasswordChar; 66 } 67 set 68 { 69 DropDownMenu.PasswordChar = value; 70 } 71 } 72 [DefaultValue(typeof(Color), "228,228,228"), Browsable(true)] 73 public Color BorderColor 74 { 75 get 76 { 77 return PanelBorder.BackColor; 78 } 79 set 80 { 81 PanelBorder.BackColor = value; 82 } 83 } 84 [Browsable(true)] 85 public bool ReadOnly 86 { 87 get 88 { 89 return DropDownMenu.ReadOnly; 90 } 91 set 92 { 93 DropDownMenu.ReadOnly = value; 94 DropDownMenu.BackColor = LbContainer.BackColor; 95 } 96 } 97 [Browsable(true)] 98 public bool ShowSearchButton 99 { 100 get 101 { 102 return LbSearch.Width > 0; 103 } 104 set 105 { 106 LbSearch.Width = value ? 30 : 0; 107 } 108 } 109 [Browsable(true)] 110 public void Select(int s, int e) 111 { 112 DropDownMenu.Select(s, e); 113 } 114 115 [Browsable(true)] 116 public string Value 117 { 118 get 119 { 120 return DropDownMenu.Text; 121 } 122 set 123 { 124 DropDownMenu.Text = value; 125 } 126 } 127 128 [Browsable(true)] 129 public string Prompt 130 { 131 get 132 { 133 return LbTips.Text; 134 } 135 set 136 { 137 LbTips.Text = value.ToString(); 138 } 139 } 140 [Browsable(true)] 141 public string Title 142 { 143 get 144 { 145 return LbTitle.Text; 146 } 147 set 148 { 149 if (string.IsNullOrEmpty(value)) 150 { 151 LbTitle.Width = 0; 152 } 153 else 154 { 155 Size sizeF = TextRenderer.MeasureText(value, LbTitle.Font); 156 LbTitle.Width = sizeF.Width; 157 } 158 LbTitle.Text = value.ToString(); 159 } 160 } 161 [Browsable(true)] 162 public int TitleWidth 163 { 164 get 165 { 166 return LbTitle.Width; 167 } 168 set 169 { 170 LbTitle.Width = value; 171 } 172 } 173 private void UserControl_TextChanged(object sender, EventArgs e) 174 { 175 LbTips.Visible = DropDownMenu.Text.Length < 1; 176 Tag = DropDownMenu.Text.Length < 1 ? "" : Tag; 177 TextBoxChanged?.Invoke(this, e); 178 } 179 180 private void UserControl_KeyUp(object sender, KeyEventArgs e) 181 { 182 TextKeyUp?.Invoke(this, e); 183 } 184 185 private void LblTips_Click(object sender, EventArgs e) 186 { 187 LbTips.Visible = true; 188 CloseOtherDropDown?.Invoke(); 189 DropDownMenu.Focus(); 190 DropDownMenu.OpenMenu(); 191 192 } 193 194 private void Btn_Click(object sender, EventArgs e) 195 { 196 SearchClick?.Invoke(sender, e); 197 } 198 199 private void TxtText_FontChanged(object sender, EventArgs e) 200 { 201 LbTips.Font = Font; 202 DropDownMenu.Font = Font; 203 } 204 } 205 }
5 .调用控件,加载数据的可以看源码
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Diagnostics; 6 using System.Drawing; 7 using System.IO; 8 using System.Linq; 9 using System.Reflection; 10 using System.Text; 11 using System.Threading.Tasks; 12 using System.Windows.Forms; 13 using TianBaoZhiYuan.UI; 14 15 namespace TianBaoZhiYuan 16 { 17 public partial class Main : Form 18 { 19 public Main() 20 { 21 InitializeComponent(); 22 } 23 24 25 26 private void TianBaoZhiYuan_Load(object sender, EventArgs e) 27 { 28 InitCollegeData(); 29 } 30 31 /// <summary> 32 /// 初始化大学 33 /// </summary> 34 private async void InitCollegeData() 35 { 36 this.txtDX.Dgv.AllowUserToResizeRows = false; 37 this.txtDX.DropDownMenu.Width = 650; 38 this.txtDX.DropDownMenu.MenuWidth = 650; 39 this.txtDX.DropDownMenu.CallBackColumns = "学校名称";//回调的列,这是需要的 40 this.txtDX.DropDownMenu.KeyWordsFilter = "学校名称,主管部门,所在地,层次,网址,全拼,简拼";//要搜索的列 41 var datasource = await GetData_College(); 42 this.txtDX.Dgv.DataSource = datasource; 43 44 this.txtDX.DropDownMenu.RowFilterVisible = true; 45 this.txtDX.DropDownMenu.MenuHeight -= 20; 46 47 //打开弹窗 48 this.txtDX.DropDownMenu.AfterOpenMenu = () => { 49 this.txtDX.Dgv.Columns[0].Width = 50; 50 this.txtDX.Dgv.Columns[1].Width = 130; 51 this.txtDX.Dgv.Columns[2].Width = 80; 52 this.txtDX.Dgv.Columns[3].Width = 60; 53 this.txtDX.Dgv.Columns[4].Width = 70; 54 this.txtDX.Dgv.Columns[5].Width = 70; 55 this.txtDX.Dgv.Columns[6].Width = 70; 56 this.txtDX.Dgv.Columns[7].Width = 80; 57 }; 58 //选择 59 this.txtDX.DropDown.SelectChanged += (data) => 60 { 61 this.txtDX.Value = data[0].ToString(); 62 this.txtDX.CloseMenu(); 63 }; 64 //关闭其他弹窗 65 this.txtDX.CloseOtherDropDown = () => 66 { 67 this.CloseOtherDropDown(); 68 }; 69 // 70 this.txtDX.DropDownMenu.CloseOtherEvent += () => 71 { 72 this.CloseOtherDropDown(); 73 }; 74 //搜索 75 this.txtDX.SearchClick += (e, v) => 76 { 77 this.CloseOtherDropDown(); 78 this.txtDX.OpenMenu(); 79 this.txtDX.Focus(); 80 }; 81 //拓展 82 this.txtDX.DropDown.UpdateEvent += async () => 83 { 84 MessageBox.Show("拓展功能2"); 85 }; 86 //拓展 87 this.txtDX.DropDown.AddEvent += () => 88 { 89 MessageBox.Show("拓展功能1"); 90 }; 91 } 92 93 #region 获取大学数据 94 public async Task<DataTable> GetData_College() 95 { 96 var dt = await Task.Run(() => 97 { 98 return DataHelper.大学数据();//这里是模拟数据。 实际情况,可能是要查询数据库的。 99 }); 100 return dt; 101 } 102 103 #endregion 104 105 106 107 108 109 public void CloseOtherDropDown() 110 { 111 var txt = this.Controls.Find<UCSearch>(true); 112 txt.ForEach(t => 113 { 114 t.DropDownMenu.CloseMenu(); 115 }); 116 117 } 118 119 private void TianBaoZhiYuan_Click(object sender, EventArgs e) 120 { 121 CloseOtherDropDown(); 122 } 123 } 124 }