Closed
Description
.NET version
.NET9 or .NET10 preview5
Did it work in .NET Framework?
Yes
Did it work in any of the earlier releases of .NET Core or .NET 5+?
It works in .NET8
Issue description
After updating .NET from 8 to 10, the handler of TreeView.AfterLabelEdit
runs into StackOverflow exception. The code were adapted from MS sample code, I have simplified them, see "Steps to reproduce".
- The first issue is,
NodeLabelEditEventArgs.Label
is not null when Enter is pressed, it doesn't respect the MS API document e.Node.EndEdit(false)
re-triggersTreeView.AfterLabelEdit
, endless loop. see my sample code for details.
Steps to reproduce
using System;
using System.Windows.Forms;
public class TreeViewExample : Form
{
private TreeView treeView;
public TreeViewExample()
{
treeView = new TreeView();
treeView.Dock = DockStyle.Fill;
TreeNode rootNode = new TreeNode("Root");
rootNode.Nodes.Add("Child 1");
rootNode.Nodes.Add("Child 2");
treeView.Nodes.Add(rootNode);
treeView.LabelEdit = true;
treeView.AfterLabelEdit += TreeView_AfterLabelEdit;
this.Controls.Add(treeView);
}
private int recursiveDepth = 0;
// In treeview, select item then click, to begin edit
private void TreeView_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
/*
* NET8:
* Press ESC or Enter or Lose Focus -> e.Label is null
* NET9/10:
* Press ESC -> e.Label is null
* Press Enter or Lose Focus -> e.Label is not null
* Expected:
* The value of this property is null if the user presses ESC to cancel the edit or presses ENTER without modifying the label text.
* https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.nodelabelediteventargs.label?view=windowsdesktop-9.0#remarks
*/
Console.WriteLine(e.Label);
/*
* As a bad result, it runs into endless loop when I use MS sample code in .NET9/10
* https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.treeview.afterlabeledit?view=windowsdesktop-9.0
*/
if (e.Label != null)
{
if (e.Label.Length > 0)
{
recursiveDepth++;
Console.WriteLine(recursiveDepth);
// it calls AfterLabelEdit recursively
e.Node.EndEdit(false);
recursiveDepth--;
}
}
}
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new TreeViewExample());
}
}