On a few computers computers (meaning most are fine), when adding an item to a tree control, for some reason TreeView_InsertItem returns NULL (Indicating something went wrong).
Unfortunately the MSDN site doesn’t specify that an error code is set (and indeed, calling GetLastError simply tells me: 0 = The operation completed successfully.
Neither does it specify what could cause a failure or how to deal with one.
So I’m wondering if anyone has encountered this before, or has any suggestions as to how to debug the problem.
The machines are all running Windows 7.
A disclaimer: I’m actually going through wxWidgets, but we’ve dug into the code and the problem appears to lie at the windows api level, and not with wx itself.
Update:
Basically, the idea is, you inherit from this class, and then implement populate, which simply calls AddItem(). There is also a version of AddItem() for specifying the root item, but since I fail on the first call to Append(), it’s presence doesn’t help.
The tree is created in wxFormBuilder with the following flags: wxTR_DEFAULT_STYLE, wxTR_HAS_BUTTONS, wxTR_HIDE_ROOT, wxTR_LINES_AT_ROOT, wxTR_SINGLE
This also works perfectly fine from my own PC
ItemSelector::ItemSelector( wxWindow* parent )
: m_tree( NULL ),
m_initialised( false )
{
wxXmlResource::Get()->LoadFrame( this, parent, wxT( "ItemSelector" ) );
m_tree = XRCCTRL( *this, "itemTree", wxTreeCtrl );
m_rootItem = m_tree->AddRoot( wxT( "ROOT" ) );
Bind( wxEVT_SHOW, &ItemSelector::OnShow, this );
MakeModal( true );
}
void ItemSelector::OnShow( wxShowEvent& event )
{
if( event.IsShown() )
{
if( !m_initialised )
{
Populate();
m_initialised = true;
}
m_tree->SetFocus();
Layout();
}
}
void ItemSelector::AddItem( const wxString& name, void* someData )
{
//Also tried m_tree->AppendItem( m_rootItem, wxT( "Test" ) ) - same problem
wxTreeItemId newItem = m_tree->AppendItem( m_rootItem, name, -1, -1, new TreeDataClass( someData ) );
}
It looks like the problem was due to the fact that I was calling
Populate()from anwxEVT_SHOWevent.I’ve discovered that this must be a known problem, because
wxDialoghas a[wxEVT_INIT_DIALOG][1]event, precisely for this purpose