設定表單 KeyPreview = true,在 OnKeyPress 事件中攔截 VK_RETURN 並發送 WM_NEXTDLGCTL 訊息。
void __fastcall TForm1::FormKeyPress(TObject *Sender, char &Key)
{
if (Key == VK_RETURN) {
SendMessage(this->Handle, WM_NEXTDLGCTL, 0, 0); // 移至下一控制項
Key = 0; // 避免發出嗶聲
}
}
KeyPreview = true 讓表單優先接收按鍵訊息。WM_NEXTDLGCTL 會依 TabOrder 移動焦點。Key = 0 取消字元輸入,防止編輯框接收換行符或發出系統提示音。