Question

When I edit the treenode (with long name), it has a background color:

enter image description here

How can I remove the background? How can I change the background color?

I was trying to remove it in OnDrawNode, but it is impossible to do. Next, I tried to use the TVM_GETEDITCONTROL message and send WM_CTLCOLOREDIT, but it doesn't work too:

    [DllImport("user32.dll")]
    internal static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    internal const int WM_CTLCOLOREDIT = 0x0133;
    internal const int TVM_GETEDITCONTROL = 0x110F;

private void NodeTree_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e)
    {
        TreeNode nodeEditing = e.Node;
        IntPtr editControlHandle = SendMessage(NodeTree.Handle, (uint)TVM_GETEDITCONTROL, IntPtr.Zero, IntPtr.Zero);
        if (editControlHandle != IntPtr.Zero)
        {
            SendMessage(editControlHandle, (uint)WM_CTLCOLOREDIT, ColorTranslator.ToWin32(Color.Red), new IntPtr(1));
        }
    }
Was it helpful?

Solution

This background - the background of the selected node. In the event BeforeLabelEdit I unsubscribe from AfterSelect, memorize the selected node and deselect nodes:

private TreeNode SelectedNodeBeforeEdit;

private void NodeTree_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e)
{
    NodeTree.AfterSelect -= NodeTree_AfterSelect;
    SelectedNodeBeforeEdit = NodeTree.SelectedNode;
    NodeTree.SelectedNode = null;
}

In the event AfterLabelEdit I subscribe to AfterSelect and exhibit the selected node:

private void NodeTree_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
    NodeTree.SelectedNode = SelectedNodeBeforeEdit;
    NodeTree.AfterSelect += NodeTree_AfterSelect;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top