Roin
Roin

Reputation: 53

wxWidgets 2.9 AutoComplete in wxComboBox custom EnterEvent

I have an issue concerning wxWidgets 2.9 and the wxComboBox AutoComplete feature. Below is my event table which takes the ENTER event of my ComboBox, on enter I fire OnComboEnter. If I do that I'm not able to select an item from the AutoComplete list since it directly executes the OnComboEnter method on the text the user typed into the ComboBox.

BEGIN_EVENT_TABLE(LVFilterPanel, wxPanel)
   EVT_TEXT_ENTER(wxID_ANY, LVFilterPanel::OnComboEnter)
   EVT_CONTEXT_MENU(LVFilterPanel::OnComboContextMenu)
END_EVENT_TABLE()

My ComboBox is declared like this:

mFilterString = new wxComboBox(this, LV_FILTER_STRING, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, 0, wxTE_PROCESS_ENTER);

AutoComplet is done using the default AutoComplete method found in wxWidgets 2.9:

mFilterString->AutoComplete(historyarr);

historyarr is a wxArrayString filled with the strings which were previously typed in by the user. The OnComboEnter method looks like this:

void LVFilterPanel::OnComboEnter(wxCommandEvent& event) {
    wxCommandEvent ce(wxEVT_COMMAND_BUTTON_CLICKED, LV_FILTER);
    static_cast<wxButton*>(FindWindow(LV_FILTER))->Command(ce);
}

My question now is, how could I change the event handling in a way that it is able to select the item first and then processes OnComboEnter, so the user is able to select an item first (or may not select an item at all and directly hit enter to launch the event and the OnComboEnter method). Thanks in advance.

Greets,

Roin

Upvotes: 3

Views: 1658

Answers (2)

lbmonsalve
lbmonsalve

Reputation: 11

I had same problem but wxTextCtrl, this is my solution:

TextCtrl2 = new wxTextCtrl(this, ID_TEXTCTRL2, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL2"));

TextCtrl2->SetHint("Search...");
TextCtrl2->AutoComplete(m_AutoCompleteChoices);
TextCtrl2->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(StartFrame::OnKeyDown),NULL, this);


void StartFrame::OnKeyDown(wxKeyEvent& event)
{
    switch (event.GetKeyCode()) {
    case WXK_RETURN:
        QueryCache(TextCtrl2->GetValue()); // <- This is anything to do!
        break;
    }
    event.Skip();
}

I could use wxSearchCtrl but Autocomplete not working in that control and I don't know why.

Upvotes: 1

VZ.
VZ.

Reputation: 22688

If you need to execute your event handler after the standard handling takes place, the usual trick is to do nothing in your event handler (which means also calling event.Skip(), of course!) except setting some internal flag and check this flag in EVT_IDLE handler. If it is set, then do whatever you need (e.g. button->Command() in your case) and reset the flag.

This approach ensures that the handler is ran "soon after" the event happens without interfering with normal event processing.

Upvotes: 2

Related Questions