(C #) comprehensive example of the Treeview Control

Source: Internet
Author: User
 

Use the Treeview control to create a tree. The program has the function of saving text and binary.

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using system. io; using system. text. regularexpressions; using system. runtime. serialization. formatters. binary; // knowledge point: // applications of the Treeview control: add, modify, and delete nodes // get the value of the current node and parent node // select an event for the response node // namespace ch0602 _ Student Score _ binary file _ {public partial class Frmgrade: FORM {public frmgrade () {initializecomponent ();} private void btnaddsc_click (Object sender, eventargs e) {If (cbotype. selectedindex = 0) {// Add a school node and specify it as the current node if (txtnamesc. text. length> 0) {treenode node = tvsc. nodes. add (txtnamesc. text); tvsc. selectednode = node;} else MessageBox. show ("Please specify the school name");} else {If (tvsc. selectednode = NULL | tvsc. selectednode. level> = 1) Message Box. show ("select a school first, and then create a class node"); else {// Add a class node, and expand the school node if (txtnamesc. text. length> 0) {tvsc. selectednode. nodes. add (txtnamesc. text); tvsc. selectednode. expand ();} else MessageBox. show ("Please specify the class name") ;}} private void btnaddstud_click (Object sender, eventargs e) {If (tvsc. selectednode = NULL | tvsc. selectednode. level! = 1) {MessageBox. show ("Please specify the class"); return;} string sname = tvsc. selectednode. parent. text; string cname = tvsc. selectednode. text; Student stud = new student (sname, cname); stud. no = txtno. text; stud. name = txtname. text; stud. sex = cbosex. text; stud. birth = datetime. parse (dtpbirth. text); stud. grade = int. parse (txtgrade. text); stud. ADDR = txtaddr. text; treenode node = tvsc. selectednode. nodes. add (stud. n O); // bind the data node to the added node. tag = stud; // expand the class node tvsc. selectednode. expand ();} private void tvsc_afterselect (Object sender, treevieweventargs e) {// display the content of the current node if (tvsc. selectednode! = NULL & tvsc. selectednode. Level = 2 & tvsc. selectednode. Tag! = NULL) {student stud = (student) tvsc. selectednode. tag; lbostudent. items. add (stud. tostring (); txtno. TEXT = stud. no; txtname. TEXT = stud. name; cbosex. TEXT = stud. sex; dtpbirth. TEXT = stud. birth. tostring (); txtgrade. TEXT = stud. grade. tostring (); txtaddr. TEXT = stud. ADDR ;}} private void tvsc_afterselect (Object sender, treevieweventargs e) {// display the content of the current node if (tvsc. selectednode! = NULL & tvsc. selectednode. Level = 2 & tvsc. selectednode. Tag! = NULL) {student stud = (student) tvsc. selectednode. tag; lbostudent. items. add (stud. tostring (); txtno. TEXT = stud. no; txtname. TEXT = stud. name; cbosex. TEXT = stud. sex; dtpbirth. TEXT = stud. birth. tostring (); txtgrade. TEXT = stud. grade. tostring (); txtaddr. TEXT = stud. ADDR ;}} private void cbotype_selectedindexchanged (Object sender, eventargs e) {txtnamesc. clear ();} private void btnmodstud_click (OB Ject sender, eventargs e) {If (tvsc. selectednode = NULL | tvsc. selectednode. level! = 2) {MessageBox. show ("Please specify student"); return;} string sname = tvsc. selectednode. parent. parent. text; string cname = tvsc. selectednode. parent. text; Student stud = new student (sname, cname); stud. no = txtno. text; stud. name = txtname. text; stud. sex = cbosex. text; stud. birth = datetime. parse (dtpbirth. text); stud. grade = int. parse (txtgrade. text); stud. ADDR = txtaddr. text; // modify the node information tvsc. selectednode. TEXT = s Tud. no; // modify the bound data of the node tvsc. selectednode. tag = stud;} private void btndelstud_click (Object sender, eventargs e) {// Delete the node data tvsc. selectednode. remove () ;}// read the text file and generate the tree structure private void mifileread_click (Object sender, eventargs e) {// display the Open File Dialog Box openfiledialog dlgopen = new openfiledialog (); if (dlgopen. showdialog ()! = Dialogresult. OK) return; // open the file in the specified file and encoding format and prepare to read streamreader sr = new streamreader (dlgopen. filename, encoding. default); tvsc. nodes. clear (); string pattern = @ "\ s *, \ s * | \ s +"; // "\ s" indicates space, "\ s *" indicates zero or multiple spaces, and "\ s +" indicates one or more spaces. String sname0 = "", cname0 = ""; // read a line of text string line; string [] fields; while (line = sr. readline ())! = NULL) {// fields = line. split ('', '\ t'); fields = RegEx. split (line, pattern); string sname = Fields [0]; string cname = Fields [1]; Student stud = new student (sname, cname); stud. no = Fields [2]; stud. name = Fields [3]; stud. sex = Fields [4]; stud. birth = convert. todatetime (fields [5]); stud. grade = int. parse (fields [6]); stud. ADDR = Fields [7]; // if the school name changes, add the school node if (sname! = Sname0) {tvsc. selectednode = tvsc. nodes. Add (sname); sname0 = sname;} // if the class name changes, add the class node if (cname! = Cname0) {// adjust the current node if (tvsc. selectednode. level = 1) tvsc. selectednode = tvsc. selectednode. parent; tvsc. selectednode = tvsc. selectednode. nodes. add (cname); cname0 = cname;} // Add the student node treenode node = tvsc. selectednode. nodes. add (stud. no); node. tag = stud;} // close the file Sr. close () ;}// traverse the tree structure and write the text file private void mifilewrite_click (Object sender, eventargs e) {// display the save file dialog box savefiledialog dlgsave = new SA Vefiledialog (); If (dlgsave. showdialog ()! = Dialogresult. OK) return; // specify the file and encoding format to open the specified file. Prepare to write streamwriter Sw = new streamwriter (dlgsave. filename, false, encoding. default); // traverses the tree structure and writes the writetxtfile (SW, tvsc. nodes); // close the SW file. close () ;}// recursively traverse all nodes in a tree. Private void writetxtfile (streamwriter SW, treenodecollection nodes) {foreach (treenode node in nodes) {If (node. level = 2) {student stud = node. tag as student; string line = string. form At ("{0} \ t {1} \ t {2} \ t {3} \ t {4} \ t {5: yyyy-mm-dd} \ t {6} \ t {7} ", stud. sname, stud. cname, stud. no, stud. name, stud. sex, stud. birth, stud. grade, stud. ADDR); Sw. writeline (line);} If (node. nodes. count> 0) writetxtfile (SW, node. nodes) ;}} private void mifilereadbin_click (Object sender, eventargs e) {openfiledialog dlgopen = new openfiledialog (); If (dlgopen. showdialog ()! = Dialogresult. OK) return; filestream FS = new filestream (dlgopen. filename, filemode. open, fileaccess. read); tvsc. nodes. clear (); // create a read stream Class BR binaryreader BR = new binaryreader (FS); // initialize the variable string sname0 = "", cname0 = ""; // The file Pointer Points to the file header BR. basestream. seek (0, seekorigin. begin); // read cyclically until the file Pointer Points to the end of the file while (BR. basestream. position <br. basestream. length) {// read the student stud = new student (); s by field Tud. sname = BR. readstring (); stud. cname = BR. readstring (); stud. no = BR. readstring (); stud. name = BR. readstring (); stud. sex = BR. readstring (); stud. birth = convert. todatetime (BR. readstring (); stud. grade = BR. readint32 (); stud. ADDR = BR. readstring (); // Add to the Tree node // if the school name changes, add the school node if (stud. sname! = Sname0) {tvsc. selectednode = tvsc. nodes. add (stud. sname); sname0 = stud. sname;} // if the class name changes, add the class node if (stud. cname! = Cname0) {// adjust the current node if (tvsc. selectednode. level = 1) tvsc. selectednode = tvsc. selectednode. parent; tvsc. selectednode = tvsc. selectednode. nodes. add (stud. cname); cname0 = stud. cname;} // Add the student node treenode node = tvsc. selectednode. nodes. add (stud. no); node. tag = stud;} BR. close (); FS. close ();} private void mifilewritebin_click (Object sender, eventargs e) {// display the save file dialog box savefiledialog dlgsave = new Savefiledialog (); If (dlgsave. showdialog ()! = Dialogresult. OK) return; // create a Write File stream FS filestream FS = new filestream (dlgsave. filename, filemode. create, fileaccess. write); // create a write stream class BW binarywriter BW = new binarywriter (FS); // traverse the tree structure and write the text file writebinfile (BW, tvsc. nodes); // close the write stream class and file stream BW. close (); FS. close () ;}// recursively traverse all nodes in a tree. Private void writebinfile (binarywriter BW, treenodecollection nodes) {foreach (treenode node in nodes) {If (node. level = 2) {student stud = node. tag as student; BW. write (stud. sname); BW. write (stud. cname); BW. write (stud. no); BW. write (stud. name); BW. write (stud. sex); BW. write (stud. birth. tostring ("yyyy-mm-dd"); BW. write (stud. grade); BW. write (stud. ADDR);} If (node. nodes. count> 0) writebinfile (BW, node. nodes) ;}} private void mifilereadbin2_click (Object sender, eventargs e) {openfiledialog dlgopen = new openfiled Ialog (); If (dlgopen. showdialog ()! = Dialogresult. OK) return; filestream FS = new filestream (dlgopen. filename, filemode. open, fileaccess. read); binaryreader BR = new binaryreader (FS); tvsc. nodes. clear (); // initialization variable string sname0 = "", cname0 = ""; // The file Pointer Points to the file header FS. seek (0, seekorigin. begin); // read cyclically until the file Pointer Points to the end of the file while (FS. position <FS. length) {// read the records of each student by field int length = BR. readint32 (); byte [] buffer = new byte [length]; buffer = BR. rea Dbytes (length); Student stud = (student) bytestoobject (buffer); // Add to the Tree node // if the school name changes, add the school node if (stud. sname! = Sname0) {tvsc. selectednode = tvsc. nodes. add (stud. sname); sname0 = stud. sname;} // if the class name changes, add the class node if (stud. cname! = Cname0) {// adjust the current node if (tvsc. selectednode. level = 1) tvsc. selectednode = tvsc. selectednode. parent; tvsc. selectednode = tvsc. selectednode. nodes. add (stud. cname); cname0 = stud. cname;} // Add the student node treenode node = tvsc. selectednode. nodes. add (stud. no); node. tag = stud;} BR. close (); FS. close ();} private void mifilewritebin2_click (Object sender, eventargs e) {// display the save file dialog box savefiledialog dlgsave = new Savefiledialog (); If (dlgsave. showdialog ()! = Dialogresult. OK) return; // create a Write File stream FS filestream FS = new filestream (dlgsave. filename, filemode. create, fileaccess. write); binarywriter BW = new binarywriter (FS); // traverses the tree structure and writes the writebinfile2 (BW, tvsc. nodes); // close the write stream class and file stream BW. close (); FS. close ();} private void writebinfile2 (binarywriter BW, treenodecollection nodes) {foreach (treenode node in nodes) {If (node. level = 2) {student stud = node. tag as student; byte [] buffer = objecttobytes (stud); BW. write (buffer. length); BW. write (buffer);} If (node. nodes. count> 0) writebinfile2 (BW, node. nodes) ;}// object serialized as a byte array public static byte [] objecttobytes (Object OBJ) {memorystream MS = new memorystream (); byte [] bytes = NULL; try {// serialize binaryformatter formatter = new binaryformatter (); formatter. serialize (MS, OBJ); bytes = Ms. toarray ();} catch (exception e) {Throw new exception (E. tostring ();} finally {Ms. close ();} return bytes;} // deserialization of the byte array into the object public static object bytestoobject (byte [] bytes) {memorystream MS = new memorystream (bytes ); object OBJ = new object (); try {// deserialization Ms. position = 0; binaryformatter formatter = new binaryformatter (); OBJ = formatter. deserialize (MS);} catch (exception e) {Throw new exception (E. tostring ();} finally {Ms. close ();} return OBJ;} private void miclear_click (Object sender, eventargs e) {tvsc. nodes. clear ();}}}

The following is the definition of a class.

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace ch0602 _ Student Score _ binary file _ {[serializable] public class student {Public String sname {Get; set;} Public String cname {Get; set ;} public String no {Get; set;} public string name {Get; set;} Public String sex {Get; set;} public datetime birth {Get; set ;} public int grade {Get; set;} Public String ADDR {Get; set;} public student () {} public student (string sname, string cname) {sname = sname; cname = cname;} public new string tostring () {return string. format ("{0 }:{ 1}, {2}, {3: yyyy-mm-dd}, {4}, {5}", no, name, sex, birth, grade, ADDR );}}}

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.