動作確認
C++ Builder XE4
Rad Studio 10.2 Tokyo Update 2 (追記: 2017/12/26)
https://groups.google.com/forum/#!topic/borland.public.delphi.vcl.components.using/85snFfhvhTI
にある実装例
- I often look for the backspace (#8) key in an OnKeyPress handler and set the ItemIndex to -1 if I see it.
backspaceでの実装例
void __fastcall TItem_options::ComboBox1KeyPress(TObject *Sender, System::WideChar &Key)
{
// Backspaceキーによる選択解除
TComboBox *cbPtr = (TComboBox *)Sender;
if (Key == VK_BACK) {
SendMessageA(cbPtr->Handle, CB_SHOWDROPDOWN, 0, 0); // Close ComboBox
cbPtr->ItemIndex = -1; // deselect
}
}
ItemIndex = -1にするだけではコンボボックスが開いたままとなり、閉じる時に再度選択されてしまう。
閉じる処理の参考 http://www.geocities.jp/asumaroyuumaro/program/tips/ComboBoxMessage.html
Ctrl-deleteで選択解除する場合
void __fastcall TItem_options::ComboBox1KeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
if (Shift.Contains(ssCtrl) && Key == VK_DELETE) {
SendMessageA(ComboBox1->Handle, CB_SHOWDROPDOWN, 0, 0); // Close ComboBox
ComboBox1->ItemIndex = -1; // deselect
}
}
Ctrl-Deleteの参考 http://bcbjournal.org/forums/viewtopic.php?f=10&t=2587
Shift == ssCtrlではエラーがでた。Contains()を使う。
追記
(2017/12/26)
ItemIndex = -1にするだけではコンボボックスが開いたままとなり、閉じる時に再度選択されてしまう。
この状況が再現しなくなっている?