在錄入界面中,用戶每每須要按回車鍵時光標自動跳入下一個文本框,以方便錄入操做。在C#中實現該功能有多種方法,如下是小編收集的不使用TAB鍵,而直接用回車鍵將光標轉到下一個文本框的實現方法。javascript
1、利用Windows消息模擬發送Tab鍵
將各個TextBox的TabIndex屬性按順序編號一、二、3……,而後將TextBox的TabStop屬性置爲True,在每個TextBox的鍵盤按下事件中,執行如下代碼便可(各個TextBox可共用同一個鍵盤按下事件)。html
///
<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}
"
); } }
2、手動置下一個須要獲取焦點的文本框
若是想讓焦點跳到任意文本框或者其餘地方, 在文本框的鍵盤按下事件中,將焦點放到目標文本框上。java
private
void
textBox1_KeyPress(
object
sender, KeyPressEventArgs e) {
if
(e.KeyChar
==
(
char
)Keys.Enter) { textBox2.focus();
//
當在文本框1中檢查到回車鍵時,直接將焦點轉入TextBox2
} }
3、利用控件的SelectNextControl函數
按方法一中設置好TextBox的TabIndex和TabStop屬性,在C# 回車Enter事件中,調用控件的SelectNextControl函數,是的輸入焦點跳到下一個TextBox(文本框)。c#
如下示例是在窗口顯示控件中,統一爲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
);} } }
4、偷樑換柱,將回車鍵替換成Tab鍵
先設置這些控件的Tab順序,而後在窗體的後臺代碼中添加以下函數就能夠了ide
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); }
5、C#WinForm自動跳轉回車問題
有個TEXTBOX輸入框!屬性設置了能夠換行multiline設置了True!在keydwon加了換下一個輸入框的命令函數
private
void
textDisease_KeyDown(
object
sender, KeyEventArgs e) {
if
(e.KeyValue
==
13
) { textOrganization.Focus(); textOrganization.SelectAll(); } }
在換到下一個時,原來的輸入框也會加了個有回車!如何處理掉這個回車!this
解決方法1:設置按鍵的e.Handled屬性
textDisease_KeyDown(
object
sender, KeyEventArgs e) {
if
(e.KeyValue
==
13
) { e.Handled
=
True textOrganization.Focus(); textOrganization.SelectAll(); } }
加一句e.Handled = True。目的是讓系統不要處理該Enter按鍵。spa
解決辦法2:使用textChange事件
private
void
textDisease_TextChanged(
object
sender, EventArgs e) { textDisease.Text
=
textDisease.Text.Trim().Replace(
"
\r\n
"
,
""
); }
6、在網頁程序中不使用TAB鍵直接用回車鍵將光標轉到下一個文本框的方法
在C#.NET中,可使用JaveScript腳本實現不使用TAB鍵,而直接用回車鍵將光標轉到下一個文本框。firefox
<%
@ Page language
=
"
c#
"
Codebehind
=
"
WebForm1.aspx.cs
"
AutoEventWireup
=
"
false
"
Inherits
=
"
回車使下一個文本框獲得焦點.WebForm1
"
%>
<
script language
=
javascript
>
function setfocus() { document.all.t2.focus(); }
</
script
>
7、如何在文本框輸入框裏按回車鍵,光標自動跳轉到下一個文本框輸入框或者是執行某按鈕的提交?
這兩個問題的本質是同樣的,通常是藉助客戶端腳原本解決,舉例以下。
<%
@ 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()方法容許程序在服務器端設置文本框的焦點..
8、回車焦點自動跳到下一個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
>