by elayaraja007 » Sun May 13, 2012 5:17 pm
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
namespace Prince.Controls
{
public class MultiColumnKryptonComboBox : ComponentFactory.Krypton.Toolkit.KryptonComboBox
{
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
this.ComboBox.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
this.ComboBox.MeasureItem += new MeasureItemEventHandler(ComboBox_MeasureItem);
this.ComboBox.DrawItem += new DrawItemEventHandler(ComboBox_DrawItem);
}
private string FilterItemOnProperty(object item, string field)
{
if (item != null && field.Length > 0)
{
try
{
PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(item).Find(field, true);
if (propertyDescriptor != null)
{
item = propertyDescriptor.GetValue(item);
if (item != null) return item.ToString();
}
}
catch (Exception exp)
{
}
}
return string.Empty;
}
void ComboBox_MeasureItem(object sender, MeasureItemEventArgs 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;
}
public void RefreshDatasource()
{
if (this.UpdateDataSource != null)
{
object obj = this.SelectedValue;
this.UpdateDataSource(this);
if (obj != null && obj != DBNull.Value)
this.SelectedValue = obj;
}
}
public bool ReadOnly
{
set
{
this.Enabled = false;
}
}
public delegate void UpdateDataSourceDelegate(MultiColumnKryptonComboBox sender);
public event UpdateDataSourceDelegate UpdateDataSource;
private bool enterKeyAsTab;
private bool _inEditMode = false;
public bool EnterKeyAsTab
{
get { return enterKeyAsTab; }
set { enterKeyAsTab = value; }
}
private Form entryScreen;
public Form EntryScreen
{
get { return entryScreen; }
set { entryScreen = value; }
}
private bool limitToList;
public bool LimitToList
{
get { return limitToList; }
set { limitToList = value; }
}
public string[] Columns
{
get
{
return columnNames;
}
set
{
columnNames = value;
columnWidths = new float[value.Length];
}
}
public MultiColumnKryptonComboBox()
{
DrawMode = DrawMode.OwnerDrawVariable;
}
public new DrawMode DrawMode
{
get
{
return this.ComboBox.DrawMode;
}
set
{
if (value != DrawMode.OwnerDrawVariable)
{
throw new NotSupportedException("Needs to be DrawMode.OwnerDrawVariable");
}
this.ComboBox.DrawMode = value;
}
}
public new ComboBoxStyle DropDownStyle
{
get
{
return base.DropDownStyle;
}
set
{
if (value == ComboBoxStyle.Simple)
{
throw new NotSupportedException("ComboBoxStyle.Simple not supported");
}
base.DropDownStyle = value;
}
}
protected override void OnDataSourceChanged(EventArgs e)
{
base.OnDataSourceChanged(e);
InitializeColumns();
}
protected override void OnValueMemberChanged(EventArgs e)
{
base.OnValueMemberChanged(e);
InitializeValueMemberColumn();
}
protected override void OnDropDown(EventArgs e)
{
base.OnDropDown(e);
this.DropDownWidth = (int)CalculateTotalWidth();
}
public event System.ComponentModel.CancelEventHandler NotInList;
const int columnPadding = 5;
private float[] columnWidths = new float[0];
private String[] columnNames = new String[0];
private int valueMemberColumnIndex = 0;
private void InitializeColumns()
{
columnWidths = new float[Columns.Length];
columnNames = new String[Columns.Length];
}
private void InitializeValueMemberColumn()
{
int colIndex = 0;
foreach (String columnName in columnNames)
{
if (String.Compare(columnName, ValueMember, true, CultureInfo.CurrentUICulture) == 0)
{
valueMemberColumnIndex = colIndex;
break;
}
colIndex++;
}
}
private float CalculateTotalWidth()
{
float totWidth = 0;
foreach (int width in columnWidths)
{
totWidth += (width + columnPadding);
}
return totWidth + SystemInformation.VerticalScrollBarWidth;
}
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == 13 && enterKeyAsTab && this.Parent is System.Windows.Forms.Form)
{
e.Handled = true;
System.Windows.Forms.Form frm = (System.Windows.Forms.Form)this.Parent;
System.Windows.Forms.Control Ctl = frm.GetNextControl(this, true);
while (Ctl.CanFocus == false || Ctl.CanSelect == false)
{
Ctl = frm.GetNextControl(Ctl, true);
}
Ctl.Focus();
}
base.OnKeyPress(e);
}
protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == System.Windows.Forms.Keys.F5)
{
RefreshDatasource();
}
else if (e.KeyCode == System.Windows.Forms.Keys.F2)
{
if (entryScreen != null)
{
entryScreen.ShowDialog();
RefreshDatasource();
}
}
else
{
_inEditMode = (e.KeyCode != System.Windows.Forms.Keys.Back && e.KeyCode != System.Windows.Forms.Keys.Delete);
base.OnKeyDown(e);
}
}
protected virtual void OnNotInList(System.ComponentModel.CancelEventArgs e)
{
if (NotInList != null)
{
NotInList(this, e);
}
}
protected override void OnTextChanged(System.EventArgs e)
{
if (_inEditMode)
{
string input = Text;
int index = FindString(input);
if (index >= 0)
{
_inEditMode = false;
SelectedIndex = index;
_inEditMode = true;
Select(input.Length, Text.Length);
}
}
base.OnTextChanged(e);
}
protected override void OnValidating(System.ComponentModel.CancelEventArgs e)
{
if (this.LimitToList)
{
int pos = this.FindStringExact(this.Text);
if (pos == -1)
{
OnNotInList(e);
}
else
{
this.SelectedIndex = pos;
}
}
base.OnValidating(e);
}
private void ValidateValue()
{
if (this.SelectedItem == null && !string.IsNullOrEmpty(this.Text))
{
string input = Text;
int index = FindString(input);
if (index >= 0)
{
_inEditMode = false;
SelectedIndex = index;
_inEditMode = true;
Select(input.Length, Text.Length);
}
}
}
void ComboBox_DrawItem(object sender, DrawItemEventArgs 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++)
{
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)
{
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();
}
}
}