📄 simpleinputprovider.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace SimpleInput
{
public delegate void TextChanged(object sender,EventArgs e);
public delegate void AttachTextInput(object sender,string Text);
public class SIItem
{
//public event ValueChanged ValueChanged;
private int _ID;
public int ID
{
get{return(_ID);}
set
{
_ID=value;
//if(this.ValueChanged!=null) this.ValueChanged();
}
}
private string _Text;
public string Text
{
get{return(_Text);}
set
{
_Text=value;
//if(this.ValueChanged!=null) this.ValueChanged();
}
}
public SIItem(int ID,string Text)
{
this.ID=ID;
this.Text=Text;
}
public SIItem(){}
}
/// <summary>
/// 简码输入控件
/// </summary>
public class SimpleInputProvider : System.Windows.Forms.UserControl
{
private bool FocuFlag=false;
public event TextChanged Change;
public event AttachTextInput AttachInput;
private System.Windows.Forms.ComboBox InputList;
private System.Windows.Forms.TextBox InputText;
private System.Windows.Forms.Panel Shadow;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public SimpleInputProvider()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
{
components.Dispose();
if(this.InputList!=null) this.InputList.Dispose();
if(this._SICollection!=null) this._SICollection.Dispose();
}
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.InputList = new System.Windows.Forms.ComboBox();
this.InputText = new System.Windows.Forms.TextBox();
this.Shadow = new System.Windows.Forms.Panel();
this.Shadow.SuspendLayout();
this.SuspendLayout();
//
// InputList
//
this.InputList.Location = new System.Drawing.Point(24, 8);
this.InputList.Name = "InputList";
this.InputList.Size = new System.Drawing.Size(121, 20);
this.InputList.TabIndex = 0;
this.InputList.TabStop = false;
this.InputList.DropDown+=new EventHandler(InputList_DropDown);
//
// InputText
//
this.InputText.AutoSize = false;
this.InputText.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.InputText.Dock = System.Windows.Forms.DockStyle.Bottom;
this.InputText.Location = new System.Drawing.Point(0, 11);
this.InputText.Name = "InputText";
this.InputText.Size = new System.Drawing.Size(112, 21);
this.InputText.TabIndex = 1;
this.InputText.Text = "";
this.InputText.TextChanged += new System.EventHandler(this.InputText_TextChanged);
this.InputText.GotFocus+=new EventHandler(InputText_GotFocus);
this.InputText.LostFocus+=new EventHandler(InputText_LostFocus);
this.InputText.KeyDown+=new KeyEventHandler(InputText_KeyDown);
this.InputText.Click+=new EventHandler(InputText_Click);
//
// Shadow
//
this.Shadow.Controls.Add(this.InputText);
this.Shadow.Location = new System.Drawing.Point(24, 32);
this.Shadow.Name = "Shadow";
this.Shadow.Size = new System.Drawing.Size(112, 32);
this.Shadow.TabIndex = 2;
//
// SimpleInputProvider
//
this.Controls.Add(this.InputList);
this.Controls.Add(this.Shadow);
this.Name = "SimpleInputProvider";
this.Size = new System.Drawing.Size(160, 72);
this.Shadow.ResumeLayout(false);
this.ResumeLayout(false);
this.Load+=new EventHandler(SimpleInputProvider_Load);
}
#endregion
private int Offset3DWidth=4;
private int Offset3DHeight=4;
private bool InternalChange=true; //控件内部的TextChange,对外部加以屏蔽以免造成死循环
private ArrayList Items; //
private const char KeyChar='*'; //通配符
public BorderStyle BorderStyle
{
get{return(Shadow.BorderStyle);}
set{Shadow.BorderStyle=value;}
}
private string _SIFieldID="ID";
public string SIFieldID
{
get{return(_SIFieldID);}
set{_SIFieldID=value;}
}
private string _SIFieldText="Name";
public string SIFieldText
{
get{return(_SIFieldText);}
set{_SIFieldText=value;}
}
private string _SIFieldSimpleInput="SimpleInput";
public string SIFieldSimpleInput
{
get{return(_SIFieldSimpleInput);}
set{_SIFieldSimpleInput=value;}
}
private DataTable _SICollection;
public DataTable SICollection
{
get{return(_SICollection);}
set{_SICollection=value;}
}
private bool _IsAttached;
/// <summary>
/// 是否已选中简码项目
/// </summary>
public bool IsAttached
{
get{return(_IsAttached);}
}
private string _Text;
public override string Text
{
get{return(_Text);}
set
{
_Text=value;
InputText.Text=_Text;
}
}
#region " 外观 "
protected override void OnResize(EventArgs e)
{
AlignObjects();
base.OnResize(e);
}
protected override void OnMove(EventArgs e)
{
AlignObjects();
base.OnMove (e);
}
protected override void OnForeColorChanged(EventArgs e)
{
InputText.ForeColor=this.ForeColor;
base.OnForeColorChanged (e);
}
protected override void OnFontChanged(EventArgs e)
{
AlignObjects();
base.OnResize(e);
}
protected override void OnBackColorChanged(EventArgs e)
{
AlignObjects();
base.OnBackColorChanged(e);
}
private void AlignObjects()
{
InputList.Location=new Point(0,0);
InputList.Width=this.Width;
InputList.Font=this.Font;
InputList.BackColor=this.BackColor;
//固定为Combo的高度
this.Height=InputList.Height;
Shadow.Location=new Point(0,0);
Shadow.Width=this.Width;
Shadow.Height=this.Height;
Shadow.BackColor=this.BackColor;
Shadow.BringToFront();
InputText.Anchor=AnchorStyles.None;
InputText.Location=new Point(Offset3DWidth/2,Offset3DHeight/2);
InputText.Width=Shadow.Width-Offset3DWidth-5;
InputText.Height=Shadow.Height-Offset3DHeight;
InputText.Anchor=AnchorStyles.Left|AnchorStyles.Right|AnchorStyles.Top|AnchorStyles.Bottom;
InputText.Font=this.Font;
InputText.BackColor=this.BackColor;
InputText.BringToFront();
}
#endregion
private void SimpleInputProvider_Load(object sender, EventArgs e)
{
Items=new ArrayList();
}
private void InputText_TextChanged(object sender, System.EventArgs e)
{
this.Text=InputText.Text;
if(this.InternalChange) return;
Items.Clear();
Items=new ArrayList();
DataRow[] drs=this.SICollection.Select(this.SIFieldSimpleInput+" like '"+this.Text+KeyChar+"'");
foreach(DataRow dr in drs)
Items.Add(new SIItem((int)dr[this.SIFieldID],(string)dr[this.SIFieldText]));
if(Items.Count==0)
{
foreach(DataColumn col in this.SICollection.Columns)
{
if(col.DataType==typeof(string))
{
drs=this.SICollection.Select(col.ColumnName+"='"+this.Text+"'");
if(drs.Length==1)
{
Items.Add(new SIItem((int)drs[0][this.SIFieldID],(string)drs[0][this.SIFieldText]));
}
}
}
}
ShowDropDown();
Change(this,new EventArgs());
}
private void InputText_GotFocus(object sender, EventArgs e)
{
ShowDropDown();
//this.InputList.DroppedDown=this.FocuFlag;
this.FocuFlag=false;
}
private void InputText_LostFocus(object sender, EventArgs e)
{
this.FocuFlag=InputList.DroppedDown;
this.InputList.DroppedDown=false;
ClipCursor(false);
}
private void InputText_KeyDown(object sender, KeyEventArgs e)
{
if(!CheckInit()) return;
this.InternalChange=true;
switch(e.KeyCode)
{
case(Keys.Up):
{
if(this.InputList.SelectedIndex>0) this.InputList.SelectedIndex-=1;
e.Handled=true;
break;
}
case(Keys.Down):
{
if(this.InputList.SelectedIndex<this.InputList.Items.Count-1) this.InputList.SelectedIndex+=1;
e.Handled=true;
break;
}
case(Keys.Enter):
{
ClipCursor(false);
Attaching();
break;
}
case(Keys.Escape):
{
this.InputList.DroppedDown=false;
ClipCursor(false);
break;
}
default:
{
this._IsAttached=false;
this.InternalChange=false;
break;
}
}
OnKeyDown(new KeyEventArgs(e.KeyCode));
OnKeyPress(new KeyPressEventArgs((char)e.KeyValue));
OnKeyUp(new KeyEventArgs(e.KeyCode));
}
private void InputText_Click(object sender, EventArgs e)
{
ClipCursor(false);
OnClick(new EventArgs());
}
private void ShowDropDown()
{
if(this.Items.Count==0)
{
this.InputList.DataSource=null;
this.InputList.DroppedDown=false;
ClipCursor(false);
return;
}
if(!CheckInit()||this._IsAttached) return;
this.InputList.DataSource=null;
this.InputList.DataSource=this.Items;
this.InputList.DisplayMember="Text";
this.InputList.ValueMember="ID";
if(this.InputList.Items.Count>0) this.InputList.SelectedIndex=0;
this.InputList.DroppedDown=(this.Text!=null&&this.Text!=""&&Items.Count>0);
}
private void Attaching()
{
if(this.InputList.SelectedIndex<0) return;
this.InternalChange=true;
//this.RetVal=(SIItem)this.Items[this.InputList.SelectedIndex];
this.Text=((SIItem)this.Items[this.InputList.SelectedIndex]).Text;
this._IsAttached=true;
this.InputList.DroppedDown=false;
if(!this.InputText.Focused) this.InputText.Focus();
this.InputText.SelectionStart=0;
this.InputText.SelectionLength=this.InputText.Text.Length;
this.InputList.DataSource=null;
if(AttachInput!=null) this.AttachInput(this,this.Text);
}
private bool CheckInit()
{
return(this.SICollection!=null&&
this.SIFieldID!=null&&this.SIFieldID!=""&&
this.SIFieldSimpleInput!=null&&this.SIFieldSimpleInput!=""&&
this.SIFieldText!=null&&this.SIFieldText!="");
}
private void InputList_DropDown(object sender, EventArgs e)
{
ClipCursor(true);
}
private void ClipCursor(bool LockFlag)
{
if(LockFlag)
Cursor.Clip=this.RectangleToScreen(this.InputText.Bounds);
else
Cursor.Clip=Screen.PrimaryScreen.Bounds;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -