public
abstract class EmbedEditControl : Component
{
private Form _clientForm;
private OrpCustomEmbedControlEdit _editControl;
private int _clientFormWidth = -1;
protected Form ClientForm
{
get
{
return _clientForm;
}
}
[Category("Appearance")]
public int ClientFormWidth
{
get
{
return _clientFormWidth;
}
set
{
_clientFormWidth = value;
}
}
[Browsable(false)]
public OrpCustomEmbedControlEdit EditControl
{
get
{
return _editControl;
}
}
public virtual void InitializeControl(Form clientForm, OrpCustomEmbedControlEdit editControl)
{
_clientForm = clientForm;
_editControl = editControl;
}
public abstract void ParseValue(object value);
public abstract object GetInputValue();
public abstract void ClientFormClosed();
public void CloseClientForm(bool isCancel)
{
EditControl.CloseClientForm(isCancel);
}
}
|
[ToolboxItem(false)]
public class OrpCustomEmbedControlEdit : OrpCustomButtonEdit
{
private EmbedEditControl _embedEditControl = null;
private Form _clientForm = null;
private bool _skipLostFocus = false;
private int _clientFormWidth = -1;
private DateTime _closeTime = DateTime.Now;
[Category("Appearance")]
public int ClientFormWidth
{
get
{
return _clientFormWidth;
}
set
{
_clientFormWidth = value;
}
}
protected Form ClientForm
{
get
{
if (_clientForm == null)
_clientForm = CreateClientForm();
return _clientForm;
}
}
protected bool Droped
{
get
{
return (_clientForm != null);
}
}
protected EmbedEditControl EmbedEditControl
{
get
{
return _embedEditControl;
}
set
{
_embedEditControl = value;
}
}
protected virtual Form CreateClientForm()
{
return new Form();
}
protected internal virtual void CloseClientForm(bool isCancel)
{
if (_clientForm != null)
{
Form ownerForm = FindForm();
if (ownerForm != null)
{
ownerForm.MouseClick -= new MouseEventHandler(ownerForm_MouseClick);
ownerForm.Activated -= new EventHandler(ownerForm_Activated);
ownerForm.Resize -= new EventHandler(ownerForm_Resize);
ownerForm.RemoveOwnedForm(_clientForm);
}
if (EmbedEditControl != null)
{
if (!isCancel)
Text = (string)EmbedEditControl.GetInputValue();
EmbedEditControl.ClientFormClosed();
}
_clientForm.Close();
_clientForm.Dispose();
_clientForm = null;
}
}
private void ShowClientForm()
{
Point pt = PointToScreen(new Point(Left, Top));
ClientForm.Location = new Point(pt.X - Left - 2, pt.Y - Top + Height - 1);
ClientForm.Width = Width;
ClientForm.Height = Screen.PrimaryScreen.Bounds.Height - ClientForm.Top - 30;
ClientForm.FormBorderStyle = FormBorderStyle.None;
ClientForm.Font = (Font)Font.Clone();
ClientForm.BackColor = SystemColors.Window;
if (ClientForm.Height > 160)
ClientForm.Height = 160;
ClientForm.StartPosition = FormStartPosition.Manual;
ClientForm.ShowInTaskbar = false;
Form ownerForm = FindForm();
if (ownerForm != null)
{
ownerForm.AddOwnedForm(ClientForm);
ownerForm.MouseClick += new MouseEventHandler(ownerForm_MouseClick);
ownerForm.Activated += new EventHandler(ownerForm_Activated);
ownerForm.Resize += new EventHandler(ownerForm_Resize);
}
if (EmbedEditControl != null && EmbedEditControl.ClientFormWidth != -1)
ClientForm.Width = EmbedEditControl.ClientFormWidth;
else if (ClientFormWidth != -1)
ClientForm.Width = ClientFormWidth;
}
void ownerForm_Resize(object sender, EventArgs e)
{
CloseClientForm(true);
}
void ownerForm_Activated(object sender, EventArgs e)
{
if (((Form)sender).ActiveControl != this)
CloseClientForm(true);
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
if (Droped)
{
if (_skipLostFocus)
_skipLostFocus = false;
else
CloseClientForm(true);
_closeTime = DateTime.Now;
}
}
void ownerForm_MouseClick(object sender, MouseEventArgs e)
{
CloseClientForm(true);
}
protected override void EmbedButtonClick(EventArgs args)
{
if (Droped)
CloseClientForm(false);
else
{
TimeSpan ts = DateTime.Now - _closeTime;
if (ts.TotalMilliseconds > 200)
{
_skipLostFocus = true;
ShowClientForm();
if (EmbedEditControl != null)
{
EmbedEditControl.InitializeControl(ClientForm, this);
EmbedEditControl.ParseValue(Text);
}
ClientForm.Visible = true;
}
}
}
}
|
[ToolboxItem(true)]
public class OrpEmbedControlEdit : OrpCustomEmbedControlEdit
{
[Category("Behavoir")]
public EmbedEditControl EditControl
{
get
{
return EmbedEditControl;
}
set
{
EmbedEditControl = value;
}
}
}
|
using
System;
using
System.Drawing.Design;
using
System.ComponentModel;
using
System.Collections;
using
System.Collections.Generic;
using
System.Text;
using
System.Windows.Forms;
namespace
LookupComboBox
{
[TypeConverter(typeof(ListItemConverter)),
Serializable]
public sealed class ListItem
{
private string _text, _value;
public string Text
{
get
{
return _text;
}
set
{
_text = value;
}
}
public string Value
{
get
{
return _value;
}
set
{
_value = value;
}
}
public ListItem(string text, string value)
{
_text = text;
_value = value;
}
public ListItem()
{
}
}
[Serializable]
public class ListItems : List<ListItem>
{
public int FindValue(string value)
{
for (int i = 0; i < Count; i++)
{
if (this[i].Value.Equals(value))
return i;
}
return -1;
}
}
public class ListEmbedEditControl : EmbedEditControl
{
private ListItems _items;
private ListBox _innerListBox = null;
private object _dataSource;
private string _displayMember, _valueMember;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Category("Data")]
public ListItems Items
{
get
{
if (_items == null)
_items = new ListItems();
return _items;
}
}
[AttributeProvider(typeof(IListSource))]
[Category("Data")]
public object DataSource
{
get
{
return _dataSource;
}
set
{
if (((value != null) && !(value is IList)) && !(value is IListSource))
throw new ArgumentException("only implement IList or IListSource can be set.");
_dataSource = value;
}
}
[DefaultValue(""), TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[Category("Data")]
public string DisplayMember
{
get
{
return _displayMember;
}
set
{
_displayMember = value;
}
}
[DefaultValue(""), TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[Category("Data")]
public string ValueMember
{
get
{
return _valueMember;
}
set
{
_valueMember = value;
}
}
public override void InitializeControl(Form clientForm, OrpCustomEmbedControlEdit editControl)
{
base.InitializeControl(clientForm, editControl);
_innerListBox = new ListBox();
_innerListBox.Click += new EventHandler(_innerListBox_Click);
_innerListBox.KeyDown += new KeyEventHandler(_innerListBox_KeyDown);
_innerListBox.Dock = DockStyle.Fill;
if (DataSource == null)
{
foreach (ListItem item in Items)
_innerListBox.Items.Add(item);
_innerListBox.DisplayMember = "Text";
_innerListBox.ValueMember = "Value";
}
else
{
_innerListBox.DataSource = DataSource;
_innerListBox.DisplayMember = DisplayMember;
_innerListBox.ValueMember = ValueMember;
}
_innerListBox.BorderStyle = BorderStyle.Fixed3D;
clientForm.Controls.Add(_innerListBox);
}
void _innerListBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return)
CloseClientForm(false);
else if (e.KeyCode == Keys.Escape)
CloseClientForm(true);
}
void _innerListBox_Click(object sender, EventArgs e)
{
CloseClientForm(false);
}
public override void ParseValue(object value)
{
int index = Items.FindValue((string)value);
if (index != -1)
_innerListBox.SelectedIndex = index;
}
public override object GetInputValue()
{
if (_innerListBox != null && _innerListBox.SelectedItem != null)
{
if (_innerListBox.SelectedItem is ListItem)
return ((ListItem)_innerListBox.SelectedItem).Value;
else if(_innerListBox.SelectedValue != null)
return _innerListBox.SelectedValue.ToString();
}
return string.Empty;
}
public override void ClientFormClosed()
{
if (_innerListBox != null)
{
_innerListBox.Click -= new EventHandler(_innerListBox_Click);
_innerListBox.KeyDown -= new KeyEventHandler(_innerListBox_KeyDown);
}
}
}
[ToolboxItem(true)]
public class OrpComboBox : OrpCustomEmbedControlEdit
{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Category("Data")]
public ListItems Items
{
get
{
return ((ListEmbedEditControl)EmbedEditControl).Items;
}
}
[AttributeProvider(typeof(IListSource))]
[Category("Data")]
public object DataSource
{
get
{
return ((ListEmbedEditControl)EmbedEditControl).DataSource;
}
set
{
((ListEmbedEditControl)EmbedEditControl).DataSource = value;
}
}
[DefaultValue(""), TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[Category("Data")]
public string DisplayMember
{
get
{
return ((ListEmbedEditControl)EmbedEditControl).DisplayMember;
}
set
{
((ListEmbedEditControl)EmbedEditControl).DisplayMember = value;
}
}
[DefaultValue(""), TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[Category("Data")]
public string ValueMember
{
get
{
return ((ListEmbedEditControl)EmbedEditControl).ValueMember;
}
set
{
((ListEmbedEditControl)EmbedEditControl).ValueMember = value;
}
}
public OrpComboBox()
: base()
{
EmbedEditControl = new ListEmbedEditControl();
}
}
}
|
using
System;
using
System.Drawing;
using
System.Drawing.Design;
using
System.ComponentModel;
using
System.Collections;
using
System.Collections.Generic;
using
System.Text;
using
System.Windows.Forms;
namespace
LookupComboBox
{
[TypeConverter(typeof(LookupColumnItemConverter)),
Serializable]
public class LookupColumnItem
{
private string _header = string.Empty, _displayMember;
private int _width;
[NonSerialized]
private LookupColumnItems _owner;
protected internal LookupColumnItems Owner
{
get
{
return _owner;
}
set
{
_owner = value;
}
}
public string Header
{
get
{
return _header;
}
set
{
_header = value;
}
}
[TypeConverter(typeof(LookupColumnNameConverter))]
public string DisplayMember
{
get
{
return _displayMember;
}
set
{
_displayMember = value;
if (Header == string.Empty)
Header = value;
}
}
public int Width
{
get
{
return _width;
}
set
{
_width = value;
}
}
public LookupColumnItem(string header, string displayMember, int width)
{
_header = header;
_displayMember = displayMember;
_width = width;
}
public LookupColumnItem()
{
}
}
[Serializable]
public class LookupColumnItems : CollectionBase
{
private GridEmbedEditControl _owner;
public LookupColumnItem this[int index]
{
get
{
return (LookupColumnItem)base.List[index];
}
set
{
base.List[index] = value;
}
}
internal GridEmbedEditControl Owner
{
get
{
return _owner;
}
}
public void Add(LookupColumnItem value)
{
((IList)this).Add(value);
}
public void AddRange(LookupColumnItem[] values)
{
foreach (LookupColumnItem item in values)
Add(item);
}
protected override void OnInsertComplete(int index, object value)
{
base.OnInsertComplete(index, value);
((LookupColumnItem)value).Owner = this;
}
public LookupColumnItems(GridEmbedEditControl owner)
: base()
{
_owner = owner;
}
}
public class GridEmbedEditControl : EmbedEditControl
{
private object _dataSource;
private string _dataMember;
private BindingSource _bindingSource;
private LookupColumnItems _items;
private DataGridView _gridView = null;
[AttributeProvider(typeof(IListSource))]
[Category("Data")]
public object DataSource
{
get
{
return _dataSource;
}
set
{
if (((value != null) && !(value is IList)) && !(value is IListSource))
throw new ArgumentException("only implement IList or IListSource can be set.");
_dataSource = value;
}
}
[TypeConverter(typeof(DataMemberConverter))]
[Category("Data")]
public string DataMember
{
get
{
return _dataMember;
}
set
{
_dataMember = value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Category("Data")]
public LookupColumnItems Items
{
get
{
if (_items == null)
_items = new LookupColumnItems(this);
return _items;
}
}
public override void InitializeControl(Form clientForm, OrpCustomEmbedControlEdit editControl)
{
bool hasCustomColumnSize = false;
base.InitializeControl(clientForm, editControl);
_bindingSource = new BindingSource();
_bindingSource.DataSource = _dataSource;
_bindingSource.DataMember = _dataMember;
_gridView = new DataGridView();
_gridView.AutoGenerateColumns = false;
_gridView.AllowUserToAddRows = false;
_gridView.AllowUserToDeleteRows = false;
_gridView.AllowUserToOrderColumns = false;
_gridView.AllowUserToResizeColumns = false;
_gridView.AllowUserToResizeRows = false;
_gridView.BorderStyle = System.Windows.Forms.BorderStyle.None;
_gridView.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
_gridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
_gridView.GridColor = System.Drawing.SystemColors.Control;
_gridView.MultiSelect = false;
_gridView.ReadOnly = true;
_gridView.RowHeadersVisible = false;
_gridView.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing;
_gridView.RowTemplate.Height = 24;
_gridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
_gridView.TabIndex = 1;
_gridView.Dock = DockStyle.Fill;
_gridView.BorderStyle = BorderStyle.Fixed3D;
_gridView.CellClick += new DataGridViewCellEventHandler(_gridView_CellClick);
_gridView.KeyDown += new KeyEventHandler(_gridView_KeyDown);
foreach (LookupColumnItem item in Items)
{
DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
column.HeaderText = item.Header;
column.DataPropertyName = item.DisplayMember;
if (column.Width != 0)
{
hasCustomColumnSize = true;
column.Width = item.Width;
}
_gridView.Columns.Add(column);
}
if (!hasCustomColumnSize)
_gridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
_gridView.DataSource = _bindingSource;
_gridView.Font = (Font)editControl.Font.Clone();
clientForm.Controls.Add(_gridView);
clientForm.ActiveControl = _gridView;
}
void _gridView_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return)
CloseClientForm(false);
else if (e.KeyCode == Keys.Escape)
CloseClientForm(true);
}
void _gridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
CloseClientForm(false);
}
public override void ParseValue(object value)
{
if (Items.Count > 0)
{
try
{
int index = _bindingSource.Find(Items[0].DisplayMember, value);
if (index != -1)
_bindingSource.Position = index;
else
_bindingSource.Position = 0;
}
catch (Exception)
{
}
}
}
public override object GetInputValue()
{
if (Items.Count > 0)
{
object data = _bindingSource.Current;
if (data != null)
{
PropertyDescriptor pd = TypeDescriptor.GetProperties(data).Find(Items[0].DisplayMember, false);
if (pd != null)
{
object value = pd.GetValue(data);
if (value != null)
return value.ToString();
}
}
}
return string.Empty;
}
public override void ClientFormClosed()
{
if (_gridView != null)
{
_gridView.CellClick -= new DataGridViewCellEventHandler(_gridView_CellClick);
_gridView.KeyDown -= new KeyEventHandler(_gridView_KeyDown);
_gridView.DataSource = null;
if(_bindingSource != null)
_bindingSource.Dispose();
}
}
}
[ToolboxItem(true)]
public class OrpLookupEdit : OrpCustomEmbedControlEdit
{
[AttributeProvider(typeof(IListSource))]
[Category("Data")]
public object DataSource
{
get
{
return ((GridEmbedEditControl)EmbedEditControl).DataSource;
}
set
{
((GridEmbedEditControl)EmbedEditControl).DataSource = value;
}
}
[TypeConverter(typeof(DataMemberConverter))]
[Category("Data")]
public string DataMember
{
get
{
return ((GridEmbedEditControl)EmbedEditControl).DataMember;
}
set
{
((GridEmbedEditControl)EmbedEditControl).DataMember = value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Category("Data")]
public LookupColumnItems Items
{
get
{
return ((GridEmbedEditControl)EmbedEditControl).Items;
}
}
public OrpLookupEdit()
: base()
{
EmbedEditControl = new GridEmbedEditControl();
}
}
}
|
using
System;
using
System.ComponentModel;
using
System.ComponentModel.Design.Serialization;
using
System.Collections.Generic;
using
System.Reflection;
using
System.Text;
using
System.Globalization;
using
System.Windows.Forms;
namespace
LookupComboBox
{
sealed class ListItemConverter : ExpandableObjectConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
{
return true;
}
else
{
return base.CanConvertTo(context, destinationType);
}
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new Exception("destination type is null.");
}
if (destinationType == typeof(InstanceDescriptor) && value is ListItem)
{
ListItem item = (ListItem)value;
ConstructorInfo constructorInfo = typeof(ListItem).GetConstructor(new Type[] { typeof(string), typeof(string) });
if (constructorInfo != null)
{
return new InstanceDescriptor(constructorInfo, new object[] { item.Text, item.Value });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
sealed class LookupColumnItemConverter : ExpandableObjectConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
{
return true;
}
else
{
return base.CanConvertTo(context, destinationType);
}
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new Exception("destination type is null.");
}
if (destinationType == typeof(InstanceDescriptor) && value is LookupColumnItem)
{
LookupColumnItem item = (LookupColumnItem)value;
ConstructorInfo constructorInfo = typeof(LookupColumnItem).GetConstructor(new Type[] { typeof(string), typeof(string), typeof(int) });
if (constructorInfo != null)
return new InstanceDescriptor(constructorInfo, new object[] { item.Header, item.DisplayMember, item.Width });
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
sealed class LookupColumnNameConverter : StringConverter
{
public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
GridEmbedEditControl control = (GridEmbedEditControl)((LookupColumnItems)((LookupColumnItem)context.Instance).Owner).Owner;
PropertyDescriptorCollection cols = ListBindingHelper.GetListItemProperties(control.DataSource, control.DataMember, null);
List<string> list = new List<string>();
foreach (PropertyDescriptor pd in cols)
list.Add(pd.Name);
StandardValuesCollection retCols = new StandardValuesCollection(list);
return retCols;
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
}
sealed class DataMemberConverter : StringConverter
{
public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
PropertyDescriptor pd = TypeDescriptor.GetProperties(context.Instance).Find("DataSource", true);
if (pd != null)
{
List<string> list = new List<string>();
object dataSource = pd.GetValue(context.Instance);
PropertyDescriptorCollection cols = ListBindingHelper.GetListItemProperties(dataSource);
foreach (PropertyDescriptor pdItem in cols)
list.Add(pdItem.Name);
StandardValuesCollection retCols = new StandardValuesCollection(list);
return retCols;
}
return base.GetStandardValues(context);
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
}
}
|
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1145846