// Trying to use as a regular combobox
multiColumnComboBox4.Items.Add("Cat");
multiColumnComboBox4.Items.Add("Tiger");
multiColumnComboBox4.Items.Add("Lion");
multiColumnComboBox4.Items.Add("Cheetah");
multiColumnComboBox4.SelectedIndex = 0;
// <ADD>
private List<String> showOnlyColumns = new List<String>();
[Description("Set the columns that will be displayed in the dropdown list. If not set, all column will be shown.")]
[Category("Data")]
[DefaultValue(null)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(System.Drawing.Design.UITypeEditor))]
public List<String> ShowOnlyColumns
{
get
{
return showOnlyColumns;
}
set
{
showOnlyColumns = value;
}
}
// </ADD>
// <MODIFY>
privatefloat CalculateTotalWidth()
{
float totWidth = 0;
for (int colIndex = 0; colIndex < columnWidths.Length; colIndex++)
{
// Conditional width calculation
if (showOnlyColumns.Count == 0 ||
showOnlyColumns.Contains(columnNames[colIndex]))
{
totWidth += (columnWidths[colIndex] + columnPadding);
}
}
return totWidth + SystemInformation.VerticalScrollBarWidth;
}
protectedoverridevoid OnDrawItem(DrawItemEventArgs e)
{
[...]
// Conditional display
if (colIndex != valueMemberColumnIndex &&
(showOnlyColumns.Count == 0 ||
showOnlyColumns.Contains(columnNames[colIndex])))
{
string item = Convert.ToString(FilterItemOnProperty(Items[e.Index], columnNames[colIndex]));
boundsRect.X = lastRight;
boundsRect.Width = (int)columnWidths[colIndex] + columnPadding;
lastRight = boundsRect.Right;
//if (colIndex == valueMemberColumnIndex)
if (colIndex == displayMemberColumnIndex)
{
using (Font boldFont = new Font(Font, FontStyle.Bold))
{
e.Graphics.DrawString(item, boldFont, brush, boundsRect);
}
}
else
{
e.Graphics.DrawString(item, Font, brush, boundsRect);
}
if (colIndex < columnNames.Length - 1)
{
e.Graphics.DrawLine(linePen, boundsRect.Right, boundsRect.Top, boundsRect.Right, boundsRect.Bottom);
}
}
[...]
e.DrawFocusRectangle();
}
// </MODIFY>
// <ADD>
protectedoverridevoid OnCreateControl()
{
this.SelectedItem = null;
base.OnCreateControl();
}
// </ADD>
下面是如何在代码中使用
multiColumnComboBox1.DataSource = _entityContext.CUSTOMER;
multiColumnComboBox1.DisplayMember = "TI_NAME";
// In code
multiColumnComboBox1.ShowOnlyColumns.Add("TI_NAME");
multiColumnComboBox1.ShowOnlyColumns.Add("TI_FORNAME");
// Or use Visual Studio String Collection Editor
评论会员:aaroncampf 时间:2011/12/14
你能否发表的所有代码 我不知道如何添加升级{S4}的
谢谢
评论会员:sgissinger 时间:2011/12/14
[ToolboxItem(true)]
[ToolboxBitmap(typeof(ComboBox))]
publicclass MultiColumnComboBox : ComboBox
{
private List<String> _showOnlyColumns = new List<String>();
public MultiColumnComboBox()
{
DrawMode = DrawMode.OwnerDrawVariable;
}
publicnew DrawMode DrawMode
{
get { returnbase.DrawMode; }
set
{
if (value != DrawMode.OwnerDrawVariable)
{
thrownew NotSupportedException("Needs to be DrawMode.OwnerDrawVariable");
}
base.DrawMode = value;
}
}
publicnew ComboBoxStyle DropDownStyle
{
get { returnbase.DropDownStyle; }
set
{
if (value == ComboBoxStyle.Simple)
{
thrownew NotSupportedException("ComboBoxStyle.Simple not supported");
}
base.DropDownStyle = value;
}
}
[Description("Set the columns that will be displayed in dropdown list. If not set, every columns will be shown.")]
[Category("Data")]
[DefaultValue(null)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(System.Drawing.Design.UITypeEditor))]
public List<String> ShowOnlyColumns
{
get { return _showOnlyColumns; }
set { _showOnlyColumns = value; }
}
protectedoverridevoid OnDataSourceChanged(EventArgs e)
{
base.OnDataSourceChanged(e);
InitializeColumns();
}
protectedoverridevoid OnValueMemberChanged(EventArgs e)
{
base.OnValueMemberChanged(e);
InitializeValueMemberColumn();
}
protectedoverridevoid OnDisplayMemberChanged(EventArgs e)
{
base.OnDisplayMemberChanged(e);
InitializeDisplayMemberColumn();
}
protectedoverridevoid OnDropDown(EventArgs e)
{
base.OnDropDown(e);
this.DropDownWidth = (int)CalculateTotalWidth();
}
constint columnPadding = 5;
privatefloat[] columnWidths = newfloat[0];
privateString[] columnNames = newString[0];
privateint valueMemberColumnIndex = 0;
privateint displayMemberColumnIndex = 0;
privatevoid InitializeColumns()
{
PropertyDescriptorCollection propertyDescriptorCollection = DataManager.GetItemProperties();
columnWidths = newfloat[propertyDescriptorCollection.Count];
columnNames = newString[propertyDescriptorCollection.Count];
for (int colIndex = 0; colIndex < propertyDescriptorCollection.Count; colIndex++)
{
String name = propertyDescriptorCollection[colIndex].Name;
columnNames[colIndex] = name;
}
}
privatevoid InitializeValueMemberColumn()
{
int colIndex = 0;
foreach (String columnName in columnNames)
{
if (String.Compare(columnName, ValueMember, true, CultureInfo.CurrentUICulture) == 0)
{
valueMemberColumnIndex = colIndex;
columnWidths[colIndex] = 0;
break;
}
colIndex++;
}
}
privatevoid InitializeDisplayMemberColumn()
{
int colIndex = 0;
foreach (String columnName in columnNames)
{
if (String.Compare(columnName, DisplayMember, true, CultureInfo.CurrentUICulture) == 0)
{
displayMemberColumnIndex = colIndex;
break;
}
colIndex++;
}
}
privatefloat CalculateTotalWidth()
{
float totWidth = 0;
for (int colIndex = 0; colIndex < columnWidths.Length; colIndex++)
{
if (_showOnlyColumns.Count == 0 ||
_showOnlyColumns.Contains(columnNames[colIndex]))
{
totWidth += (columnWidths[colIndex] + columnPadding);
}
}
return totWidth + SystemInformation.VerticalScrollBarWidth;
}
protectedoverridevoid OnMeasureItem(MeasureItemEventArgs e)
{
base.OnMeasureItem(e);
if (DesignMode)
return;
for (int colIndex = 0; colIndex < columnNames.Length; colIndex++)
{
string item = Convert.ToString(FilterItemOnProperty(Items[e.Index], columnNames[colIndex]));
SizeF sizeF = e.Graphics.MeasureString(item, Font);
columnWidths[colIndex] = Math.Max(columnWidths[colIndex], sizeF.Width);
}
float totWidth = CalculateTotalWidth();
e.ItemWidth = (int)totWidth;
}
protectedoverridevoid OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
if (DesignMode)
return;
e.DrawBackground();
Rectangle boundsRect = e.Bounds;
int lastRight = 0;
using (Pen linePen = new Pen(SystemColors.GrayText))
{
using (SolidBrush brush = new SolidBrush(ForeColor))
{
if (columnNames.Length == 0)
{
e.Graphics.DrawString(Convert.ToString(Items[e.Index]), Font, brush, boundsRect);
}
else
{
for (int colIndex = 0; colIndex < columnNames.Length; colIndex++)
{
if (colIndex != valueMemberColumnIndex &&
(_showOnlyColumns.Count == 0 ||
_showOnlyColumns.Contains(columnNames[colIndex])))
{
string item = Convert.ToString(FilterItemOnProperty(Items[e.Index], columnNames[colIndex]));
boundsRect.X = lastRight;
boundsRect.Width = (int)columnWidths[colIndex] + columnPadding;
lastRight = boundsRect.Right;
if (colIndex == displayMemberColumnIndex)
{
using (Font boldFont = new Font(Font, FontStyle.Bold))
{
e.Graphics.DrawString(item, boldFont, brush, boundsRect);
}
}
else
{
e.Graphics.DrawString(item, Font, brush, boundsRect);
}
if (colIndex < columnNames.Length - 1)
{
e.Graphics.DrawLine(linePen, boundsRect.Right, boundsRect.Top, boundsRect.Right, boundsRect.Bottom);
}
}
}
}
}
}
e.DrawFocusRectangle();
}
protectedoverridevoid OnCreateControl()
{
this.SelectedItem = null;
base.OnCreateControl();
}
}