最近在做东西的时候,需要做一些具有特殊功能的ComboBox,于是就写了一些控件,本篇主要介绍支持多列数据显示的ComboBox,为需要的朋友提供一些参考,控件下载后,都可以直接使用。
开发环境:Visual Studio.net 2003
控 件 名:MultiColumnComboBox
MultiColumnComboBox.cs
1using System;
2using System.Data;
3using System.Drawing;
4using System.Windows.Forms;
5
6namespace EBright.Controls.Form.MultiColumnComboBox
7{
8 /**//// <summary>
9 /// 支持多列数据显示的ComboBox
10 /// </summary>
11 public class MultiColumnComboBox:ComboBox
12 {
13 variables#region variables
14 private System.ComponentModel.Container components = null;
15 //确定哪些列显示出来
16 private int[] _displayColumns = null;
17 //计算显示列的显示宽度
18 private int[] _displayWidths = null;
19 //数据表
20 private DataTable _dtSource = null;
21 //显示的数据行
22 private DataRow[] _drDisplay = null;
23 //列与列之间的间隙
24 private int _columnSpacing = 4;
25 //是否是第一次初始化items
26 private bool _needInitItem = true;
27 #endregion
28
29 initialize#region initialize
30 public MultiColumnComboBox()
31 {
32 if(components == null)
33 components = null;
34 this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable ;
35 }
36
37 Component Designer generated code#region Component Designer generated code
38 /**//// <summary>
39 /// Required method for Designer support - do not modify
40 /// the contents of this method with the code editor.
41 /// </summary>
42 private void InitializeComponent()
43 {
44 }
45
46 #endregion
47 #endregion
48
49 Inherited events#region Inherited events
50 protected override void OnDrawItem(DrawItemEventArgs e)
51 {
52 try
53 {
54 int iIndex = e.Index;
55 if(_drDisplay == null)
56 return;
57 if(iIndex >= _drDisplay.Length)
58 return;
59 if(iIndex > -1)
60 {
61 int iXPos = 0;
62 int iYPos = 0;
63
64 if(_drDisplay.Length == 0)
65 return;
66 e.DrawBackground();
67 for(int index = 0; index < _dtSource.Columns.Count; index++)
68 {
69 if(!this.ColumnIsDisplay(index,_displayColumns))
70 continue;
71 int idwIndex = GetColumnIndexOfArray(_displayColumns,index);
72 int idw = idwIndex==-1?80:_displayWidths[idwIndex];
73 e.Graphics.DrawString(_drDisplay[iIndex][index].ToString(), Font, new SolidBrush(e.ForeColor), new RectangleF(iXPos, e.Bounds.Y, idw, ItemHeight));
74 iXPos += idw + _columnSpacing ;
75 }
76 iXPos = 0;
77 iYPos += ItemHeight;
78 e.DrawFocusRectangle();
79 base.OnDrawItem(e);
80 }
81 }
82 catch(Exception ex)
83 {
84 throw new Exception(ex.Message + "\r\nIn ColumnComboBox.OnDrawItem(DrawItemEventArgs).");
85 }
86 }
87
88 protected override void OnDropDown(EventArgs e)
89 {
90 if(_needInitItem)
91 {
92 InitItems();
93 _needInitItem = false;
94 }
95 InitDisplay();
96 base.OnDropDown (e);
97 }
98
99 protected override void OnDataSourceChanged(EventArgs e)
100 {
101 base.OnDataSourceChanged (e);
102 InitDisplay();
103 }
104 #endregion
105
106 Property#region Property
107 public int[] DisplayColumns
108 {
109 get{return _displayColumns;}
110 set{_displayColumns = value;}
111 }
112 public DataTable Data
113 {
114 get{return _dtSource ;}
115 set
116 {
117 DataSource = value;
118 _dtSource = value;
119 }
120 }
121 #endregion
122
123 private methods#region private methods
124 private bool ColumnIsDisplay(int column,int[] columns)
125 {
126 if(column< 0)
127 throw new Exception("column must be greater than 0.\r\n(private method)ColumnIsDisplay(int,int[])") ;
128 if(columns == null)
129 {
130 columns = new int[1] ;
131 columns[0] = 0;
132 }
133 else if(_dtSource != null)
134 {
135 if(columns.Length > _dtSource.Columns.Count)
136 throw new Exception("DisplayColumns' count cannot be greater than count of datasource.\r\n(private method)ColumnIsDisplay(int,int[])") ;
137 }
138 foreach(int c in columns)
139 {
140 if(c == column)
141 return true;
142 }
1using System;
2using System.Data;
3using System.Drawing;
4using System.Windows.Forms;
5
6namespace EBright.Controls.Form.MultiColumnComboBox
7{
8 /**//// <summary>
9 /// 支持多列数据显示的ComboBox
10 /// </summary>
11 public class MultiColumnComboBox:ComboBox
12 {
13 variables#region variables
14 private System.ComponentModel.Container components = null;
15 //确定哪些列显示出来
16 private int[] _displayColumns = null;
17 //计算显示列的显示宽度
18 private int[] _displayWidths = null;
19 //数据表
20 private DataTable _dtSource = null;
21 //显示的数据行
22 private DataRow[] _drDisplay = null;
23 //列与列之间的间隙
24 private int _columnSpacing = 4;
25 //是否是第一次初始化items
26 private bool _needInitItem = true;
27 #endregion
28
29 initialize#region initialize
30 public MultiColumnComboBox()
31 {
32 if(components == null)
33 components = null;
34 this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable ;
35 }
36
37 Component Designer generated code#region Component Designer generated code
38 /**//// <summary>
39 /// Required method for Designer support - do not modify
40 /// the contents of this method with the code editor.
41 /// </summary>
42 private void InitializeComponent()
43 {
44 }
45
46 #endregion
47 #endregion
48
49 Inherited events#region Inherited events
50 protected override void OnDrawItem(DrawItemEventArgs e)
51 {
52 try
53 {
54 int iIndex = e.Index;
55 if(_drDisplay == null)
56 return;
57 if(iIndex >= _drDisplay.Length)
58 return;
59 if(iIndex > -1)
60 {
61 int iXPos = 0;
62 int iYPos = 0;
63
64 if(_drDisplay.Length == 0)
65 return;
66 e.DrawBackground();
67 for(int index = 0; index < _dtSource.Columns.Count; index++)
68 {
69 if(!this.ColumnIsDisplay(index,_displayColumns))
70 continue;
71 int idwIndex = GetColumnIndexOfArray(_displayColumns,index);
72 int idw = idwIndex==-1?80:_displayWidths[idwIndex];
73 e.Graphics.DrawString(_drDisplay[iIndex][index].ToString(), Font, new SolidBrush(e.ForeColor), new RectangleF(iXPos, e.Bounds.Y, idw, ItemHeight));
74 iXPos += idw + _columnSpacing ;
75 }
76 iXPos = 0;
77 iYPos += ItemHeight;
78 e.DrawFocusRectangle();
79 base.OnDrawItem(e);
80 }
81 }
82 catch(Exception ex)
83 {
84 throw new Exception(ex.Message + "\r\nIn ColumnComboBox.OnDrawItem(DrawItemEventArgs).");
85 }
86 }
87
88 protected override void OnDropDown(EventArgs e)
89 {
90 if(_needInitItem)
91 {
92 InitItems();
93 _needInitItem = false;
94 }
95 InitDisplay();
96 base.OnDropDown (e);
97 }
98
99 protected override void OnDataSourceChanged(EventArgs e)
100 {
101 base.OnDataSourceChanged (e);
102 InitDisplay();
103 }
104 #endregion
105
106 Property#region Property
107 public int[] DisplayColumns
108 {
109 get{return _displayColumns;}
110 set{_displayColumns = value;}
111 }
112 public DataTable Data
113 {
114 get{return _dtSource ;}
115 set
116 {
117 DataSource = value;
118 _dtSource = value;
119 }
120 }
121 #endregion
122
123 private methods#region private methods
124 private bool ColumnIsDisplay(int column,int[] columns)
125 {
126 if(column< 0)
127 throw new Exception("column must be greater than 0.\r\n(private method)ColumnIsDisplay(int,int[])") ;
128 if(columns == null)
129 {
130 columns = new int[1] ;
131 columns[0] = 0;
132 }
133 else if(_dtSource != null)
134 {
135 if(columns.Length > _dtSource.Columns.Count)
136 throw new Exception("DisplayColumns' count cannot be greater than count of datasource.\r\n(private method)ColumnIsDisplay(int,int[])") ;
137 }
138 foreach(int c in columns)
139 {
140 if(c == column)
141 return true;
142 }