I wanted to copy some example code I found online, but this bit has me puzzled. In the MainForm() method is
treeView.BeforeSelect += (sender, e) => e.Cancel = !_isTreeInitialized;
Task.Delay(10).GetAwaiter().OnCompleted(() => _isTreeInitialized = true);
then in the body
private bool _isTreeInitialized = false;
It appears to attempt to delay any navigation of the treeView until the delay expires, in order to give the treeView time to initialize. Oddly enough the variable _isTreeInitialized does not appear anywhere else in the code.
I thought to put a Watch on _isTreeInitialized in Visual Studio and run the example, only to realize it never changes, but always remains false.
My assumption was that it would become true after the Task.Delay time ended. Am I misunderstanding something fundamental here or is this just an example of broken code? If the latter, it would explain the lack of _isTreeInitialized being used elsewhere in the code.
I ran the example but didn't get any change in the mentioned variable.
Updated code located here on GitHub
Original post of interest on SO
After some clarification I can see the code has the purpose of suppressing the initial selection of Node[0] of the treeview control, I guess the blocking of user interaction with the control temporarily is just a side effect but not necessarily a bad thing.
You're in luck. I can tell you with some certainty what the author of that code intended. The thing is, the native Win32 tree control seems to exhibit a default behavior of selecting the first node. It's caused problems for me before. And as I recall, you'd think it would be really easy to keep this from happening but it's trickier timing-wise than you think.
So, what is the purpose of that code? It succeeds in suppressing the default selection of Node0
for purposes of cosmetics and aesthetics. Also, it was for clarity because I wanted the app to open looking identically to the screenshot that I posted on my original answer. Its theory of operation is to ignore node selection for its first 10 milliseconds of life. (This is why you don't see it changing thereafter.)
I'll probably update that repo with a slightly improved version that simply kills off the first selection.
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
treeView.Sorted = false;
treeView.Invalidated += (sender, e) => checkBoxSorted.Checked = treeView.Sorted;
treeView.ExpandAll();
treeView.Iterate(Index, null);
treeView.Iterate(LogPath, null);
// COSMETIC - Suppresses the default initial selection of Node0
treeView.BeforeSelect += localTemporaryHandler;
void localTemporaryHandler(object sender, TreeViewCancelEventArgs e)
{
e.Cancel = true;
treeView.BeforeSelect -= localTemporaryHandler;
}
}
// NO LONGER REQUIRED private bool _isTreeInitialized = false;
.
.
.
}
AND NOW THIS
It wouldn't be accurate to say that it's "stuck false". If you wanted to observe it, we could reframe it as a property in order to set a breakpoint when it changes.
public MainForm()
{
InitializeComponent();
treeView.Sorted = false;
treeView.Invalidated += (sender, e) => checkBoxSorted.Checked = treeView.Sorted;
treeView.ExpandAll();
treeView.Iterate(Index, null);
treeView.Iterate(LogPath, null);
// COSMETIC - Suppresses the default initial selection of Node0
Task.Delay(10)
.GetAwaiter()
.OnCompleted(() => _isTreeInitialized = true);
treeView.BeforeSelect += (sender, e) => e.Cancel = !_isTreeInitialized;
}
private bool _isTreeInitialized
{
get => _bk;
set
{
if (!Equals(_bk, value))
{
_bk = value;
Debug.Assert(
_isTreeInitialized = true,
"Expecting value to go true after 10 ms.");
}
}
}
bool _bk = false;