ComboBoxItemTypeConvert.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace OTSModelSharp
  9. {
  10. //重写下拉菜单中的项,使之与属性页的项关联
  11. public abstract class ComboBoxItemTypeConvert : TypeConverter
  12. {
  13. public Hashtable myhash = null;
  14. public ComboBoxItemTypeConvert()
  15. {
  16. myhash = new Hashtable();
  17. GetConvertHash();
  18. }
  19. public abstract void GetConvertHash();
  20. //是否支持选择列表的编辑
  21. public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  22. {
  23. return true;
  24. }
  25. //重写combobox的选择列表
  26. public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  27. {
  28. int[] ids = new int[myhash.Values.Count];
  29. int i = 0;
  30. foreach (DictionaryEntry myDE in myhash)
  31. {
  32. ids[i++] = (int)(myDE.Key);
  33. }
  34. return new StandardValuesCollection(ids);
  35. }
  36. //判断转换器是否可以工作
  37. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  38. {
  39. if (sourceType == typeof(string))
  40. {
  41. return true;
  42. }
  43. return base.CanConvertFrom(context, sourceType);
  44. }
  45. //重写转换器,将选项列表(即下拉菜单)中的值转换到该类型的值
  46. public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object obj)
  47. {
  48. if (obj is string)
  49. {
  50. foreach (DictionaryEntry myDE in myhash)
  51. {
  52. if (myDE.Value.Equals((obj.ToString())))
  53. return myDE.Key;
  54. }
  55. }
  56. return base.ConvertFrom(context, culture, obj);
  57. }
  58. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  59. {
  60. if (destinationType == typeof(string))
  61. {
  62. return true;
  63. }
  64. return base.CanConvertTo(context, destinationType);
  65. }
  66. //重写转换器将该类型的值转换到选择列表中
  67. public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object obj, Type destinationType)
  68. {
  69. if (destinationType == typeof(string))
  70. {
  71. foreach (DictionaryEntry myDE in myhash)
  72. {
  73. if (myDE.Key.Equals(obj))
  74. return myDE.Value.ToString();
  75. }
  76. return "";
  77. }
  78. return base.ConvertTo(context, culture, obj, destinationType);
  79. }
  80. public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
  81. {
  82. return false;
  83. }
  84. }
  85. //重写下拉菜单,在这里实现定义下拉菜单内的项
  86. public class MyComboItemConvert : ComboBoxItemTypeConvert
  87. {
  88. private Hashtable hash;
  89. public override void GetConvertHash()
  90. {
  91. try
  92. {
  93. myhash = hash;
  94. }
  95. catch
  96. {
  97. throw new NotImplementedException();
  98. }
  99. }
  100. public MyComboItemConvert(string str)
  101. {
  102. hash = new Hashtable();
  103. string[] stest = str.Split(',');
  104. for (int i = 0; i < stest.Length; i++)
  105. {
  106. hash.Add(i, stest[i]);
  107. }
  108. GetConvertHash();
  109. value = 0;
  110. }
  111. public int value { get; set; }
  112. public MyComboItemConvert(string str, int s)
  113. {
  114. hash = new Hashtable();
  115. string[] stest = str.Split(',');
  116. for (int i = 0; i < stest.Length; i++)
  117. {
  118. hash.Add(i, stest[i]);
  119. }
  120. GetConvertHash();
  121. value = s;
  122. }
  123. }
  124. }