views:

119

answers:

2

I have a wxTextCtrl-derived class that overrides OnDropFiles. However, dragging something over the control does nothing. (The cursor changes to the 'not allowed' cursor.) I tried DragAcceptFiles(true) but that only enabled the built-in drop handler. (Which just loads the file into the control.) How can I get my own handler to be invoked?

I also tried SetDropTarget, but that never got invoked either. It worked in a wxFrame, though.

Any ideas?

+1  A: 

This is a stripped down version of what I have in one of my projects:

My form code

wxTextCtrl* textctrl = new wxTextCtrl(...);
textctrl->SetDropTarget(new DropFiles(textctrl));

The dropfiles class

class DropFiles: public wxFileDropTarget{
public:
    DropFiles(wxTextCtrl *text): m_Text(text){}
    bool OnDropFiles(wxCoord x, wxCoord y, const wxArrayString& arrFilenames);

private:
    wxTextCtrl *m_Text;
};

bool DropFiles::OnDropFiles(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), const wxArrayString& arrFilenames){
    //Just take the first filename
    m_Text->SetValue(arrFilenames.Item(0));
    return true;
}

Hope that helps!

SteveL
Thanks for trying but that still didn't work - OnDropFiles never gets called. I did find a solution, though. See my answer.
George Edison
Odd, I certainly don't handle it, I guess it is because you are deriving from wxTextCtrl perhaps?
SteveL
A: 

You have to handle the EVT_DROP_FILES event. Any other attempt to get notified will fail :(

George Edison