The correct way to modify the contents of a VCL component

Quite often, when I make VCL programs, I come across this scenario:

  • I have several components on the form that users are allowed to mess with. Most often this is a bunch of edit fields.
  • The contents of these edit fields should be checked by the OnChange event when the user manually enters data.
  • Somewhere else on the form, there is some component that the user can click to get the default values ​​loaded into the edit fields (in TEdit :: Text).

Now I want that whenever a user types in TEdit :: Text, the OnChange event should handle the user's input. But when my program sets TEdit :: Text to the default value, this is not necessary, because then I know that this value is correct.

Unfortunately, writing type code myedit->Text = "Default";raises the OnChange event.

I usually solve this by believing that this is a rather ugly approach: by creating a bool variable is_user_inputthat checks TEdit::OnChange. If so, TEdit :: Text will receive confirmation, otherwise it will be ignored. But, of course, this does not prevent the program from starting TEdit::OnChangewhen it is not needed.

Is there a better or cleaner way to achieve this?

OnChange , ? , OnChange . TEdit::Enabled, , OnChange .

+3
2

OnChange:

template <typename T>
void SetControlTextNoChange(T *Control, const String &S)
{
    TNotifyEvent event = Control->OnChange;
    Control->OnChange = NULL;
    try {
        Control->Text = S;
    }
    __finally {
        Control->OnChange = event;
    }
 }

SetControlTextNoChange(myedit, "Default");

RAII :

template <typename T>
class DisableChangeEvent
{
private:
    T *m_control;
    TNotifyEvent m_event;
public:
    DisableChangeEvent(T *control);
    {
        m_control = control;
        m_event = control->OnChange;
        control->OnChange = NULL;
     }

    ~DisableChangeEvent();
    {
        m_control->OnChange = m_event;
    }

    T* operator->() { return m_control; }
};

DisableChangeEvent(myedit)->Text = "Default";
+6

OnChange , ?

Sender, , (Sender == ButtonSetDefaults) . , , OnChange .

, , , .

0

All Articles