TreeCtrl.InsertItemBefore

I discovered something undocumented that is worth pointing out to list,
if it hasn’t been already.
In the documentation, TreeCtrl.InsertItem
wxTreeItemId InsertItem(**const wxTreeItemId&**parent, size_t before, **const
wxString&*text, int image = -1, int selImage = -1, wxTreeItemData data = NULL)
Inserts an item after a given one (previous) or before one identified by its position (before). before must be less than the number of children.

However, before can in fact be equal to the number of children. When you do this, the item is appended to the end of the children, providing

a single method to put an item anywhere. This is extremely useful and to the best of my knowledge undocumented.

Included is a minimal example proving this.

D

import wx

class MyFrame(wx.Frame):

def __init__(self, parent, id, title):

    wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(450, 350))

    tree = wx.TreeCtrl(self, 1, wx.DefaultPosition, (-1,-1), wx.TR_HAS_BUTTONS|wx.TR_EDIT_LABELS)

    root = tree.AddRoot('Root Node')

    tree.AppendItem(root,'child1')

    tree.AppendItem(root,'child2')

    tree.InsertItemBefore(root,2,'insert before 2') # note, "before = # children

    tree.Expand(root)

    self.Centre()

class MyApp(wx.App):

def OnInit(self):

    frame = MyFrame(None, -1, 'treectrl.py')

    frame.Show(True)

    return True

app = MyApp(0)

app.MainLoop()