ComboBoxBase.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using CustomControls;
  2. namespace System.Windows.Forms
  3. {
  4. public class ComboBoxBase : ListControl
  5. {
  6. private CustomControls.OwnerDrawComboBox comboBox;
  7. private bool suppressSelectionChanged;
  8. public ComboBoxStyle DropDownStyle
  9. {
  10. get => comboBox.IsEditable ? ComboBoxStyle.DropDown : ComboBoxStyle.DropDownList;
  11. set
  12. {
  13. comboBox.IsEditable = value == ComboBoxStyle.DropDown;
  14. if (value == ComboBoxStyle.DropDownList)
  15. comboBox.SetDropDownList();
  16. }
  17. }
  18. public override string Text
  19. {
  20. get => comboBox.ComboText;
  21. set
  22. {
  23. suppressSelectionChanged = true;
  24. if (comboBox.ComboText != value)
  25. {
  26. comboBox.ComboText = value;
  27. OnTextChanged(EventArgs.Empty);
  28. }
  29. comboBox.SelectedIndex = FindStringExact(value);
  30. suppressSelectionChanged = false;
  31. }
  32. }
  33. public int ItemHeight
  34. {
  35. get => (int)(comboBox.ItemHeight * DpiScale);
  36. set => comboBox.ItemHeight = (int)(value / DpiScale);
  37. }
  38. private DrawMode drawMode;
  39. public DrawMode DrawMode
  40. {
  41. get => drawMode;
  42. set
  43. {
  44. drawMode = value;
  45. if (value != DrawMode.Normal)
  46. comboBox.SetOwnerDraw();
  47. }
  48. }
  49. public int DropDownWidth
  50. {
  51. get => (int)(comboBox.DropDownWidth * DpiScale);
  52. set => comboBox.DropDownWidth = value / DpiScale;
  53. }
  54. public int DropDownHeight
  55. {
  56. get => (int)(comboBox.MaxDropDownHeight * DpiScale);
  57. set => comboBox.MaxDropDownHeight = value / DpiScale;
  58. }
  59. public int MaxDropDownItems { get; set; } // TODO?
  60. public event DrawItemEventHandler DrawItem;
  61. public event MeasureItemEventHandler MeasureItem;
  62. public event EventHandler DropDown;
  63. public event EventHandler SelectionChangeCommitted;
  64. protected virtual void OnDrawItem(DrawItemEventArgs e) => DrawItem?.Invoke(this, e);
  65. protected virtual void OnMeasureItem(MeasureItemEventArgs e) => MeasureItem?.Invoke(this, e);
  66. protected virtual void OnDropDown(EventArgs e) => DropDown?.Invoke(this, e);
  67. protected override void OnSelectedIndexChanged(EventArgs e)
  68. {
  69. if (suppressSelectionChanged)
  70. return;
  71. // check if selected item is string: wrong behavior in FR PG object's combobox
  72. if (SelectedIndex != -1 && Items[SelectedIndex] is string)
  73. Text = Items[SelectedIndex].ToString();
  74. // reset Text: wrong behavior in FR Relation editor
  75. if (SelectedIndex == -1 && !comboBox.IsEditable)
  76. Text = null;
  77. base.OnSelectedIndexChanged(e);
  78. // this event fires when item is selected by the user
  79. if (comboBox.IsDropDownOpen)
  80. {
  81. // suppress side effects that may happen due to the user code in SelectionChangeCommitted
  82. suppressSelectionChanged = true;
  83. SelectionChangeCommitted?.Invoke(this, e);
  84. suppressSelectionChanged = false;
  85. }
  86. }
  87. protected override void SetControlHeight(int value)
  88. {
  89. // in SWF, this is controlled by the ItemHeight
  90. }
  91. protected void SetComboBox(CustomControls.OwnerDrawComboBox comboBox)
  92. {
  93. this.comboBox = comboBox;
  94. SetControl(comboBox);
  95. comboBox.IsEditable = true;
  96. comboBox.TextChanged += (sender, e) => OnTextChanged(e);
  97. comboBox.DrawItem += (sender, e) => OnDrawItem(e);
  98. comboBox.MeasureItem += (sender, e) => OnMeasureItem(e);
  99. comboBox.DropDownOpened += (sender, e) => OnDropDown(e);
  100. }
  101. public override void Refresh()
  102. {
  103. base.Refresh();
  104. var nullItem = comboBox.Template.FindName("nullItem", comboBox) as OwnerDrawItem;
  105. nullItem?.InvalidateVisual();
  106. }
  107. }
  108. }