UiComboBox.cs 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Drawing.Design;
  6. using System.Windows.Forms;
  7. using Sunny.UI;
  8. [DefaultProperty("Items")]
  9. [DefaultEvent("SelectedIndexChanged")]
  10. [ToolboxItem(true)]
  11. [Description("组合框控件")]
  12. [LookupBindingProperties("DataSource", "DisplayMember", "ValueMember", "SelectedValue")]
  13. public class UiComboBox : UIDropControl, IToolTip, IHideDropDown
  14. {
  15. private object filterSelectedItem;
  16. private object filterSelectedValue;
  17. private bool showFilter;
  18. private CurrencyManager dataManager;
  19. private List<object> filterList = new List<object>();
  20. private bool SelectTextChange;
  21. private readonly UIComboBoxItem dropForm = new UIComboBoxItem();
  22. private readonly UIComboBoxItem filterForm = new UIComboBoxItem();
  23. private UIDropDown filterItemForm;
  24. private bool Wana_1;
  25. private BindingMemberInfo ValueMemberBindingMemberInfo;
  26. private IContainer components;
  27. [Browsable(false)]
  28. public override string[] FormTranslatorProperties => null;
  29. [DefaultValue(0)]
  30. [Category("SunnyUI")]
  31. [Description("垂直滚动条宽度,最小为原生滚动条宽度")]
  32. public int ScrollBarWidth
  33. {
  34. get
  35. {
  36. return ListBox.ScrollBarWidth;
  37. }
  38. set
  39. {
  40. ListBox.ScrollBarWidth = value;
  41. }
  42. }
  43. [DefaultValue(6)]
  44. [Category("SunnyUI")]
  45. [Description("垂直滚动条滑块宽度,最小为原生滚动条宽度")]
  46. public int ScrollBarHandleWidth
  47. {
  48. get
  49. {
  50. return ListBox.ScrollBarHandleWidth;
  51. }
  52. set
  53. {
  54. ListBox.ScrollBarHandleWidth = value;
  55. }
  56. }
  57. [DefaultValue(false)]
  58. [Description("显示清除按钮")]
  59. [Category("SunnyUI")]
  60. public bool ShowClearButton
  61. {
  62. get
  63. {
  64. return showClearButton;
  65. }
  66. set
  67. {
  68. showClearButton = value;
  69. }
  70. }
  71. [DefaultValue(false)]
  72. [Description("显示过滤")]
  73. [Category("SunnyUI")]
  74. public bool ShowFilter
  75. {
  76. get
  77. {
  78. return showFilter;
  79. }
  80. set
  81. {
  82. showFilter = value;
  83. if (value)
  84. {
  85. base.DropDownStyle = UIDropDownStyle.DropDown;
  86. }
  87. }
  88. }
  89. [DefaultValue(100)]
  90. [Description("过滤显示最大条目数")]
  91. [Category("SunnyUI")]
  92. public int FilterMaxCount { get; set; } = 100;
  93. [DefaultValue(false)]
  94. public bool Sorted
  95. {
  96. get
  97. {
  98. return ListBox.Sorted;
  99. }
  100. set
  101. {
  102. ListBox.Sorted = value;
  103. }
  104. }
  105. [DefaultValue(false)]
  106. [Description("过滤时删除字符串前面、后面的空格")]
  107. [Category("SunnyUI")]
  108. public bool TrimFilter { get; set; }
  109. [DefaultValue(false)]
  110. [Description("过滤时忽略大小写")]
  111. [Category("SunnyUI")]
  112. public bool FilterIgnoreCase { get; set; }
  113. private UIDropDown FilterItemForm
  114. {
  115. get
  116. {
  117. if (filterItemForm == null)
  118. {
  119. filterItemForm = new UIDropDown(filterForm);
  120. if (filterItemForm != null)
  121. {
  122. filterItemForm.VisibleChanged += FilterItemForm_VisibleChanged;
  123. }
  124. }
  125. return filterItemForm;
  126. }
  127. }
  128. private UIListBox ListBox => dropForm.ListBox;
  129. private UIListBox FilterListBox => filterForm.ListBox;
  130. [DefaultValue(25)]
  131. [Description("列表项高度")]
  132. [Category("SunnyUI")]
  133. public int ItemHeight
  134. {
  135. get
  136. {
  137. return ListBox.ItemHeight;
  138. }
  139. set
  140. {
  141. UIListBox filterListBox = FilterListBox;
  142. int itemHeight = (ListBox.ItemHeight = value);
  143. filterListBox.ItemHeight = itemHeight;
  144. }
  145. }
  146. [DefaultValue(8)]
  147. [Description("列表下拉最大个数")]
  148. [Category("SunnyUI")]
  149. public int MaxDropDownItems { get; set; } = 8;
  150. [DefaultValue(false)]
  151. [Description("不显示过滤可以自动调整下拉框宽度")]
  152. [Category("SunnyUI")]
  153. public bool DropDownAutoWidth { get; set; }
  154. public object DataSource
  155. {
  156. get
  157. {
  158. return ListBox.DataSource;
  159. }
  160. set
  161. {
  162. ListBox.DataSource = value;
  163. Box_DataSourceChanged(this, EventArgs.Empty);
  164. }
  165. }
  166. [DefaultValue(150)]
  167. [Description("下拉框宽度")]
  168. [Category("SunnyUI")]
  169. public int DropDownWidth { get; set; }
  170. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  171. [Localizable(true)]
  172. [Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
  173. [MergableProperty(false)]
  174. [Description("列表项")]
  175. [Category("SunnyUI")]
  176. public ListBox.ObjectCollection Items => ListBox.Items;
  177. [Browsable(false)]
  178. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  179. [Description("选中索引")]
  180. [Category("SunnyUI")]
  181. public int SelectedIndex
  182. {
  183. get
  184. {
  185. if (!ShowFilter)
  186. {
  187. return ListBox.SelectedIndex;
  188. }
  189. return -1;
  190. }
  191. set
  192. {
  193. if (!ShowFilter)
  194. {
  195. if (DataSource != null && value == -1 && SelectedIndex > 0)
  196. {
  197. Wana_1 = true;
  198. SelectedIndex = 0;
  199. Wana_1 = false;
  200. }
  201. ListBox.SelectedIndex = value;
  202. }
  203. }
  204. }
  205. [Browsable(false)]
  206. [Bindable(true)]
  207. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  208. [Description("选中项")]
  209. [Category("SunnyUI")]
  210. public object SelectedItem
  211. {
  212. get
  213. {
  214. if (!ShowFilter)
  215. {
  216. return ListBox.SelectedItem;
  217. }
  218. return filterSelectedItem;
  219. }
  220. set
  221. {
  222. if (!ShowFilter)
  223. {
  224. ListBox.SelectedItem = value;
  225. }
  226. }
  227. }
  228. [Browsable(false)]
  229. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  230. [Description("选中文字")]
  231. [Category("SunnyUI")]
  232. public string SelectedText
  233. {
  234. get
  235. {
  236. if (base.DropDownStyle == UIDropDownStyle.DropDown)
  237. {
  238. return edit.SelectedText;
  239. }
  240. return Text;
  241. }
  242. }
  243. [Description("获取或设置要为此列表框显示的属性。")]
  244. [Category("SunnyUI")]
  245. [DefaultValue("")]
  246. [Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
  247. [TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
  248. public string DisplayMember
  249. {
  250. get
  251. {
  252. return ListBox.DisplayMember;
  253. }
  254. set
  255. {
  256. ListBox.DisplayMember = value;
  257. Box_DisplayMemberChanged(this, EventArgs.Empty);
  258. }
  259. }
  260. [Description("获取或设置指示显示值的方式的格式说明符字符。")]
  261. [Category("SunnyUI")]
  262. [DefaultValue("")]
  263. [MergableProperty(false)]
  264. public string FormatString
  265. {
  266. get
  267. {
  268. return ListBox.FormatString;
  269. }
  270. set
  271. {
  272. UIListBox filterListBox = FilterListBox;
  273. string formatString = (ListBox.FormatString = value);
  274. filterListBox.FormatString = formatString;
  275. }
  276. }
  277. [Description("获取或设置指示显示值是否可以进行格式化操作。")]
  278. [Category("SunnyUI")]
  279. [DefaultValue(false)]
  280. public bool FormattingEnabled
  281. {
  282. get
  283. {
  284. return ListBox.FormattingEnabled;
  285. }
  286. set
  287. {
  288. UIListBox filterListBox = FilterListBox;
  289. bool formattingEnabled = (ListBox.FormattingEnabled = value);
  290. filterListBox.FormattingEnabled = formattingEnabled;
  291. }
  292. }
  293. [Description("获取或设置要为此列表框实际值的属性。")]
  294. [Category("SunnyUI")]
  295. [DefaultValue("")]
  296. [Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
  297. public string ValueMember
  298. {
  299. get
  300. {
  301. return ListBox.ValueMember;
  302. }
  303. set
  304. {
  305. ListBox.ValueMember = value;
  306. ValueMemberBindingMemberInfo = new BindingMemberInfo(value);
  307. }
  308. }
  309. [DefaultValue(null)]
  310. [Browsable(false)]
  311. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  312. [Bindable(true)]
  313. public object SelectedValue
  314. {
  315. get
  316. {
  317. if (!ShowFilter)
  318. {
  319. return ListBox.SelectedValue;
  320. }
  321. return filterSelectedValue;
  322. }
  323. set
  324. {
  325. if (!ShowFilter)
  326. {
  327. ListBox.SelectedValue = value;
  328. }
  329. }
  330. }
  331. [DefaultValue(typeof(Color), "White")]
  332. public Color ItemFillColor
  333. {
  334. get
  335. {
  336. return ListBox.FillColor;
  337. }
  338. set
  339. {
  340. UIListBox filterListBox = FilterListBox;
  341. Color color2 = (ListBox.FillColor = value);
  342. filterListBox.FillColor = color2;
  343. }
  344. }
  345. [DefaultValue(typeof(Color), "48, 48, 48")]
  346. public Color ItemForeColor
  347. {
  348. get
  349. {
  350. return ListBox.ForeColor;
  351. }
  352. set
  353. {
  354. UIListBox filterListBox = FilterListBox;
  355. Color color2 = (ListBox.ForeColor = value);
  356. filterListBox.ForeColor = color2;
  357. }
  358. }
  359. [DefaultValue(typeof(Color), "243, 249, 255")]
  360. public Color ItemSelectForeColor
  361. {
  362. get
  363. {
  364. return ListBox.ItemSelectForeColor;
  365. }
  366. set
  367. {
  368. UIListBox filterListBox = FilterListBox;
  369. Color itemSelectForeColor = (ListBox.ItemSelectForeColor = value);
  370. filterListBox.ItemSelectForeColor = itemSelectForeColor;
  371. }
  372. }
  373. [DefaultValue(typeof(Color), "80, 160, 255")]
  374. public Color ItemSelectBackColor
  375. {
  376. get
  377. {
  378. return ListBox.ItemSelectBackColor;
  379. }
  380. set
  381. {
  382. UIListBox filterListBox = FilterListBox;
  383. Color itemSelectBackColor = (ListBox.ItemSelectBackColor = value);
  384. filterListBox.ItemSelectBackColor = itemSelectBackColor;
  385. }
  386. }
  387. [DefaultValue(typeof(Color), "220, 236, 255")]
  388. public Color ItemHoverColor
  389. {
  390. get
  391. {
  392. return ListBox.HoverColor;
  393. }
  394. set
  395. {
  396. UIListBox filterListBox = FilterListBox;
  397. Color hoverColor = (ListBox.HoverColor = value);
  398. filterListBox.HoverColor = hoverColor;
  399. }
  400. }
  401. [DefaultValue(typeof(Color), "80, 160, 255")]
  402. public Color ItemRectColor
  403. {
  404. get
  405. {
  406. return ListBox.RectColor;
  407. }
  408. set
  409. {
  410. UIListBox filterListBox = FilterListBox;
  411. Color color2 = (ListBox.RectColor = value);
  412. filterListBox.RectColor = color2;
  413. }
  414. }
  415. public event EventHandler SelectionChangeCommitted;
  416. public new event EventHandler TextChanged;
  417. public event EventHandler SelectedIndexChanged;
  418. public event EventHandler DataSourceChanged;
  419. public event EventHandler DisplayMemberChanged;
  420. public event EventHandler ValueMemberChanged;
  421. public event EventHandler SelectedValueChanged;
  422. public UiComboBox()
  423. {
  424. InitializeComponent();
  425. ListBox.SelectedIndexChanged += Box_SelectedIndexChanged;
  426. ListBox.ValueMemberChanged += Box_ValueMemberChanged;
  427. ListBox.SelectedValueChanged += ListBox_SelectedValueChanged;
  428. ListBox.ItemsClear += ListBox_ItemsClear;
  429. ListBox.ItemsRemove += ListBox_ItemsRemove;
  430. ListBox.MouseClick += ListBox_MouseClick;
  431. filterForm.BeforeListClick += ListBox_Click;
  432. edit.TextChanged += Edit_TextChanged;
  433. edit.KeyDown += Edit_KeyDown;
  434. DropDownWidth = 150;
  435. fullControlSelect = true;
  436. base.MouseWheel += UIComboBox_MouseWheel;
  437. CreateInstance();
  438. }
  439. private void UIComboBox_MouseWheel(object sender, MouseEventArgs e)
  440. {
  441. if (!ShowFilter && base.DropDownStyle == UIDropDownStyle.DropDownList)
  442. {
  443. if (e.Delta <= -100 && SelectedIndex < Items.Count)
  444. {
  445. SelectedIndex++;
  446. }
  447. if (e.Delta >= 100 && SelectedIndex > 0)
  448. {
  449. SelectedIndex--;
  450. }
  451. }
  452. }
  453. private void ListBox_MouseClick(object sender, MouseEventArgs e)
  454. {
  455. if (((UIListBox)sender).IndexFromPoint(e.X, e.Y) != -1)
  456. {
  457. this.SelectionChangeCommitted?.Invoke(this, EventArgs.Empty);
  458. }
  459. }
  460. public override void Clear()
  461. {
  462. base.Clear();
  463. if (DataSource != null)
  464. {
  465. DataSource = null;
  466. }
  467. else
  468. {
  469. ListBox.Items.Clear();
  470. }
  471. }
  472. private void ListBox_Click(object sender, EventArgs e)
  473. {
  474. SelectTextChange = true;
  475. filterSelectedItem = filterList[(int)sender];
  476. filterSelectedValue = GetItemValue(filterSelectedItem);
  477. Text = GetItemText(filterSelectedItem);
  478. edit.SelectionStart = Text.Length;
  479. this.SelectedValueChanged?.Invoke(this, EventArgs.Empty);
  480. SelectTextChange = false;
  481. }
  482. private void ShowDropDownFilter()
  483. {
  484. if (Text.IsNullOrEmpty() && ShowFilter)
  485. {
  486. FillFilterTextEmpty();
  487. }
  488. FilterItemForm.AutoClose = false;
  489. if (!FilterItemForm.Visible)
  490. {
  491. filterForm.Style = base.StyleDropDown;
  492. if (base.StyleDropDown != UIStyle.Inherited)
  493. {
  494. filterForm.Style = base.StyleDropDown;
  495. }
  496. else
  497. {
  498. filterForm.Style = UIStyles.Style;
  499. }
  500. FilterItemForm.Show(this, new Size((DropDownWidth < base.Width) ? base.Width : DropDownWidth, CalcItemFormHeight()));
  501. edit.Focus();
  502. }
  503. }
  504. private void Edit_KeyDown(object sender, KeyEventArgs e)
  505. {
  506. if (ShowFilter)
  507. {
  508. if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up)
  509. {
  510. if (!FilterItemForm.Visible)
  511. {
  512. ShowDropDownFilter();
  513. }
  514. int count = filterForm.ListBox.Items.Count;
  515. int selectedIndex = filterForm.ListBox.SelectedIndex;
  516. if (count > 0)
  517. {
  518. if (e.KeyCode == Keys.Down && selectedIndex < count - 1)
  519. {
  520. filterForm.ListBox.SelectedIndex++;
  521. }
  522. if (e.KeyCode == Keys.Up && selectedIndex > 0)
  523. {
  524. filterForm.ListBox.SelectedIndex--;
  525. }
  526. }
  527. }
  528. else if (e.KeyCode == Keys.Escape)
  529. {
  530. FilterItemForm.Close();
  531. }
  532. else if (e.KeyCode == Keys.Return)
  533. {
  534. if (FilterItemForm.Visible)
  535. {
  536. int count2 = filterForm.ListBox.Items.Count;
  537. int selectedIndex2 = filterForm.ListBox.SelectedIndex;
  538. if (count2 > 0 && selectedIndex2 >= 0 && selectedIndex2 < count2)
  539. {
  540. SelectTextChange = true;
  541. filterSelectedItem = filterList[selectedIndex2];
  542. filterSelectedValue = GetItemValue(filterSelectedItem);
  543. Text = GetItemText(filterSelectedItem);
  544. edit.SelectionStart = Text.Length;
  545. this.SelectedValueChanged?.Invoke(this, EventArgs.Empty);
  546. SelectTextChange = false;
  547. }
  548. FilterItemForm.Close();
  549. }
  550. else
  551. {
  552. ShowDropDownFilter();
  553. }
  554. }
  555. else
  556. {
  557. base.OnKeyDown(e);
  558. }
  559. }
  560. else if (e.KeyCode == Keys.Return)
  561. {
  562. ShowDropDown();
  563. }
  564. else if (e.KeyCode == Keys.Escape)
  565. {
  566. base.ItemForm.Close();
  567. }
  568. else
  569. {
  570. base.OnKeyDown(e);
  571. }
  572. }
  573. protected override void DropDownStyleChanged()
  574. {
  575. if (base.DropDownStyle == UIDropDownStyle.DropDownList)
  576. {
  577. showFilter = false;
  578. }
  579. }
  580. private void SetDataConnection()
  581. {
  582. if (ShowFilter && base.DropDownStyle == UIDropDownStyle.DropDown && DataSource != null && DisplayMember.IsValid())
  583. {
  584. dataManager = (CurrencyManager)BindingContext[DataSource, new BindingMemberInfo(DisplayMember).BindingPath];
  585. }
  586. }
  587. private object GetItemValue(object item)
  588. {
  589. if (dataManager == null)
  590. {
  591. return item;
  592. }
  593. if (ValueMember.IsNullOrWhiteSpace())
  594. {
  595. return null;
  596. }
  597. return dataManager.GetItemProperties().Find(ValueMemberBindingMemberInfo.BindingField, ignoreCase: true)?.GetValue(item);
  598. }
  599. public Control ExToolTipControl()
  600. {
  601. return edit;
  602. }
  603. public int FindString(string s)
  604. {
  605. return ListBox.FindString(s);
  606. }
  607. public int FindString(string s, int startIndex)
  608. {
  609. return ListBox.FindString(s, startIndex);
  610. }
  611. public int FindStringExact(string s)
  612. {
  613. return ListBox.FindStringExact(s);
  614. }
  615. public int FindStringExact(string s, int startIndex)
  616. {
  617. return ListBox.FindStringExact(s, startIndex);
  618. }
  619. private void ListBox_ItemsRemove(object sender, EventArgs e)
  620. {
  621. if (ListBox.Count == 0 && base.DropDownStyle == UIDropDownStyle.DropDownList)
  622. {
  623. Text = "";
  624. edit.Text = "";
  625. }
  626. }
  627. private void ListBox_ItemsClear(object sender, EventArgs e)
  628. {
  629. if (base.DropDownStyle == UIDropDownStyle.DropDownList)
  630. {
  631. Text = "";
  632. edit.Text = "";
  633. }
  634. }
  635. private void Edit_TextChanged(object sender, EventArgs e)
  636. {
  637. this.TextChanged?.Invoke(this, e);
  638. if (!ShowFilter)
  639. {
  640. if (!SelectTextChange)
  641. {
  642. if (Text.IsValid())
  643. {
  644. ListBox.ListBox.Text = Text;
  645. return;
  646. }
  647. SelectTextChange = true;
  648. SelectedIndex = -1;
  649. edit.Text = "";
  650. SelectTextChange = false;
  651. }
  652. }
  653. else
  654. {
  655. if (base.DropDownStyle == UIDropDownStyle.DropDownList)
  656. {
  657. return;
  658. }
  659. if (edit.Focused && Text.IsValid())
  660. {
  661. ShowDropDownFilter();
  662. }
  663. if (Text.IsValid())
  664. {
  665. string text = Text;
  666. if (TrimFilter)
  667. {
  668. text = text.Trim();
  669. }
  670. filterForm.ListBox.Items.Clear();
  671. filterList.Clear();
  672. if (DataSource == null)
  673. {
  674. foreach (object item in Items)
  675. {
  676. if (FilterIgnoreCase)
  677. {
  678. if (item.ToString().ToUpper().Contains(text.ToUpper()))
  679. {
  680. filterList.Add(item.ToString());
  681. if (filterList.Count > FilterMaxCount)
  682. {
  683. break;
  684. }
  685. }
  686. }
  687. else if (item.ToString().Contains(text))
  688. {
  689. filterList.Add(item.ToString());
  690. if (filterList.Count > FilterMaxCount)
  691. {
  692. break;
  693. }
  694. }
  695. }
  696. }
  697. else if (dataManager != null)
  698. {
  699. for (int i = 0; i < Items.Count; i++)
  700. {
  701. if (FilterIgnoreCase)
  702. {
  703. if (GetItemText(dataManager.List[i]).ToUpper().Contains(text.ToUpper()))
  704. {
  705. filterList.Add(dataManager.List[i]);
  706. if (filterList.Count > FilterMaxCount)
  707. {
  708. break;
  709. }
  710. }
  711. }
  712. else if (GetItemText(dataManager.List[i]).Contains(text))
  713. {
  714. filterList.Add(dataManager.List[i]);
  715. if (filterList.Count > FilterMaxCount)
  716. {
  717. break;
  718. }
  719. }
  720. }
  721. }
  722. {
  723. foreach (object filter in filterList)
  724. {
  725. filterForm.ListBox.Items.Add(GetItemText(filter));
  726. }
  727. return;
  728. }
  729. }
  730. FillFilterTextEmpty();
  731. filterSelectedItem = null;
  732. filterSelectedValue = null;
  733. this.SelectedValueChanged?.Invoke(this, EventArgs.Empty);
  734. }
  735. }
  736. private void FillFilterTextEmpty()
  737. {
  738. filterForm.ListBox.Items.Clear();
  739. filterList.Clear();
  740. if (DataSource == null)
  741. {
  742. foreach (object item in Items)
  743. {
  744. filterList.Add(item.ToString());
  745. }
  746. }
  747. else if (dataManager != null)
  748. {
  749. for (int i = 0; i < Items.Count; i++)
  750. {
  751. filterList.Add(dataManager.List[i]);
  752. }
  753. }
  754. foreach (object filter in filterList)
  755. {
  756. filterForm.ListBox.Items.Add(GetItemText(filter));
  757. }
  758. }
  759. private void ListBox_SelectedValueChanged(object sender, EventArgs e)
  760. {
  761. if (!ShowFilter)
  762. {
  763. this.SelectedValueChanged?.Invoke(this, e);
  764. }
  765. }
  766. private void Box_ValueMemberChanged(object sender, EventArgs e)
  767. {
  768. this.ValueMemberChanged?.Invoke(this, e);
  769. }
  770. private void Box_DisplayMemberChanged(object _, EventArgs e)
  771. {
  772. this.DisplayMemberChanged?.Invoke(this, e);
  773. SetDataConnection();
  774. }
  775. private void Box_DataSourceChanged(object _, EventArgs e)
  776. {
  777. this.DataSourceChanged?.Invoke(this, e);
  778. SetDataConnection();
  779. }
  780. private void Box_SelectedIndexChanged(object sender, EventArgs e)
  781. {
  782. SelectTextChange = true;
  783. if (!ShowFilter)
  784. {
  785. if (ListBox.SelectedItem != null)
  786. {
  787. Text = ListBox.GetItemText(ListBox.SelectedItem);
  788. }
  789. else
  790. {
  791. Text = "";
  792. }
  793. }
  794. SelectTextChange = false;
  795. if (!Wana_1)
  796. {
  797. this.SelectedIndexChanged?.Invoke(this, e);
  798. }
  799. }
  800. protected override void ItemForm_ValueChanged(object sender, object value)
  801. {
  802. Invalidate();
  803. }
  804. private void FilterItemForm_VisibleChanged(object sender, EventArgs e)
  805. {
  806. dropSymbol = base.SymbolNormal;
  807. if (filterItemForm.Visible)
  808. {
  809. dropSymbol = base.SymbolDropDown;
  810. }
  811. Invalidate();
  812. }
  813. protected override void CreateInstance()
  814. {
  815. base.ItemForm = new UIDropDown(dropForm);
  816. }
  817. protected override int CalcItemFormHeight()
  818. {
  819. int num = base.ItemForm.Height - base.ItemForm.ClientRectangle.Height;
  820. return 4 + Math.Min(ListBox.Items.Count, MaxDropDownItems) * ItemHeight + num;
  821. }
  822. private void UIComboBox_FontChanged(object sender, EventArgs e)
  823. {
  824. if (base.ItemForm != null)
  825. {
  826. ListBox.Font = Font;
  827. }
  828. if (filterForm != null)
  829. {
  830. filterForm.ListBox.Font = Font;
  831. }
  832. }
  833. public void ShowDropDown()
  834. {
  835. UIComboBox_ButtonClick(this, EventArgs.Empty);
  836. }
  837. public void HideDropDown()
  838. {
  839. try
  840. {
  841. if (!ShowFilter)
  842. {
  843. if (base.ItemForm != null && base.ItemForm.Visible)
  844. {
  845. base.ItemForm.Close();
  846. }
  847. }
  848. else if (FilterItemForm != null && FilterItemForm.Visible)
  849. {
  850. FilterItemForm.Close();
  851. }
  852. }
  853. catch
  854. {
  855. }
  856. }
  857. private void UIComboBox_ButtonClick(object sender, EventArgs e)
  858. {
  859. if (NeedDrawClearButton)
  860. {
  861. NeedDrawClearButton = false;
  862. Text = "";
  863. if (!showFilter)
  864. {
  865. while (dropForm.ListBox.SelectedIndex != -1)
  866. {
  867. dropForm.ListBox.SelectedIndex = -1;
  868. }
  869. }
  870. else
  871. {
  872. while (filterForm.ListBox.SelectedIndex != -1)
  873. {
  874. filterForm.ListBox.SelectedIndex = -1;
  875. }
  876. }
  877. Invalidate();
  878. }
  879. else if (!ShowFilter)
  880. {
  881. if (Items.Count <= 0)
  882. {
  883. return;
  884. }
  885. int num = base.Width;
  886. if (DropDownAutoWidth)
  887. {
  888. if (DataSource == null)
  889. {
  890. for (int i = 0; i < Items.Count; i++)
  891. {
  892. num = Math.Max(TextRenderer.MeasureText(Items[i].ToString(), Font).Width + ScrollBarInfo.VerticalScrollBarWidth() + 6, num);
  893. }
  894. }
  895. else
  896. {
  897. for (int j = 0; j < Items.Count; j++)
  898. {
  899. num = Math.Max(TextRenderer.MeasureText(dropForm.ListBox.GetItemText(Items[j]), Font).Width + ScrollBarInfo.VerticalScrollBarWidth() + 6, num);
  900. }
  901. }
  902. }
  903. else
  904. {
  905. num = Math.Max(DropDownWidth, num);
  906. }
  907. if (base.StyleDropDown != UIStyle.Inherited)
  908. {
  909. dropForm.Style = base.StyleDropDown;
  910. }
  911. base.ItemForm.Show(this, new Size(num, CalcItemFormHeight()));
  912. }
  913. else
  914. {
  915. if (base.StyleDropDown != UIStyle.Inherited)
  916. {
  917. filterForm.Style = base.StyleDropDown;
  918. }
  919. if (FilterItemForm.Visible)
  920. {
  921. FilterItemForm.Close();
  922. }
  923. else
  924. {
  925. ShowDropDownFilter();
  926. }
  927. }
  928. }
  929. public override void SetStyleColor(UIBaseStyle uiColor)
  930. {
  931. base.SetStyleColor(uiColor);
  932. ListBox.SetStyleColor(uiColor.DropDownStyle);
  933. FilterListBox.SetStyleColor(uiColor.DropDownStyle);
  934. }
  935. public override void ResetText()
  936. {
  937. Clear();
  938. }
  939. public string GetItemText(object item)
  940. {
  941. return ListBox.GetItemText(item);
  942. }
  943. private void UIComboBox_KeyDown(object sender, KeyEventArgs e)
  944. {
  945. if (e.KeyCode == Keys.Return && !ShowFilter)
  946. {
  947. ShowDropDown();
  948. }
  949. }
  950. private void edit_Leave(object sender, EventArgs e)
  951. {
  952. HideDropDown();
  953. }
  954. protected override void Dispose(bool disposing)
  955. {
  956. if (disposing && components != null)
  957. {
  958. components.Dispose();
  959. }
  960. dropForm?.Dispose();
  961. filterForm?.Dispose();
  962. filterItemForm?.Dispose();
  963. base.Dispose(disposing);
  964. }
  965. private void InitializeComponent()
  966. {
  967. base.SuspendLayout();
  968. base.edit.Leave += new System.EventHandler(edit_Leave);
  969. base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  970. base.Name = "UIComboBox";
  971. base.KeyDown += new System.Windows.Forms.KeyEventHandler(UIComboBox_KeyDown);
  972. base.ButtonClick += new System.EventHandler(UIComboBox_ButtonClick);
  973. base.FontChanged += new System.EventHandler(UIComboBox_FontChanged);
  974. base.ResumeLayout(false);
  975. base.PerformLayout();
  976. }
  977. }