using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Diagnostics;
namespace WindowsControlLibrary1
{
/// <summary>
/// MyTextBox 的摘要说明。
/// </summary>
public class MyTextBox : System.Windows.Forms.TextBox
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
///
private bool IsEnterSetFocus=false;
[
Category("Appearance"),
Description("Test")
]
public bool SetFoucusByEnter
{
get
{
return IsEnterSetFocus;
}
set
{
IsEnterSetFocus=value;
if (IsEnterSetFocus)
this.KeyPress+=new System.Windows.Forms.KeyPressEventHandler(PressKey);
else
this.KeyPress-=new System.Windows.Forms.KeyPressEventHandler(PressKey);
}
}
private System.ComponentModel.Container components = null;
public MyTextBox()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitializeComponent 调用后添加任何初始化
Text="";
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
SetFoucusByEnter=true;
}
#endregion
public virtual void PressKey(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar==(char)13)
{
Control selControl,myControl;
myControl=this.Parent;
selControl=GetChildControl(ref myControl,this.TabIndex);
if (selControl==null)
{
selControl=GetBrothControl(ref myControl);
}
if (selControl==null)
{
myControl=(Control)sender;
selControl=GetParentControl(ref myControl);
}
if (selControl!=null) selControl.Focus();
}
}
private Control GetChildControl(ref Control firstControl,int nTabIndex)
{
int nTab,nCount;
Control selControl=null;
Control myControl=firstControl.Controls[0];
nTab=nTabIndex+1;
nCount=firstControl.Controls.Count-1;
for(int i=0;i<=nCount;i++)
{
if (firstControl.Controls[i].TabIndex==nTab)
{
myControl=firstControl.Controls[i];
if (myControl.Controls.Count>0)
{
selControl=GetChildControl(ref myControl,-1);
if (selControl!=null) break;
}
else if (myControl.CanFocus)
{
selControl=myControl;
break;
}
}
}
return selControl;
}
private Control GetParentControl(ref Control firstControl)
{
bool IsDone=false;
Control selControl=null;
do
{
if (firstControl.Parent!=null)
firstControl=firstControl.Parent;
else
IsDone=true;
} while(!IsDone);
selControl=GetChildControl(ref firstControl,-1);
return selControl;
}
private Control GetBrothControl(ref Control firstControl)
{
bool IsFind=false;
int nIndex;
nIndex=0;
Control selControl=null;
while ((firstControl.Parent!=null)&&(!IsFind))
{
nIndex=firstControl.TabIndex+1;
firstControl=firstControl.Parent;
for (int i=0;i<firstControl.Controls.Count;i++)
if (firstControl.Controls[i].TabIndex==nIndex)
{
firstControl=firstControl.Controls[i];
if (firstControl.Controls.Count>0)
{
selControl=GetChildControl(ref firstControl,-1);
if (selControl!=null)
{
IsFind=true;
break;
}
}
else
{
selControl=firstControl;
IsFind=true;
break;
}
}
}
return selControl;
}
}
}