You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Framework 1.1中TreeView缺失指定方法及EventArgs的问题咨询

Workarounds for .NET 1.1 TreeView Limitations

Ah, I feel your pain—.NET Framework 1.1’s System.Windows.Forms.TreeView is missing a handful of convenient virtual methods and event args that were added in later versions (specifically .NET 2.0). Let’s walk through workarounds for each of your issues:

Missing OnDrawNode Virtual Method

.NET 1.1 doesn’t expose an OnDrawNode override, but you can still implement custom drawing by leveraging the existing DrawNode event. Here’s how to do this in a custom TreeView subclass:

public class CustomTreeView : TreeView
{
    public CustomTreeView()
    {
        // Enable owner-draw mode first
        DrawMode = TreeViewDrawMode.OwnerDrawText;
        // Wire up the DrawNode event to our handler
        DrawNode += CustomTreeView_DrawNode;
    }

    private void CustomTreeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
    {
        // Add your custom drawing logic here
        // For example, draw the node text in a different color
        if (e.Node.IsSelected)
        {
            e.Graphics.DrawString(e.Node.Text, Font, Brushes.White, e.Bounds);
        }
        else
        {
            e.Graphics.DrawString(e.Node.Text, Font, Brushes.Black, e.Bounds);
        }
        // Or set e.DrawDefault = true to use the default rendering
    }
}

Missing OnNodeMouseClick/OnNodeMouseDoubleClick and TreeNodeMouseClickEventArgs

These events and the TreeNodeMouseClickEventArgs type didn’t exist until .NET 2.0. In 1.1, you’ll need to use the generic mouse events and manually detect which node was clicked with TreeView.HitTest:

Replicating Node Click/Double-Click Behavior

private void treeView1_MouseClick(object sender, MouseEventArgs e)
{
    TreeViewHitTestInfo hitResult = treeView1.HitTest(e.X, e.Y);
    if (hitResult.Node != null)
    {
        // This acts like the NodeMouseClick event in later versions
        HandleNodeClick(hitResult.Node, e.Button);
    }
}

private void treeView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    TreeViewHitTestInfo hitResult = treeView1.HitTest(e.X, e.Y);
    if (hitResult.Node != null)
    {
        // This acts like the NodeMouseDoubleClick event
        HandleNodeDoubleClick(hitResult.Node, e.Button);
    }
}

private void HandleNodeClick(TreeNode node, MouseButtons button)
{
    // Your node click logic here
    MessageBox.Show($"Clicked node: {node.Text} with {button}");
}

private void HandleNodeDoubleClick(TreeNode node, MouseButtons button)
{
    // Your node double-click logic here
    MessageBox.Show($"Double-clicked node: {node.Text} with {button}");
}

Creating a Custom TreeNodeMouseClickEventArgs (Optional)

If you want to mimic the .NET 2.0 event args structure for consistency, you can roll your own:

public class TreeNodeMouseClickEventArgs : EventArgs
{
    public TreeNode Node { get; private set; }
    public MouseButtons Button { get; private set; }
    public int Clicks { get; private set; }
    public int X { get; private set; }
    public int Y { get; private set; }

    public TreeNodeMouseClickEventArgs(TreeNode node, MouseButtons button, int clicks, int x, int y)
    {
        Node = node;
        Button = button;
        Clicks = clicks;
        X = x;
        Y = y;
    }
}

You can then use this in a custom TreeView subclass to raise your own NodeMouseClick/NodeMouseDoubleClick events, just like in newer .NET versions.


If at all possible, upgrading to .NET Framework 2.0 or later would eliminate these workarounds entirely, since all the missing methods and types were added in that release.

内容的提问来源于stack exchange,提问作者Vignesh Nethaji

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:32:31