views:

25

answers:

1

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.

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.

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.

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:

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

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.

+2  A: 

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.

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
Hans Passant
Thank goodness for that - I thought I was losing my mind. I've implemented your suggested solution and it works absolutely perfectly. Thank you very much for your quick response and the example workaround code. If I had enough reputation I'd vote your answer up, but I'm not quite there yet!!
Steve Pettifer
That should be fixed now.
Hans Passant
Ah, suddenly I do have enough rep - voting now!
Steve Pettifer