2011-10-31 54 views

回答

4

表單有一個名爲「AcceptButton」的屬性,用於標識應該與「Enter」按鍵關聯的按鈕。它被認爲是該表格的「默認行爲」。

此處瞭解詳情:

Windows Form - AcceptButton property

+0

謝謝..這是有幫助的! – Aan

7

這將是最好的做法

private void txtSearch_Enter(object sender, EventArgs e) 
{ 
    AcceptButton = btnSearch; 
} 

private void txtSearch_Leave(object sender, EventArgs e) 
{ 
    AcceptButton = null; 
} 
+1

謝謝。好的和有用的代碼! – Aan

0

如果你想使用輸入/返回其他的東西,你也可以嘗試:

private void EnterKeyAction() 
{ 
    // Search... 
} 

private void btnEnter_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (e.KeyChar == (char)Keys.Return) 
      EnterKeyAction();  
} 

private void btnEnter_Click(object sender, EventArgs e) 
{ 
    EnterKeyAction(); 
} 
相關問題