Solution for treenode in. Net winform to display + when there are no child nodes

Source: Internet
Author: User
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 ;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.