净树形事件处理 [英] .Net Treeview Event Handling

查看:113
本文介绍了净树形事件处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在迁移VB6应用程序到.NET的过程中,我们必须始终提供像对等的功能。在一种形式是复选框有三个层次节点的树视图。第一个层次只是用来组中的一个新的水平了,他们都没有辨认。第二个层次节点是可检查由用户选中或取消选中时,所有的孩子效仿。各级,点击一个节点或它的复选框,将意味着它也选择了,不管检查状态是否受到影响。

I am in the process of migrating a VB6 app to .Net and we must provide like-for-like functionality at all times. In one form is a treeview with checkboxes that has three levels of nodes. The first level serves only to group the next level down and they are not checkable. The second level nodes are checkable by the user and when checked or unchecked all its children follow suit. At all levels, clicking a node or its checkbox will mean it becomes selected, regardless of whether or not the check state is affected.

第三个层次是问题的症结所在(虽然在所有的TreeView复选框的问题本身体现):该级别包含两个类型节点,其中一个可以被选中和未选中的用户(如家长检查)和一种不能被选中或取消选中用户不管父的状态,但它的状态反映了其父。

The third level is the crux of the problem (although the issue itself manifests on all treeview checkboxes): this level contains two 'types' of node, one which can be checked and unchecked by the user (if the parent is checked) and one type which cannot be checked or unchecked by the user regardless of the state of the parent, but its state mirrors that of its parent.

在正常使用中这一切都按预期工作。但是,如果您快速单击第三级节点中的一个(这不应该是直接辨认)的两倍,它似乎改变其检查状态。但是,如果检查Checked属性的基本价值,但它仍然受到影响,所以现在看来​​,这只是一个显示问题。如果发现,这种不正常现象将是一个问题,为我们的客户,因为用户可能会认为他们可以做一些事情,他们不能导致昂贵的混乱。

In normal use this all works as expected. However, if you quickly click one of the third level nodes (which is not supposed to be directly checkable) twice, it appears to change its check state. But if you examine the underlying value of the Checked property, it remains unaffected, so it seems it is simply a display issue. If discovered, this anomaly will be an issue for our clients as users may think they can do something that they cannot leading to expensive confusion.

我新鲜的想法就这一个 - 有其他人观察到这种行为,或知道这件事情以及是否有变通方法/解决方案呢?我不禁感到我已经错过了一些东西真的很明显,但一天半的时间我现在隧道的设想后。下面是一些code来说明问题。创建一个树状形式(大到足以看到发生了什么事情),以及两个按钮,然后放下这:

I am fresh out of ideas on this one - has anyone else observed this behaviour or know about it and are there workarounds/solutions to it? I can't help feeling I've missed something really obvious but after a day and a half I now have tunnel vision. Here's some code to demonstrate the problem. Create a form with a treeview (big enough to see what's going on) and two buttons then drop this in:

私人_node作为树节点=无

Private _node As TreeNode = Nothing

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    MessageBox.Show(_node.Text & " : " & _node.Checked.ToString)
    _node = Nothing

End Sub

Private Sub InitialiseTreeview()

    TreeView1.Nodes.Clear()

    Dim ran As New Random
    Randomize()

    For i As Int32 = 1 To 5
        Dim TLNode As New TreeNode
        Dim children As Int32 = 0

        children = ran.Next(1, 5)

        TLNode.Text = "Top Level Node " & i.ToString

        For j As Int32 = 1 To children
            TLNode.Nodes.Add("Child Node " & j.ToString)
        Next

        TreeView1.Nodes.Add(TLNode)
    Next

    TreeView1.ExpandAll()
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    InitialiseTreeview()
End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    InitialiseTreeview()
End Sub

运行它,然后点击一个节点上一次。点击按钮1,它会告诉你该节点的文本,它的选中状态。现在点击同一个节点的复选框两次,速度快,观察勾选的状态,然后再次单击按钮1。你会明白我的意思。按钮2产生一套新的树节点。

Run it, and click on a node ONCE. Click Button 1 and it will tell you the node text and it's checked state. Now click the same nodes checkbox twice, fast, observe the state of the checkmark and click button 1 again. You'll see what I mean. Button 2 generates a fresh set of tree nodes.

推荐答案

是的,这是Vista版本的本机TreeView控件引入了错误。当它看到了双击事件,它会自动切换该项目的选中状态。没有告诉它在.NET TreeView的包装中,前/ AfterCheck事件将不会运行。这还没有被固定在.NET包装,可能永远也不会。

Yes, this is a bug introduced by the Vista version of the native TreeView control. When it sees the double-click event, it will automatic toggle the check state of the item. Without telling the .NET TreeView wrapper about it, the Before/AfterCheck event won't run. This hasn't been fixed in the .NET wrapper and probably never will.

工作解决此bug需要preventing看见双击消息的本地控制。添加一个新类到您的项目并粘贴下面所示的code。编译。从工具箱顶部的新控件到窗体,以取代现有的TreeView。

Working around this bug requires preventing the native control from seeing the double-click message. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form, replacing the existing TreeView.

Public Class MyTreeView
    Inherits TreeView

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        '' Filter the WM_LBUTTONDBLCLK message
        If m.Msg <> &H203 Then MyBase.WndProc(m)
    End Sub

End Class

这篇关于净树形事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆