自定义 MultiColumnComboBox[转]
// taken from a control written by Nishant Sivakumar.
// http://www.codeproject.com/cs/combobox/DotNetMultiColumnComboBox.asp
// http://www.51aspx.com/CodeFile/FengfanSell/Market/MultiColumnComboBox.cs.html
// Bugfixes or Suggestions can be sent to dcaillouet@littlerock.org using System;
using System.Windows.Forms;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Globalization; namespace WindowsFormsApplication1
{
public class MultiColumnComboBox : ComboBox
{
private bool _AutoComplete;
private bool _AutoDropdown;
private Color _BackColorEven = Color.White;
private Color _BackColorOdd = Color.White;
private string _ColumnNameString = "";
private int _ColumnWidthDefault = ;
private string _ColumnWidthString = "";
private int _LinkedColumnIndex;
private TextBox _LinkedTextBox;
private int _TotalWidth = ;
private int _ValueMemberColumnIndex = ; private Collection<string> _ColumnNames = new Collection<string>();
private Collection<int> _ColumnWidths = new Collection<int>(); public MultiColumnComboBox()
{
DrawMode = DrawMode.OwnerDrawVariable; // If all of your boxes will be RightToLeft, uncomment
// the following line to make RTL the default.
//RightToLeft = RightToLeft.Yes; // Remove the Context Menu to disable pasting
ContextMenu = new ContextMenu();
} public event System.EventHandler OpenSearchForm; public bool AutoComplete
{
get
{
return _AutoComplete;
}
set
{
_AutoComplete = value;
}
} public bool AutoDropdown
{
get
{
return _AutoDropdown;
}
set
{
_AutoDropdown = value;
}
} public Color BackColorEven
{
get
{
return _BackColorEven;
}
set
{
_BackColorEven = value;
}
} public Color BackColorOdd
{
get
{
return _BackColorOdd;
}
set
{
_BackColorOdd = value;
}
} public Collection<string> ColumnNameCollection
{
get
{
return _ColumnNames;
}
} public string ColumnNames
{
get
{
return _ColumnNameString;
} set
{
// If the column string is blank, leave it blank.
// The default width will be used for all columns.
if (!Convert.ToBoolean(value.Trim().Length))
{
_ColumnNameString = "";
}
else if (value != null)
{
char[] delimiterChars = { ',', ';', ':' };
string[] columnNames = value.Split(delimiterChars); if (!DesignMode)
{
_ColumnNames.Clear();
} // After splitting the string into an array, iterate
// through the strings and check that they're all valid.
foreach (string s in columnNames)
{
// Does it have length?
if (Convert.ToBoolean(s.Trim().Length))
{
if (!DesignMode)
{
_ColumnNames.Add(s.Trim());
}
}
else // The value is blank
{
throw new NotSupportedException("Column names can not be blank.");
}
}
_ColumnNameString = value;
}
}
} public Collection<int> ColumnWidthCollection
{
get
{
return _ColumnWidths;
}
} public int ColumnWidthDefault
{
get
{
return _ColumnWidthDefault;
}
set
{
_ColumnWidthDefault = value;
}
} public string ColumnWidths
{
get
{
return _ColumnWidthString;
} set
{
// If the column string is blank, leave it blank.
// The default width will be used for all columns.
if (!Convert.ToBoolean(value.Trim().Length))
{
_ColumnWidthString = "";
}
else if (value != null)
{
char[] delimiterChars = { ',', ';', ':' };
string[] columnWidths = value.Split(delimiterChars);
string invalidValue = "";
int invalidIndex = -;
int idx = ;
int intValue; // After splitting the string into an array, iterate
// through the strings and check that they're all integers
// or blanks
foreach (string s in columnWidths)
{
// If it has length, test if it's an integer
if (Convert.ToBoolean(s.Trim().Length))
{
// It's not an integer. Flag the offending value.
if (!int.TryParse(s, out intValue))
{
invalidIndex = idx;
invalidValue = s;
}
else // The value was okay. Increment the item index.
{
idx++;
}
}
else // The value is a space. Use the default width.
{
idx++;
}
} // If an invalid value was found, raise an exception.
if (invalidIndex > -)
{
string errMsg; errMsg = "Invalid column width '" + invalidValue + "' located at column " + invalidIndex.ToString();
throw new ArgumentOutOfRangeException(errMsg);
}
else // The string is fine
{
_ColumnWidthString = value; // Only set the values of the collections at runtime.
// Setting them at design time doesn't accomplish
// anything and causes errors since the collections
// don't exist at design time.
if (!DesignMode)
{
_ColumnWidths.Clear();
foreach (string s in columnWidths)
{
// Initialize a column width to an integer
if (Convert.ToBoolean(s.Trim().Length))
{
_ColumnWidths.Add(Convert.ToInt32(s));
}
else // Initialize the column to the default
{
_ColumnWidths.Add(_ColumnWidthDefault);
}
} // If the column is bound to data, set the column widths
// for any columns that aren't explicitly set by the
// string value entered by the programmer
if (DataManager != null)
{
InitializeColumns();
}
}
}
}
}
} public new DrawMode DrawMode
{
get
{
return base.DrawMode;
}
set
{
if (value != DrawMode.OwnerDrawVariable)
{
throw new NotSupportedException("Needs to be DrawMode.OwnerDrawVariable");
}
base.DrawMode = value;
}
} public new ComboBoxStyle DropDownStyle
{
get
{
return base.DropDownStyle;
}
set
{
if (value != ComboBoxStyle.DropDown)
{
throw new NotSupportedException("ComboBoxStyle.DropDown is the only supported style");
}
base.DropDownStyle = value;
}
} public int LinkedColumnIndex
{
get
{
return _LinkedColumnIndex;
}
set
{
if (value < )
{
throw new ArgumentOutOfRangeException("A column index can not be negative");
}
_LinkedColumnIndex = value;
}
} public TextBox LinkedTextBox
{
get
{
return _LinkedTextBox;
}
set
{
_LinkedTextBox = value; if (_LinkedTextBox != null)
{
// Set any default properties of the Linked Textbox here
_LinkedTextBox.ReadOnly = true;
_LinkedTextBox.TabStop = false;
}
}
} public int TotalWidth
{
get
{
return _TotalWidth;
}
} protected override void OnDataSourceChanged(EventArgs e)
{
base.OnDataSourceChanged(e); InitializeColumns();
} protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e); if (DesignMode)
return; e.DrawBackground(); Rectangle boundsRect = e.Bounds;
int lastRight = ; Color brushForeColor;
if ((e.State & DrawItemState.Selected) == )
{
// Item is not selected. Use BackColorOdd & BackColorEven
Color backColor;
backColor = Convert.ToBoolean(e.Index % ) ? _BackColorOdd : _BackColorEven;
using (SolidBrush brushBackColor = new SolidBrush(backColor))
{
e.Graphics.FillRectangle(brushBackColor, e.Bounds);
}
brushForeColor = Color.Black;
}
else
{
// Item is selected. Use ForeColor = White
brushForeColor = Color.White;
} using (Pen linePen = new Pen(SystemColors.GrayText))
{
using (SolidBrush brush = new SolidBrush(brushForeColor))
{
if (!Convert.ToBoolean(_ColumnNames.Count))
{
e.Graphics.DrawString(Convert.ToString(Items[e.Index]), Font, brush, boundsRect);
}
else
{
// If the ComboBox is displaying a RightToLeft language, draw it this way.
if (RightToLeft.Equals(RightToLeft.Yes))
{
// Define a StringFormat object to make the string display RTL.
StringFormat rtl = new StringFormat();
rtl.Alignment = StringAlignment.Near;
rtl.FormatFlags = StringFormatFlags.DirectionRightToLeft; // Draw the strings in reverse order from high column index to zero column index.
for (int colIndex = _ColumnNames.Count - ; colIndex >= ; colIndex--)
{
if (Convert.ToBoolean(_ColumnWidths[colIndex]))
{
string item = Convert.ToString(FilterItemOnProperty(Items[e.Index], _ColumnNames[colIndex])); boundsRect.X = lastRight;
boundsRect.Width = (int)_ColumnWidths[colIndex];
lastRight = boundsRect.Right; // Draw the string with the RTL object.
e.Graphics.DrawString(item, Font, brush, boundsRect, rtl); if (colIndex > )
{
e.Graphics.DrawLine(linePen, boundsRect.Right, boundsRect.Top, boundsRect.Right, boundsRect.Bottom);
}
}
}
}
// If the ComboBox is displaying a LeftToRight language, draw it this way.
else
{
// Display the strings in ascending order from zero to the highest column.
for (int colIndex = ; colIndex < _ColumnNames.Count; colIndex++)
{
if (Convert.ToBoolean(_ColumnWidths[colIndex]))
{
string item = Convert.ToString(FilterItemOnProperty(Items[e.Index], _ColumnNames[colIndex])); boundsRect.X = lastRight;
boundsRect.Width = (int)_ColumnWidths[colIndex];
lastRight = boundsRect.Right;
e.Graphics.DrawString(item, Font, brush, boundsRect); if (colIndex < _ColumnNames.Count - )
{
e.Graphics.DrawLine(linePen, boundsRect.Right, boundsRect.Top, boundsRect.Right, boundsRect.Bottom);
}
}
}
}
}
}
} e.DrawFocusRectangle();
} protected override void OnDropDown(EventArgs e)
{
base.OnDropDown(e); if (_TotalWidth > )
{
if (Items.Count > MaxDropDownItems)
{
// The vertical scrollbar is present. Add its width to the total.
// If you don't then RightToLeft languages will have a few characters obscured.
this.DropDownWidth = _TotalWidth + SystemInformation.VerticalScrollBarWidth;
}
else
{
this.DropDownWidth = _TotalWidth;
}
}
} protected override void OnKeyDown(KeyEventArgs e)
{
// Use the Delete or Escape Key to blank out the ComboBox and
// allow the user to type in a new value
if ((e.KeyCode == Keys.Delete) ||
(e.KeyCode == Keys.Escape))
{
SelectedIndex = -;
Text = "";
if (_LinkedTextBox != null)
{
_LinkedTextBox.Text = "";
}
}
else if (e.KeyCode == Keys.F3)
{
// Fire the OpenSearchForm Event
if (OpenSearchForm != null)
{
OpenSearchForm(this, System.EventArgs.Empty);
}
}
} // Some of the code for OnKeyPress was derived from some VB.NET code
// posted by Laurent Muller as a suggested improvement for another control.
// http://www.codeproject.com/vb/net/autocomplete_combobox.asp?df=100&forumid=3716&select=579095#xx579095xx
protected override void OnKeyPress(KeyPressEventArgs e)
{
int idx = -;
string toFind; DroppedDown = _AutoDropdown;
if (!Char.IsControl(e.KeyChar))
{
if (_AutoComplete)
{
toFind = Text.Substring(, SelectionStart) + e.KeyChar;
idx = FindStringExact(toFind); if (idx == -)
{
// An exact match for the whole string was not found
// Find a substring instead.
idx = FindString(toFind);
}
else
{
// An exact match was found. Close the dropdown.
DroppedDown = false;
} if (idx != -) // The substring was found.
{
SelectedIndex = idx;
SelectionStart = toFind.Length;
SelectionLength = Text.Length - SelectionStart;
}
else // The last keystroke did not create a valid substring.
{
// If the substring is not found, cancel the keypress
e.KeyChar = (char);
}
}
else // AutoComplete = false. Treat it like a DropDownList by finding the
// KeyChar that was struck starting from the current index
{
idx = FindString(e.KeyChar.ToString(), SelectedIndex); if (idx != -)
{
SelectedIndex = idx;
}
}
} // Do no allow the user to backspace over characters. Treat it like
// a left arrow instead. The user must not be allowed to change the
// value in the ComboBox.
if ((e.KeyChar == (char)(Keys.Back)) && // A Backspace Key is hit
(_AutoComplete) && // AutoComplete = true
(Convert.ToBoolean(SelectionStart))) // And the SelectionStart is positive
{
// Find a substring that is one character less the the current selection.
// This mimicks moving back one space with an arrow key. This substring should
// always exist since we don't allow invalid selections to be typed. If you're
// on the 3rd character of a valid code, then the first two characters have to
// be valid. Moving back to them and finding the 1st occurrence should never fail.
toFind = Text.Substring(, SelectionStart - );
idx = FindString(toFind); if (idx != -)
{
SelectedIndex = idx;
SelectionStart = toFind.Length;
SelectionLength = Text.Length - SelectionStart;
}
} // e.Handled is always true. We handle every keystroke programatically.
e.Handled = true;
} protected override void OnSelectedValueChanged(EventArgs e)
{
base.OnSelectedValueChanged(e); //Added after version 1.3 on 01/31/2008 if (_LinkedTextBox != null)
{
if (_LinkedColumnIndex < _ColumnNames.Count)
{
_LinkedTextBox.Text = Convert.ToString(FilterItemOnProperty(SelectedItem, _ColumnNames[_LinkedColumnIndex]));
}
}
} protected override void OnValueMemberChanged(EventArgs e)
{
base.OnValueMemberChanged(e); InitializeValueMemberColumn();
} private void InitializeColumns()
{
if (!Convert.ToBoolean(_ColumnNameString.Length))
{
PropertyDescriptorCollection propertyDescriptorCollection = DataManager.GetItemProperties(); _TotalWidth = ;
_ColumnNames.Clear(); for (int colIndex = ; colIndex < propertyDescriptorCollection.Count; colIndex++)
{
_ColumnNames.Add(propertyDescriptorCollection[colIndex].Name); // If the index is greater than the collection of explicitly
// set column widths, set any additional columns to the default
if (colIndex >= _ColumnWidths.Count)
{
_ColumnWidths.Add(_ColumnWidthDefault);
}
_TotalWidth += _ColumnWidths[colIndex];
}
}
else
{
_TotalWidth = ; for (int colIndex = ; colIndex < _ColumnNames.Count; colIndex++)
{
// If the index is greater than the collection of explicitly
// set column widths, set any additional columns to the default
if (colIndex >= _ColumnWidths.Count)
{
_ColumnWidths.Add(_ColumnWidthDefault);
}
_TotalWidth += _ColumnWidths[colIndex];
} } // Check to see if the programmer is trying to display a column
// in the linked textbox that is greater than the columns in the
// ComboBox. I handle this error by resetting it to zero.
if (_LinkedColumnIndex >= _ColumnNames.Count)
{
_LinkedColumnIndex = ; // Or replace this with an OutOfBounds Exception
}
} private void InitializeValueMemberColumn()
{
int colIndex = ;
foreach (String columnName in _ColumnNames)
{
if (String.Compare(columnName, ValueMember, true, CultureInfo.CurrentUICulture) == )
{
_ValueMemberColumnIndex = colIndex;
break;
}
colIndex++;
}
}
}
}
自定义 MultiColumnComboBox[转]的更多相关文章
- 关于Unity3D自定义编辑器的学习
被人物编辑器折腾了一个月,最终还是交了点成品上去(还要很多优化都还么做). 刚接手这项工作时觉得没概念,没想法,不知道.后来就去看<<Unity5.X从入门到精通>>中有关于 ...
- 一起学微软Power BI系列-使用技巧(5)自定义PowerBI时间日期表
1.日期函数表作用 经常使用Excel或者PowerBI,Power Pivot做报表,时间日期是一个重要的纬度,加上做一些钻取,时间日期函数表不可避免.所以今天就给大家分享一个自定义的做日期表的方法 ...
- JavaScript自定义浏览器滚动条兼容IE、 火狐和chrome
今天为大家分享一下我自己制作的浏览器滚动条,我们知道用css来自定义滚动条也是挺好的方式,css虽然能够改变chrome浏览器的滚动条样式可以自定义,css也能够改变IE浏览器滚动条的颜色.但是css ...
- ASP.NET Aries 入门开发教程8:树型列表及自定义右键菜单
前言: 前面几篇重点都在讲普通列表的相关操作. 本篇主要讲树型列表的操作. 框架在设计时,已经把树型列表和普通列表全面统一了操作,用法几乎是一致的. 下面介绍一些差距化的内容: 1:树型列表绑定: v ...
- ASP.NET Aries 入门开发教程5:自定义列表页工具栏区
前言: 抓紧时间,继续写教程,因为发现用户期待的内容,都在业务处理那一块. 不得不继续勤劳了. 这节主要介绍工具栏区的玩法. 工具栏的默认介绍: 工具栏默认包括5个按钮,根据不同的权限决定显示: 添加 ...
- UWP中实现自定义标题栏
UWP中实现自定义标题栏 0x00 起因 在UWP开发中,有时候我们希望实现自定义标题栏,例如在标题栏中加入搜索框.按钮之类的控件.搜了下资料居然在一个日文网站找到了一篇介绍这个主题的文章: http ...
- JavaScript 自定义对象
在Js中,除了Array.Date.Number等内置对象外,开发者可以通过Js代码创建自己的对象. 目录 1. 对象特性:描述对象的特性 2. 创建对象方式:对象直接量.new 构造函数.Objec ...
- 【WCF】自定义错误处理(IErrorHandler接口的用法)
当被调用的服务操作发生异常时,可以直接把异常的原始内容传回给客户端.在WCF中,服务器传回客户端的异常,通常会使用 FaultException,该异常由这么几个东东组成: 1.Action:在服务调 ...
- 自定义Inspector检视面板
Unity中的Inspector面板可以显示的属性包括以下两类:(1)C#以及Unity提供的基础类型:(2)自定义类型,并使用[System.Serializable]关键字序列化,比如: [Sys ...
随机推荐
- (19)模型层 -ORM之msql 跨表查询(正向和反向查询)
基于对象的跨表查询 基于对象的跨表查询'''正向和反向查询'''# 正向 ----> 关联字段在当前表中,从当前表向外查叫正向# 反向 ---> 关联字段不在当前表中,当当前表向外查叫反向 ...
- maven 总结整理(二)——download source code
当我们用maven下载jar包时,有时希望下载jar包的源代码,此时可以在pom.xml文件中,进行设置. <build> <finalName>WebProject&l ...
- 深入浅出理解 COOKIE MAPPING
转载自:http://www.myttnn.com/digital-marketing/cookie-mapping-introduction/ 在RTB(实时竞价广告,Real-Time-Biddi ...
- hive GenericUDF1
和UDF相比,通用GDF(GenericUDF)支持复杂类型(比如List,struct等)的输入和输出. 下面来看一个小示例. Hive中whereme表中包含若干人的行程如下: A 2 ...
- 验证远程主机SSH指纹
转自:https://marskid.net/2018/02/05/how-to-verify-ssh-public-key-fingerprint/ 使用SSH进行远程连接新的主机的时候,经常会看到 ...
- Babelfish 基本试用
测试使用docker 部署 docker-compose文件 注意网络模型选择的host,同时配置了opentracing 服务 version: "3" services: b ...
- airflow-operator 可以管理airflow 的kuberntes 自定义controller && crd
使用airflow-operator 我们可以让airflow 可以很方便的运行在k8s集群环境中,当前还在开发中 主要分为两部分:airflowbbase && airfowclus ...
- LoadRunner参数化&关联
我们用 HTTP 协议做脚本,要注意的是,不同协议的函数是不一样的,假如换 websocket 协议,关联函数就要用其他的 参数化 原理 1.什么叫参数化 把脚本内一个写死的值,去一个数组内取值,进行 ...
- ubuntu 客户机安装VMware tools时出现编译错误,无法与主机共享文件(转)
主机:win7旗舰版 vmware workstation 10.0.7 (其他10.x版本也有这个问题) 客户机:Ubuntu14.04.4-16.x 安装vmware tools时出现下列编译错误 ...
- 删除JavaScript对象中的元素
参考http://stackoverflow.com/questions/208105/how-to-remove-a-property-from-a-javascript-object 通过dojo ...