SelectDialog.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using BluePointLilac.Methods;
  2. using System;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. namespace ContextMenuManager.Controls
  6. {
  7. class SelectDialog : CommonDialog
  8. {
  9. public string Title { get; set; }
  10. public string Selected { get; set; }
  11. public int SelectedIndex { get; set; }
  12. public string[] Items { get; set; }
  13. public bool CanEdit { get; set; }
  14. public override void Reset() { }
  15. protected override bool RunDialog(IntPtr hwndOwner)
  16. {
  17. using(SelectForm frm = new SelectForm())
  18. {
  19. frm.Text = this.Title;
  20. frm.Items = this.Items;
  21. if(this.Selected != null) frm.Selected = this.Selected;
  22. else frm.SelectedIndex = this.SelectedIndex;
  23. frm.CanEdit = this.CanEdit;
  24. bool flag = frm.ShowDialog() == DialogResult.OK;
  25. if(flag)
  26. {
  27. this.Selected = frm.Selected;
  28. this.SelectedIndex = frm.SelectedIndex;
  29. }
  30. return flag;
  31. }
  32. }
  33. sealed class SelectForm : Form
  34. {
  35. public SelectForm()
  36. {
  37. this.AcceptButton = btnOk;
  38. this.CancelButton = btnCancel;
  39. this.Font = SystemFonts.MenuFont;
  40. this.ShowIcon = this.ShowInTaskbar = false;
  41. this.MaximizeBox = this.MinimizeBox = false;
  42. this.FormBorderStyle = FormBorderStyle.FixedSingle;
  43. this.StartPosition = FormStartPosition.CenterParent;
  44. this.InitializeComponents();
  45. }
  46. public string Selected
  47. {
  48. get => cmbItems.Text;
  49. set => cmbItems.Text = value;
  50. }
  51. public string[] Items
  52. {
  53. get
  54. {
  55. string[] value = new string[cmbItems.Items.Count];
  56. cmbItems.Items.CopyTo(value, 0);
  57. return value;
  58. }
  59. set
  60. {
  61. cmbItems.Items.Clear();
  62. cmbItems.Items.AddRange(value);
  63. }
  64. }
  65. public bool CanEdit
  66. {
  67. get => cmbItems.DropDownStyle == ComboBoxStyle.DropDown;
  68. set => cmbItems.DropDownStyle = value ? ComboBoxStyle.DropDown : ComboBoxStyle.DropDownList;
  69. }
  70. public int SelectedIndex
  71. {
  72. get => cmbItems.SelectedIndex;
  73. set => cmbItems.SelectedIndex = value;
  74. }
  75. readonly Button btnOk = new Button
  76. {
  77. DialogResult = DialogResult.OK,
  78. Text = AppString.Dialog.Ok,
  79. AutoSize = true
  80. };
  81. readonly Button btnCancel = new Button
  82. {
  83. DialogResult = DialogResult.Cancel,
  84. Text = AppString.Dialog.Cancel,
  85. AutoSize = true
  86. };
  87. readonly ComboBox cmbItems = new ComboBox
  88. {
  89. AutoCompleteMode = AutoCompleteMode.SuggestAppend,
  90. AutoCompleteSource = AutoCompleteSource.ListItems,
  91. DropDownHeight = 294.DpiZoom(),
  92. ImeMode = ImeMode.Disable
  93. };
  94. private void InitializeComponents()
  95. {
  96. this.Controls.AddRange(new Control[] { cmbItems, btnOk, btnCancel });
  97. int a = 20.DpiZoom();
  98. cmbItems.Left = a;
  99. cmbItems.Width = 85.DpiZoom();
  100. cmbItems.Top = btnOk.Top = btnCancel.Top = a;
  101. btnOk.Left = cmbItems.Right + a;
  102. btnCancel.Left = btnOk.Right + a;
  103. this.ClientSize = new Size(btnCancel.Right + a, btnCancel.Bottom + a);
  104. cmbItems.AutosizeDropDownWidth();
  105. }
  106. }
  107. }
  108. }