views:

51

answers:

1

Consider the following code:

Dim Working As Boolean = False
Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck
    If Working Then Exit Sub
    Working = True
    e.Node.Checked = Not e.Node.Checked
    Working = False
End Sub

Private Sub TreeView1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseClick
    If e.Button = Windows.Forms.MouseButtons.Right Then MsgBox("Checked = " & TreeView1.SelectedNode.Checked)
End Sub

Where TreeView1 is a TreeView added to the form, with CheckBoxes set to true and one node added. The code basically cancel any node checking occuring on the form.

Single-clicking the top node to check it works well : your click is immediately canceled. Yet if you double-click the checkbox, it will display a tick. But verifying the check state through a right click will yield a Checked = False dialog.

How come? Is it a bug (I'm using the latest .Net Framework 4.0, and he same occurs in 2.0), or am I doing something wrong here? Is there a work around?

EDIT: Additionally, the MouseDoubleClick event is not raised before you click once again.

EDIT 2: Posted a bug report at Microsoft Connect

EDIT 3: See this bug report as well.

+1  A: 

This appears to be a bug to me. The checkbox is clearly checked on the screen, yet the API states that it is not checked.

One question that comes to mind is, what are you trying to accomplish? You've got code that cancels a user's action of checking the checkbox... Why have the checkbox to begin with if you're intention is to not allow it to be checked?

Walter
:). In fact, I've just stripped my code down enough to demonstrate the problem. I've configured my treeview to check/uncheck all child nodes when checking a node, and the same behaviour appears...
CFP
I agree: it's clearly a bug.
Dan Tao
@CFP - understood, I knew there had to be a logical reason for it. :)
Walter
Yup =) I'm developing backup software, and I need pretty precise selections settings, so I have codes that basically looks quite close to what has been posted. And when users believe a folder is checked while it isn't, it can get pretty bad ;)
CFP