0

I want to just do something simple, override WM_COPY of a TEdit control.

If I try to use the following, you can't change the type in the .dfm or it complains that it doesn't exist so you have to leave it TEdit. But now when saving the .h/.cpp complains about it should be a TEdit when I want it to be TMyEdit.

In MyForm.h:

class TMyEdit : public TEdit
{
    private:
        virtual void __fastcall WndProc(TMessage &Message);
};

//---------------------------------------------------------------------------
class TMyForm : public TForm
{
  // ...
  TMyEdit *m_MyEditCtl; // previously TEdit *m_MyEditCtl
  // ...
};

In the MyForm.cpp

// ...
void __fastcall TMyEdit::WndProc(TMessage &Message)
{
    if (Message.Msg == WM_COPY) {
      // I'll handle it ....
    }
    else {
        __super::WndProc(Message);
    }
}
// ...

What is the correct way to do this in C++Builder so it works in the form and C++ source files?

TIA!!

user3161924
  • 1,849
  • 18
  • 33

1 Answers1

1

Design-time controls must be installed in the IDE. So, if you want to use your custom TMyEdit at design-time, you will have to move it into a separate .BPL package, and then you can install that package into the IDE and have it register your class. Only then will you be able to use your class with the Form Designer.

Read Embarcadero's documentation for more details about writing custom components:

Introduction to component creation


UPDATE:

However, that being said, there is an alternative (thanks to this answer), which will allow you to use the standard TEdit at design-time, and use TMyEdit at runtime, no .BPL needed:

class TMyEdit : public TEdit
{
    // ...
};

class TMyForm : public TForm
{
__published:
    // ...
    TEdit *m_MyEditCtl;
    // ...
protected:
    virtual void __fastcall ReadState(TReader *Reader);
    // ...
private:
    void __fastcall FindComponentClass(TReader *Reader, const String ClassName, TComponentClass &ComponentClass);
    // ...
};

void __fastcall TMyForm::ReadState(TReader *Reader)
{
    Reader->OnFindComponentClass = &FindComponentClass;
    TForm::ReadState(Reader);
}

void __fastcall TMyForm::FindComponentClass(TReader *Reader, const String ClassName, TComponentClass &ComponentClass)
{
    if (ComponentClass == __classid(TEdit))
        ComponentClass = __classid(TMyEdit);
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770