c++c++buildervcl

How can I set SelStart and SelLength when setting focus to a TComboBox


When I move focus with the Tab key to a VCL TComboBox control containing text in the Text field, the complete text is always displayed as selected.

How can this be changed so that the selection can be customized in C++Builder?

Changing SelStart and SelLength in OnEnter doesn't seem to help.

What am I doing wrong?


Solution

  • One option is to have your OnEnter handler use TThread::ForceQueue() to delay your custom selection until after the standard selection gets set first, eg:

    void __fastcall TMyForm::ComboBox1Enter(TObject *Sender)
    {
        TThread::ForceQueue(NULL, &DoMySelect);
    }
    
    void __fastcall TMyForm::DoMySelect()
    {
        ComboBox1->SelStart = ...;
        ComboBox1->SelLength = ...;
    }
    

    Or, if you are using one of the clang-based compilers, you can use a C++ lambda instead of a separate class method, eg:

    void __fastcall TMyForm::ComboBox1Enter(TObject *Sender)
    {
        TThread::ForceQueue(nullptr,
            [this](){
                ComboBox1->SelStart = ...;
                ComboBox1->SelLength = ...;
            }
        );
    }