Asking a TreeNode if it's CheckBox is visible.

P

Phil Jones

I've been turning of selected checkboxes on a TreeView using the sample code
both Jeffrey and Claes kindly pointed me to on this list.

I'm wondering, is there a way to determine ("ask") a tree-node if it has a
check box or not? The issue is, when setting the check box value
(specifically when performing a bulk 'un-check' operation of the tree) any
change to the CheckBox property on the node causes it to be revealed again.

Thanks!
 
J

Jeffrey Tan[MSFT]

Hi Phil,

I am glad your original problem resolved. Let's focus on this further issue.

In treeview, the checkbox for the treenode is actually a build-in image.
For treenode, there are a serial of build-in images to be displayed as the
state of treenode, the treenode's state image is defined in TVITEM
structure's 12 to 15 bit, and index 0 for no checkbox image, index 1 for
unchecked state checkbox image, index 2 for checked state checkbox image.

So what we should do is send TVM_GETITEM message to the treeview to query
the treenode item state image index. I see that you use VB.net, below is
the code snippet for VB.net:

Const TVIF_STATE As Integer = &H8
Public Const TVIS_STATEIMAGEMASK As Integer = &HF000
Public Const TV_FIRST As Integer = &H1100
Public Const TVM_SETITEM As Integer = TV_FIRST + 63
Public Const TVM_GETITEM As Integer = TV_FIRST + 62
_

Public Structure TVITEM
Public mask As Integer
Public hItem As IntPtr
Public state As Integer
Public stateMask As Integer
<MarshalAs(UnmanagedType.LPTStr)> Public lpszText As [String]
Public cchTextMax As Integer
Public iImage As Integer
Public iSelectedImage As Integer
Public cChildren As Integer
Public lParam As IntPtr
End Structure 'TVITEM


<DllImport("User32.dll", CharSet:=CharSet.Auto,
Entrypoint:="SendMessage")> _
Private Shared Function SendMessage(ByVal hwnd As IntPtr, ByVal wMsg As
Integer, ByVal wparam As IntPtr, ByVal lparam As IntPtr) As IntPtr
End Function

<DllImport("User32.dll", CharSet:=CharSet.Auto,
Entrypoint:="SendMessage")> _
Private Shared Function SendMessage(ByVal hwnd As IntPtr, ByVal
wMsg As Integer, ByVal wparam As IntPtr, ByRef lparam As TVITEM) As IntPtr
End Function

Private Sub button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim tvi As New TVITEM
tvi.hItem = Me.TreeView1.SelectedNode.Handle
tvi.mask = TVIF_STATE
tvi.stateMask = TVIS_STATEIMAGEMASK
tvi.state = 0
tvi.state = tvi.state << 12
Dim lparam As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(tvi))
Marshal.StructureToPtr(tvi, lparam, False)
SendMessage(Me.TreeView1.Handle, TVM_SETITEM, IntPtr.Zero, lparam)
End Sub 'button1_Click


Private Sub button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim tvi As New TVITEM
tvi.hItem = Me.TreeView1.SelectedNode.Handle
tvi.mask = TVIF_STATE
tvi.stateMask = TVIS_STATEIMAGEMASK
tvi.state = 0
SendMessage(Me.TreeView1.Handle, TVM_GETITEM, IntPtr.Zero, tvi)
tvi.state = tvi.state >> 12
If tvi.state = 0 Then
MessageBox.Show("There is no checkbox for current selected
treenode!")
Else
If tvi.state = 1 Then
MessageBox.Show("There is a uncheck state checkbox for
current selected treenode!")
Else
If tvi.state = 2 Then
MessageBox.Show("There is a checked state checkbox for
current selected treenode!")
End If
End If
End If
End Sub 'button2_Click

It works well on my side, for your information.
=============================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
P

Phil Jones

Ahh Jeffrey - that's great! Useful background ideas on what's actually
going on too - which is extremely important to me.

Very much appreciated!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top