DropDownBool.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using CustomControls.BaseClasses;
  3. using CustomControls.HelperClasses;
  4. using System.Windows.Forms;
  5. using System.ComponentModel;
  6. namespace CustomControls.Win32Controls
  7. {
  8. public class DropDownBool:DropDownListBox
  9. {
  10. private DropDownBool.ListItem TrueItem = new DropDownBool.ListItem("True");
  11. private DropDownBool.ListItem FalseItem = new DropDownBool.ListItem("False");
  12. private bool _Value= true;
  13. public event EventHandler ValueChanged;
  14. [DefaultValue(typeof(string),"True")]
  15. public string TrueValueString
  16. {
  17. get{return TrueItem.Text;}
  18. set
  19. {
  20. if(value != TrueItem.Text)
  21. {
  22. TrueItem.Text= value;
  23. }
  24. }
  25. }
  26. [DefaultValue(typeof(string),"False")]
  27. public string FalseValueString
  28. {
  29. get{return FalseItem.Text;}
  30. set
  31. {
  32. if(value != FalseItem.Text)
  33. {
  34. FalseItem.Text= value;
  35. }
  36. }
  37. }
  38. [Browsable(false)]
  39. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  40. public override ComboBoxStyle DropDownStyle
  41. {
  42. get{return ComboBoxStyle.DropDownList;}
  43. }
  44. [DefaultValue(typeof(bool),"True")]
  45. public bool Value
  46. {
  47. get{return _Value;}
  48. set
  49. {
  50. if(value!=_Value)
  51. {
  52. _Value=value;
  53. OnValueChanged(new EventArgs());
  54. Invalidate();
  55. }
  56. }
  57. }
  58. public DropDownBool()
  59. {
  60. this.DropDownStyle=ComboBoxStyle.DropDownList;
  61. this.List.Items.Add(TrueItem);
  62. this.List.Items.Add(FalseItem);
  63. this.List.SelectedIndexChanged+=new EventHandler(List_SelectedIndexChanged);
  64. }
  65. protected virtual void OnValueChanged(System.EventArgs e)
  66. {
  67. if(Value)
  68. {
  69. List.SelectedItem=TrueItem;
  70. this.Text=TrueValueString;
  71. }
  72. else
  73. {
  74. List.SelectedItem=FalseItem;
  75. this.Text=FalseValueString;
  76. }
  77. if(ValueChanged!= null) {ValueChanged(this,e);}
  78. }
  79. private void List_SelectedIndexChanged(object sender, System.EventArgs e)
  80. {
  81. if(List.SelectedItem==TrueItem){Value=true;}
  82. else{Value=false;}
  83. }
  84. protected override void OnKeyDown(KeyEventArgs e)
  85. {
  86. base.OnKeyDown(e);
  87. if(e.KeyData==Keys.Space){Value=!Value;}
  88. }
  89. internal class ListItem:object
  90. {
  91. private string _Text=string.Empty;
  92. public string Text
  93. {
  94. get{return _Text; }
  95. set{_Text= value;}
  96. }
  97. public ListItem()
  98. {}
  99. public ListItem(string Text)
  100. {
  101. _Text= Text;
  102. }
  103. public override string ToString()
  104. {
  105. return _Text;
  106. }
  107. }
  108. }
  109. }