c++wxwidgetswxtextctrl

How to disable a wxTextCtrl Widget in wxWidgets C++?


I'm writing a wxWidgets program in C++. It has several single line wxTextCtrls in it and I need to disable them so the user cannot enter text in them. Later, when a menu item is clicked, I want to enable them again. How do I do this?


Solution

  • Since wxTextCtrl is a subclass of wxWindow, it contains (probably overridden) virtual method Enable of wxWindow, documentation for which can be found here and which controls whether the window is enabled for user input according to its boolean argument (which defaults to true - enable input). Also, there is a handy non-virtual Disable method, which is defined to be equivalent to Enable(false).

    You can use it like this to disable text control (assuming you save pointer to your wxTextCtrl instance in a m_pTextCtrl member of your window class):

    m_pTextCtrl = new wxTextCtrl(...);
    // ...
    m_pTextCtrl->Disable();
    

    and like this to enable it in your menu item event handler:

    m_pTextCtrl->Enable();