Search code examples
c++buildervcltlistview

VCL TListView and EditCaption()


In C++Builder, I've got a TListView with some items.

Whenever someone enters a numeric value, it should be applied to the caption of the currently selected TListItem in the ListView:

void __fastcall TFormMain::ListViewKeyDown(TObject *Sender, WORD &Key,
  TShiftState Shift)
{
    if ( Key >= '0' && Key <= '9' )
    {
        if ( !ListView->IsEditing() )
        {
            ListView->Selected->EditCaption();
        }
    }
}

This code works "somehow": Entering a numeric value puts the TListView into editing mode. Then I have to re-enter the number to apply it to the TListItem's caption.

Isn't there a way to do EditCaption() and apply the number just in one single step?


Solution

  • Isn't there a way to do EditCaption() and apply the number just in one single step?

    You would have to manually forward the typed digit to the ListView's editor after invoking it, eg:

    void __fastcall TFormMain::ListViewKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
    {
        if ( (Key >= '0') && (Key <= '9') )
        {
            TListItem *Item = ListView->Selected;
            if ( (Item) && (!ListView->IsEditing()) )
            {
                Item->EditCaption();
    
                HWND hWnd = ListView_GetEditControl(ListView->Handle);
    
                TCHAR str[2] = {TCHAR(Key), 0};
                SetWindowText(hWnd, str);
            }
        }
    }