标题:我的TEXTBOX控件,实现按ENTER键失去焦点
取消只看楼主
zhengli0
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-3-17
结帖率:0
 问题点数:0 回复次数:0 
我的TEXTBOX控件,实现按ENTER键失去焦点

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;
}


}
}

搜索更多相关主题的帖子: TEXTBOX 控件 ENTER 焦点 
2006-03-21 11:04



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-51765-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.222690 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved