在录入界面中,用户往往需要按回车键时光标自动跳入下一个文本框,以方便录入操作。在C#中实现该功能有多种方法,以下是小编收集的不使用TAB键,而直接用回车键将光标转到下一个文本框的实现方法。
一、利用Windows消息模拟发送Tab键
将各个TextBox的TabIndex属性按顺序编号1、2、3……,然后将TextBox的TabStop属性置为True,在每一个TextBox的键盘按下事件中,执行以下代码即可(各个TextBox可共用同一个键盘按下事件)。
///
<summary>
///
鼠标按键事件。
///
如果检查到按下的是回车键,则发一个消息,模拟键盘按以下Tab键,以使输入焦点转移到下一个文本框(或其他焦点可停留的控件)
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
private
void
textBox_KeyPress(
object
sender, KeyPressEventArgs e) {
if
(e.KeyChar
==
(
char
)Keys.Enter) { SendKeys.Send(
"
{tab}
"
); } }
二、手动置下一个需要获取焦点的文本框
如果想让焦点跳到任意文本框或者其他地方, 在文本框的键盘按下事件中,将焦点放到目标文本框上。
private
void
textBox1_KeyPress(
object
sender, KeyPressEventArgs e) {
if
(e.KeyChar
==
(
char
)Keys.Enter) { textBox2.focus();
//
当在文本框1中检查到回车键时,直接将焦点转入TextBox2
} }
三、利用控件的SelectNextControl函数
按方法一中设置好TextBox的TabIndex和TabStop属性,在C# 回车Enter事件中,调用控件的SelectNextControl函数,是的输入焦点跳到下一个TextBox(文本框)。
以下示例是在窗口显示控件中,统一为TextBox的鼠标按下KeyDown事件添加处理函数。(示例来自红日的百度空间)
protected
override
void
OnShown(EventArgs e) {
base
.OnShown(e);
foreach
(Control ct
in
this
.Controls) { TextBox tx
=
ct
as
TextBox;
if
(tx
!=
null
) { tx.KeyDown
+=
(sender, e_args)
=>
{
if
(e_args.KeyCode
==
Keys.Enter) {
this
.SelectNextControl(tx,
true
,
true
,
false
,
true
);} } }
四、偷梁换柱,将回车键替换成Tab键
先设置这些控件的Tab顺序,然后在窗体的后台代码中添加如下函数就可以了
protected
override
bool
ProcessDialogKey(Keys keyData) {
if
(keyData
==
Keys.Enter)
//
按下的是回车键
{
foreach
(Control c
in
this
.Controls) {
if
(c
is
System.Windows.Forms.TextBox)
//
当前控件是文本框控件
{ keyData
=
Keys.Tab; } } keyData
=
Keys.Tab; }
return
base
.ProcessDialogKey(keyData); }
protected
override
bool
ProcessDialogKey(Keys keyData) {
if
((ActiveControl
is
TextBox
||
ActiveControl
is
ComboBox)
&&
keyData
==
Keys.Enter) { keyData
=
Keys.Tab; }
return
base
.ProcessDialogKey(keyData); }
五、C#WinForm自动跳转回车问题
有个TEXTBOX输入框!属性设置了可以换行multiline设置了True!在keydwon加了换下一个输入框的命令
private
void
textDisease_KeyDown(
object
sender, KeyEventArgs e) {
if
(e.KeyValue
==
13
) { textOrganization.Focus(); textOrganization.SelectAll(); } }
在换到下一个时,原来的输入框也会加了个有回车!如何处理掉这个回车!
解决方法1:设置按键的e.Handled属性
textDisease_KeyDown(
object
sender, KeyEventArgs e) {
if
(e.KeyValue
==
13
) { e.Handled
=
True textOrganization.Focus(); textOrganization.SelectAll(); } }
加一句e.Handled = True。目的是让系统不要处理该Enter按键。
解决办法2:使用textChange事件
private
void
textDisease_TextChanged(
object
sender, EventArgs e) { textDisease.Text
=
textDisease.Text.Trim().Replace(
"
\r\n
"
,
""
); }
六、在网页程序中不使用TAB键直接用回车键将光标转到下一个文本框的方法
在C#.NET中,可以使用JaveScript脚本实现不使用TAB键,而直接用回车键将光标转到下一个文本框。
<%
@ Page language
=
"
c#
"
Codebehind
=
"
WebForm1.aspx.cs
"
AutoEventWireup
=
"
false
"
Inherits
=
"
回车使下一个文本框得到焦点.WebForm1
"
%>
<
script language
=
javascript
>
function setfocus() { document.all.t2.focus(); }
</
script
>
七、如何在文本框输入框里按回车键,光标自动跳转到下一个文本框输入框或者是执行某按钮的提交?
这两个问题的本质是一样的,一般是借助客户端脚本来解决,举例如下。
<%
@ Page Language
=
"
C#
"
%>
<
script runat
=
"
server
"
>
protected
void
Button_Click(
object
sender,EventArgs e)
{
Lable1.Text
=
"
您点击了:
"
+
((Button)sender).Text;
}
protected
void
Page_Load(
object
sender,EventArgs e)
{
int
TextBoxNum
=
4
;
for
(
int
i
=
1
;i
<=
TextBoxNum;i
++
)
{
if
(i
!=
TextBoxNum)
{
((TextBox)form1.FindControl(
"
TextBox
"
+
i.ToString())).Attributes.Add(
"
onkeydown
"
,
"
TabNext(event,'0','
"
+
((TextBox)form1.FindControl(
"
TextBox+(1+i).ToString())).ClientID+'
"
)
"
);
}
else
{
((TextBox)form1.FindControl(
"
TextBox
"
+
i.ToString())).Attributes.Add(
"
onkeydown
"
,
"
TabNext(event,'
"
+
Button2.ClientID
+
'
",)");
}
}
}
</
script
>
在页面中有一个form1的表单,4个TextBox,还有一个BUtton2按钮,一个Label1,在页面中添加下列javascript脚本:
<
script language
=
"
javascript
"
type
=
"
text/javascsript
"
>
function TabNext(e,s1,s2) {
if
(window.
event
)
//
ie
{ keynum
=
e.KeyCode }
else
if
(e.which)
//
netscape,firefox,opera
{ keynum
=
e.which }
if
(keynum
==
13
) {
if
(s1
==
"
0
"
) { document.getElementById(s2).focus() }
else
{ docuemnt.getElementById(s1).click() }
if
(window.
event
) { e.returnValue
=
false
; e.cancelBubble
=
true
; }
else
if
(e.which) { e.rreventDefault() } } }
</
script
>
记得要把button2的onclick的onclick事件绑定到button_click上。在使用这个方法时,注意TextBox控件的ID的命名规则 TextBox1,TextBox2,TextBox3....和对应的客户端的ID属性。TextBox的Focus()方法允许程序在服务器端设置文本框的焦点..
八、回车焦点自动跳到下一个TEXTBOX
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
test.aspx.cs
"
Inherits
=
"
test
"
%>
<!
DOCTYPE html PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
<
head runat
=
"
server
"
>
<
title
>
无标题页
</
title
>
<
script language
=
javascript
>
<!--
function setfocus() {
if
(
event
.keyCode
==
13
) {
event
.keyCode
=
9
} }
//
-->
</
script
>
</
head
>
<
body
>
<
form id
=
"
Form1
"
onkeydown
=
"
setfocus()
"
runat
=
"
server
"
>
<
DIV align
=
"
left
"
>
<
asp:TextBox id
=
"
TextBox1
"
runat
=
"
server
"
></
asp:TextBox
></
DIV
>
<
DIV align
=
"
left
"
>
<
asp:TextBox id
=
"
TextBox2
"
runat
=
"
server
"
></
asp:TextBox
></
DIV
>
<
DIV align
=
"
left
"
>
<
asp:TextBox id
=
"
TextBox3
"
runat
=
"
server
"
></
asp:TextBox
></
DIV
>
<
DIV align
=
"
left
"
>
</
DIV
>
<
DIV align
=
"
left
"
>
<
asp:Button id
=
"
Button1
"
runat
=
"
server
"
Text
=
"
Button
"
></
asp:Button
></
DIV
>
</
form
>
</
body
>
</
html
>