After receiving a new email, outlook and Foxmail will display the number of new emails after the Inbox: inbox (1) . We sometimes need similar functions when doing applications, such as the number of warning messages displayed in alarm management. How can this problem be achieved? After reading the treeveiw and treenode attributes and methods, we did not find any direct implementation. We did not find any on Google or Baidu. However, the Treeview control has a drawnode event, which allows you to draw nodes by yourself and implement the desired function: inbox. (1) . I think other friends may also be thinking about how to implement this function. Share it with me so that you can avoid detours.
Effect:
Implementation Code : Private Void Form1_load ( Object Sender, eventargs E)
{
Treenode Root = New Treenode ( " Myname@163.com " );
Root. Name = " Root " ;
Root. nodes. Add ( " Inbox " , " Inbox " );
Root. nodes. Add ( " Outbox " , " Sender " );
Root. nodes. Add ( " Sentbox " , " Sent Email Addresses " );
Root. nodes. Add ( " Spam " , " Spam " );
Root. nodes. Add ( " Trashbox " , " Waste case box " );
This . Treeview1.nodes. Add (Root );
This . Treeview1.expandall ();
//Set the painting mode to ownerdrawtext:
//Manually draw the label part of the node, and draw the other part of the system.
Treeview1.drawmode=Treeviewdrawmode. ownerdrawtext;
}
Private Void Treeviewmediadrawnode ( Object Sender, drawtreenodeeventargs E)
{
// Drawn by System
E. drawdefault = True ;
// Draw "Number of new mails" behind the node ". For example: inbox (3)
If (E. node. Tag ! = Null )
{
String Newmail = String . Format ( " ({0 }) " , E. node. Tag. tostring ());
E. Graphics. drawstring (newmail, E. node. Treeview. Font, brushes. Blue, E. bounds. Right, E. bounds. Top );
}
}
Private Void Newmailbutton_click ( Object Sender, eventargs E)
{
Treenode inboxnode = This . Treeview1.nodes [ " Root " ]. Nodes [ " Inbox " ];
Inboxnode. Tag = " 3 " ;
This . Treeview1.refresh ();
}
Private Void Readmailbutton_click ( Object Sender, eventargs E)
{
Treenode inboxnode = This . Treeview1.nodes [ " Root " ]. Nodes [ " Inbox " ];
Inboxnode. Tag = Null ;
This . Treeview1.refresh ();
}