ComboBoxTextEx.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace LYFZ.ComponentLibrary
  10. {
  11. public partial class ComboBoxTextEx : UserControl
  12. {
  13. public ComboBoxTextEx()
  14. {
  15. InitializeComponent();
  16. }
  17. #region 属性
  18. DataTable _Bindtable;
  19. /// <summary>
  20. /// 要绑定数据
  21. /// </summary>
  22. public DataTable Bindtable
  23. {
  24. get { return _Bindtable; }
  25. set
  26. {
  27. _Bindtable = value;
  28. int an = txtValue.Height;
  29. TextComboBoxExLoad();
  30. }
  31. }
  32. string _ShowName;
  33. /// <summary>
  34. /// 要显示的字段
  35. /// </summary>
  36. public string ShowName
  37. {
  38. get { return _ShowName; }
  39. set
  40. {
  41. _ShowName = value;
  42. TextComboBoxExLoad();
  43. }
  44. }
  45. string _Text;
  46. /// <summary>
  47. /// 选择后返回的值
  48. /// </summary>
  49. public override string Text
  50. {
  51. get { return _Text; }
  52. set { _Text = value; }
  53. }
  54. int _LstBoxHeiht;
  55. public int LstBoxHeiht
  56. {
  57. get { return _LstBoxHeiht; }
  58. set { _LstBoxHeiht = value; }
  59. }
  60. #endregion
  61. #region 属性重写
  62. [Category("控件扩展属性"), Description("设置控件中文本字体")]
  63. public new Font Font
  64. {
  65. get
  66. {
  67. return txtValue.Font;
  68. }
  69. set
  70. {
  71. txtValue.Font = value;
  72. lstboxData.Font = txtValue.Font;
  73. OnResize(null);
  74. Invalidate(false);
  75. }
  76. }
  77. /// <summary>
  78. /// 禁止调整大小
  79. /// </summary>
  80. /// <param name="e"></param>
  81. protected override void OnResize(EventArgs e)
  82. {
  83. if (lstboxData.Visible == true)
  84. {
  85. this.Height = txtValue.Height + lstboxData.Height;
  86. if (LstBoxHeiht > lstboxData.Font.Height)
  87. {
  88. lstboxData.Height = LstBoxHeiht;
  89. }
  90. }
  91. else
  92. {
  93. this.Height = txtValue.Height;
  94. btnOpenDrop.Width = this.Height;
  95. btnOpenDrop.Height = this.Height;
  96. btnOpenDrop.Location = new Point(this.Width - this.Height, 0);
  97. txtValue.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
  98. txtValue.Width = this.Width - btnOpenDrop.Width;
  99. this.Width = txtValue.Width + btnOpenDrop.Width;
  100. lstboxData.Height = lstboxData.Font.Height; ;
  101. lstboxData.Width = this.Width - 1;
  102. lstboxData.Location = new Point(0, txtValue.Height - 1);
  103. lstboxData.BorderStyle = BorderStyle.None;
  104. lstboxData.BackColor = Color.FromArgb(238, 238, 238);
  105. lstboxData.Visible = false;
  106. }
  107. }
  108. #endregion
  109. /// <summary>
  110. /// 窗体加载事件
  111. /// </summary>
  112. /// <param name="sender"></param>
  113. /// <param name="e"></param>
  114. private void ComboBoxTextEx_Load(object sender, EventArgs e)
  115. {
  116. }
  117. /// <summary>
  118. /// 给列表画值
  119. /// </summary>
  120. private void TextComboBoxExLoad()
  121. {
  122. txtValue.Text = "";
  123. Text = "";
  124. lstboxData.Items.Clear();
  125. lstboxData.Items.Add("");
  126. LstBoxHeiht = lstboxData.Font.Height;
  127. if (Bindtable != null && ShowName != null && ShowName != "")
  128. {
  129. if (Bindtable.Rows.Count > 0)
  130. {
  131. for (int i = 0; i < Bindtable.Rows.Count; i++)
  132. {
  133. LstBoxHeiht += lstboxData.Font.Height;
  134. lstboxData.Items.Add(Bindtable.Rows[i][ShowName].ToString());
  135. }
  136. }
  137. }
  138. }
  139. /// <summary>
  140. /// 按钮的点击
  141. /// </summary>
  142. /// <param name="sender"></param>
  143. /// <param name="e"></param>
  144. private void btnOpenDrop_Click(object sender, EventArgs e)
  145. {
  146. if (lstboxData.Visible)
  147. {
  148. lstboxData.Visible = false;
  149. this.Height = btnOpenDrop.Height;
  150. }
  151. else
  152. {
  153. TextComboBoxExLoad();
  154. this.BringToFront();
  155. lstboxData.Visible = true;
  156. OnResize(null);
  157. }
  158. }
  159. /// <summary>
  160. /// 列表选择事件
  161. /// </summary>
  162. /// <param name="sender"></param>
  163. /// <param name="e"></param>
  164. private void lstboxData_SelectedIndexChanged(object sender, EventArgs e)
  165. {
  166. Text = lstboxData.Items[lstboxData.SelectedIndex].ToString();
  167. txtValue.Text = Text;
  168. lstboxData.Visible = false;
  169. this.Size = new Size(txtValue.Width + btnOpenDrop.Width, txtValue.Height);
  170. }
  171. private void lstboxData_Leave(object sender, EventArgs e)
  172. {
  173. txtValue_Leave(null, null);
  174. }
  175. private void txtValue_Leave(object sender, EventArgs e)
  176. {
  177. if (lstboxData.Visible == true)
  178. {
  179. lstboxData.Visible = false;
  180. this.Height = btnOpenDrop.Height;
  181. }
  182. }
  183. }
  184. }