使用者習慣按 Enter (而非 Tab) 在輸入框間移動焦點。
void __fastcall TForm1::FormKeyPress(TObject *Sender, char &Key)
{
if (Key == VK_RETURN) {
SendMessage(Handle, WM_NEXTDLGCTL, 0, 0); // 移至下一控件
Key = 0; // 吞掉按鍵,避免觸發按鈕 Click 等
}
}
`WM_NEXTDLGCTL` 讓系統按 `TabOrder` 尋找下一個可聚焦控件。`Key=0` 防止字元輸入或預設按鈕觸發。
若 `TRichEdit`/`TMemo` 需保留 Enter 換行,可判斷 `ActiveControl` 類型:
if (Key == VK_RETURN && !dynamic_cast(ActiveControl) && !dynamic_cast(ActiveControl)) {
SendMessage(Handle, WM_NEXTDLGCTL, 0, 0);
Key = 0;
}