Opened 2 years ago

Last modified 2 years ago

#18136 new defect

The autocomplete method and the wxEVT_TEXT event handler for the wxTextCtrl object

Reported by: Cn_nC Owned by:
Priority: normal Milestone:
Component: samples Version: 3.1.1
Keywords: Cc:
Blocked By: Blocking:
Patch: no

Description

A detailed description of the problem is posted on the forum: https://forums.wxwidgets.org/viewtopic.php?f=1&t=44631
Code of the example in which there is a problem:

#include <wx/wx.h>

class Window : public wxFrame {
public:
    const int ID_TC_CURRECT = 1;
    const int ID_TC_UNCURRECT = 2;
    wxPanel *panel = new wxPanel(this);
    wxBoxSizer *box_vertical = new wxBoxSizer(wxVERTICAL);
    wxTextCtrl *tc_currect = new wxTextCtrl(panel, ID_TC_CURRECT);
    wxTextCtrl *tc_uncurrect = new wxTextCtrl(panel, ID_TC_UNCURRECT);
    wxArrayString *arr = new wxArrayString();
    Window (
        wxWindow *parent,
        wxWindowID id,
        const wxString &title,
        const wxPoint &pos=wxDefaultPosition,
        const wxSize &size=wxDefaultSize,
        long style=wxDEFAULT_FRAME_STYLE,
        const wxString &name=wxFrameNameStr) :
    wxFrame (
        parent,
        id,
        title,
        pos,
        size,
        style,
        name
    ) {
        box_vertical->Add(tc_currect);
        box_vertical->Add(tc_uncurrect);
        panel->SetSizer(box_vertical);
        /*Complete for example*/
        arr->Add(wxT("111222333"));
        arr->Add(wxT("22334455"));
        arr->Add(wxT("543"));
        arr->Add(wxT("12"));
        arr->Add(wxT("23"));
        arr->Add(wxT("34422"));
        arr->Add(wxT("154"));
        tc_currect->AutoComplete(*arr);   
       
        Bind(wxEVT_TEXT, m_textprint, this, ID_TC_UNCURRECT);
    }
    void m_textprint(wxCommandEvent & event);
};

void Window::m_textprint(wxCommandEvent & event) {
    /*Here, a string array is formed when the xml document is parsed,
    * which is added as an argument for the text field autocomplete method.
    * Each time you enter a new letter, the array changes.
    * For simple autocomplete method is added to an immutable array.
    * Going on the fact that autocomplete is not working properly -
    * automatically fills the text box and does not gives you the opportunity to change it.*/
    Window::tc_uncurrect->AutoComplete(*(Window::arr));
}

class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};

IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
   Window *window = new Window(NULL, wxID_ANY, wxT("Simple Window"));
    window->Show(true);
    return true;
}

There are two wxTextCtrl objects in the window: object number 1 (tc_currect) and object number 2 (tc_uncurrect). For both objects, the appropriate autocomplete methods are invoked, which take as the argument a text array filled in the constructor of the "Window" class. For object № 1, the autocomplete method is also called in the constructor of the "Window" class and works correctly. For object № 2, the autocomplete method is called in the class method "Window :: m_textprint". The method of the Window :: m_textprint class is called when the wxEVT_TEXT event occurs in object № 2. The following problem arises: the call to the autocomplete method is somehow regarded as a wxEVT_TEXT event and the text field of object № 2 automatically begins to be populated with one of the elements of the wxArrayString text array. That is, typing automatically selects, but you can not delete the last characters in the text box, because the field is immediately filled in again.

The example was compiled and launched on Windows 7, Windows 10; wxWidgets 3.1.1 is loaded separately from the official site and is separately cloned using GitHub; IDE CodeLite.

Change History (4)

comment:1 Changed 2 years ago by Cn_nC

  • Cc obstaclestone@… added

comment:2 Changed 2 years ago by Cn_nC

  • Cc obstaclestone@… removed
  • Version changed from 3.0.3 to 3.1.1

comment:3 Changed 2 years ago by vadz

  • Status changed from new to infoneeded_new

I have no idea how can this code work, do you realize that you create all the windows before calling the base class ctor? Do you really do this intentionally? Because I can't see how it doesn't result in much worse problems than autocomplete not working...

Please rewrite the code in normal way, with the members being initialized inside the ctor and check if this doesn't resolve the problem you're observing.

comment:4 Changed 2 years ago by oneeyeman

  • Status changed from infoneeded_new to new

@Cn_nC, can we close the ticket as invalid? Did you do what Vadim advised?

Note: See TracTickets for help on using tickets.