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 : ListListItem>
{
public int FindValue(string value)
{
for (int i = 0; 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
{
发表评论
最新评论
|
评论