Treenode does not have the haschildren attribute, FT!
Write todayProgramEncountered this problem, find the msdn and reference here: http://support.microsoft.com/default.aspx? SCID = kb % 3ben-us % 3b313134. It took two hours to solve this problem.
Code As shown in the following figure (debugging is successful on vs2k5 ):
Using system. runtime. interopservices; Class Treenodehelper
{
Treenode = Null ;
Public Treenodehelper (treenode TR)
{
Treenode=TR;
}
/**/ /// <Summary>
///Returns whether treenode has subnodes.
/// </Summary>
Public Bool Haschildren
{
Get
{
ReturnIstreenodehaschildren (treenode );
}
Set
{
Maketreenodehaschildren (treenode, value );
}
}
Public Const Uint32 TV _first = 4352 ;
Public Const Uint32 tvsil_normal = 0 ;
Public Const Uint32 tvsil_state = 2 ;
Public Const Uint32 tvm_setimagelist = TV _first + 9 ;
Public Const Uint32 tvm_getnextitem = TV _first + 10 ;
Public Const Uint32 tvif_handle = 16 ;
Public Const Uint32 tvif_state = 8 ;
Public Const Uint32 tvis_stateimagemask = 61440 ;
Public Const Uint32 tvm_setitem = TV _first + 13 ;
Public Const Uint32 tvm_getitem = TV _first + 12 ;
Public Const Uint32 tvgn_root = 0 ;
Public Const Int Tvif_children = 64 ;
// Use a sequential structure layout to define tvitem for the Treeview.
[Structlayout (layoutkind. Sequential, pack = 8 , Charset = Charset. Auto)]
Public Struct TV _item
{
Public Uint Mask;
Public Intptr hitem;
Public Uint State;
Public Uint Statemask;
Public Intptr psztext;
Public Int Cchtextmax;
Public Int Iimage;
Public Int Iselectedimage;
Public Int CChildren;
Public Intptr lparam;
}
// Declare two overloaded sendmessage functions.
// Difference is in the last parameter: one is byval and
// Other is byref.
[Dllimport ( " User32.dll " )]
Public Static Extern Uint32 sendmessage (intptr hwnd, uint32 MSG,
Uint32 wparam, uint32 lparam );
[Dllimport ( " USER32 " , Charset = Charset. Auto)]
Public Static Extern Intptr sendmessage (intptr hwnd, uint32 MSG,
Uint32 wparam, Ref TV _item lparam );
/**/ /// <Summary>
/// Specifies whether the treenode has a subnode (if there is a subnode, The treenode will display the + number, and the treenode will not display if there is no subnode)
/// </Summary>
/// <Param name = "TR"> </param>
/// <Param name = "bhaschildren"> </param>
Public Static Void Maketreenodehaschildren (treenode TR, Bool Bhaschildren)
{
TV _item tvitem = New TV _item ();
Tvitem. Mask = Tvif_children | Tvif_handle;
Tvitem. hitem = Tr. Handle;
Tvitem. cChildren = Bhaschildren ? 1 : 0 ;
Sendmessage (tr. Treeview. Handle, tvm_setitem, 0 , Ref Tvitem );
}
/**/ /// <Summary>
/// Returns whether the tree node has a subnode.
/// </Summary>
/// <Param name = "? "> </Param>
/// <Returns> </returns>
Public Static Bool Istreenodehaschildren (treenode TR)
{
If (Tr. nodes. Count > 0 )
{
Return True;
}
TV _item tvitem = New TV _item ();
Tvitem. Mask = Tvif_children | Tvif_handle;
Tvitem. hitem = Tr. Handle;
Sendmessage (tr. Treeview. Handle, tvm_getitem, 0 , Ref Tvitem );
Return Tvitem. cChildren = 1 ;
}
}