<p>In some VB6 code, I have a handler for a TreeView's Collapse event:</p>

<pre><code>Private Sub MyTree_Collapse(ByVal Node as MSComCtlLib.Node)
</code></pre>

<p>This is called whenever a node in the tree is collapsed, whether by the user or programmatically.  As it turns out, through some roundabout execution, it may happen that this handler will wind up telling a node to collapse, leading to infinite recursion.</p>

<p>I can think of multiple ways to skin this cat, but what seems simplest to me is to tell the TreeView not to raise events for some period of time.  I can't find a simple call to let me do this, though.  Has anyone successfully done this, or do I need to keep track of state in some other manner so I can respond appropriately when recursive events come along?</p>

[{'answer_id': 15591, 'author': 'Phil Bachmann', 'author_id': 1855, 'author_profile': 'https://Stackoverflow.com/users/1855', 'pm_score': 2, 'selected': True, 'text': "<p>I think that like many events in VB, it can't be switched off.</p>\n\n<p>Just set a boolean flag as you've suggested.</p>\n"}, {'answer_id': 16629, 'author': 'Matt Dillard', 'author_id': 863, 'author_profile': 'https://Stackoverflow.com/users/863', 'pm_score': 2, 'selected': False, 'text': '<p>@Phil - I came to the same conclusion.  My implementation of <code>MyTree_Collapse</code> now looks something like this (where <code>m_bHandlingCallback</code> is a member variable):</p>\n\n<pre><code>Private Sub MyTree_Collapse(ByVal Node as MSComCtlLib.Node)\n    If m_bHandlingCallback Then Exit Sub\n\n    m_bHandlingCallback = True\n    DoSomeStuff\n    m_bHandlingCallback = False\nEnd Sub\n</code></pre>\n'}, {'answer_id': 148286, 'author': 'Guillermo Phillips', 'author_id': 441661, 'author_profile': 'https://Stackoverflow.com/users/441661', 'pm_score': 0, 'selected': False, 'text': '<p>I would declare the flag variable as STATIC in the Sub.  This avoids making the variable global and makes it keep its value between calls.</p>\n'}, {'answer_id': 1813531, 'author': 'quamrana', 'author_id': 4834, 'author_profile': 'https://Stackoverflow.com/users/4834', 'pm_score': 2, 'selected': False, 'text': "<p>Another way in VB6 is to have an alternate <code>WithEvents</code> reference to the control:</p>\n\n<pre><code>Private WithEvents alt as TreeView\n</code></pre>\n\n<p>and in <code>Form_Load</code>:</p>\n\n<pre><code>Private Sub Form_Load()\nSet alt = MyTree\nEnd Sub\n</code></pre>\n\n<p>Now <code>alt</code> will receive lots of events like this:</p>\n\n<pre><code>Private Sub alt_Collapse(ByVal Node as MSComCtlLib.Node)\nSet alt = Nothing\n'DoSomeStuff'\nSet alt = MyTree\nEnd Sub\n</code></pre>\n\n<p>But, during the <code>DoSomeStuff</code>, the events are unhooked - which also applies to all other event <code>Sub</code>s for <code>alt</code> without the need for them to have intrusive changes.</p>\n"}]
['https://Stackoverflow.com/questions/15163', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/863/']