自定义 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 ...
随机推荐
- centos7 添加第三方源
第三方源下载地址: http://repoforge.org/use/ 选择合适自己包 我选择的是EL7的 wget 下载这个包 接着使用rpm -ivh 包名 确认是否添加成功 ls /etc/yu ...
- Module Sources
转自:https://www.terraform.io/docs/modules/sources.html 主要记录module source 的格式 The source argument in a ...
- 记一次接口504 Gateway Time-out原因及解决方法
今天在刷新公司项目页面时发现有个板块一直刷新不出数据,最后发现接口报错(504 Gateway Time-out) 通过查看代码发现有个sql语句,如下,特别慢 select `vdc1`, ...
- SVN提交强制输入日志信息
在团队开发时,因一些团队成员提交代码时没有写提交说明的习惯,为了规范版本管理,增强大家的团队意识,上网找到了强制录入提交日志的方法.方法如下: 一.编写bat文件,命名为pre-commit.bat, ...
- 客户端代码压缩成zip和服务器开启gzip
1.我说的zip是打包完的js代码,用压缩工具压缩为zip文件,这样放CDN上,下载量会进一步缩小,提高进入游戏的速度 嗯,需要在游戏页用js解压zip文件 2.最简单的就服务器开启gzip
- mysql全备和增量备份以及恢复过程(percona工具)
实验环境 系统环境,内核版本和xtrabackup工具版本 [root@linux-node1 mysql]# cat /etc/redhat-release CentOS Linux release ...
- 在公司上wifi
公司的wifi上不了咋办?自己搞! 做法: 把自己的newifi mini带到公司, 记录公司内网ip,登入路由器设置, 1. 把ip改为内网一致, 2.关闭dhcp功能 3.设置wifi 网线连接: ...
- egg-init 知识点
Create a simple type application $ egg-init --type simple [dest]
- 代码风格统一工具:EditorConfig 和 静态代码检查工具:ESLint
EditorConfig 最常见的用途是:统一文件的编码字符集以及缩进风格 使用 Eslint 做代码 lint,那么为什么还要使用 .editorconfig 呢?细细想了下,应该有两个方面吧. E ...
- 2、visualBox虚拟机扩容
1.找到VBoxManager工具 1)打开Finder,找到[应用程序],在右侧找到VirtualBox.app,然后打开右键,找到[显示包内容],点击打开 2.打开终端,来到这个目录下 cd /A ...