问题描述
求把treeview保存到txt文本文档之中之后下次运行程序会自动加载txt文本中的节点的代码最好是一粘贴就能用的!!还有就是用c#实现前进和后退功能也求代码谢谢大家了
解决方案
解决方案二:
该回复于2011-12-19 13:58:25被版主删除
解决方案三:
思路别人能给,叫别人给你敲代码貌似很少、
解决方案四:
//我例举一个文本框值保存到记事本中然后下次运行程序自动加载进来值if(File.Exists("../../test.txt"))//保存到记事本中{if(!this.txtZhangHao.AutoCompleteCustomSource.Contains(txtZhangHao.Text))//判断记录是否存在{StreamWritersw=newStreamWriter("../../test.txt",false);//true参数不可少,否则会覆盖以前存入的记录sw.WriteLine(this.txtZhangHao.Text.Trim());//存入记录sw.Close();if(!this.txtZhangHao.AutoCompleteCustomSource.Contains(this.txtZhangHao.Text)){this.txtZhangHao.AutoCompleteCustomSource.Add(this.txtZhangHao.Text);}}this.DialogResult=DialogResult.OK;}//加载load事件中if(File.Exists(@"../../test.txt"))//读取{StreamReadersr=newStreamReader(@"../../test.txt",true);stringstr=sr.ReadLine();if(str!=null){txtZhangHao.Text=str.Trim();}sr.Close();}//txtZhangHao为文本框name可以任意命名
解决方案五:
如果是winform实现前进后退的话可以用WebBrowserwinform里面有一个WebBrowser控件,具有IE的功能WebBrowser.Url//指定将要显示的UrlWebBrowser.GoBack()//后退WebBrowser.GoForward()//前进WebBrowser.GoHome()//访问当前用户主页WebBrowser.Navigate(URL)//跳转到指定的URL
解决方案六:
privatevoidpopulateTreeview(){OpenFileDialogdlg=newOpenFileDialog();dlg.Title="OpenXMLDocument";dlg.Filter="XMLFiles(*.xml)|*.xml";dlg.FileName=Application.StartupPath+"\..\..\example.xml";if(dlg.ShowDialog()==DialogResult.OK){try{//Justagoodpractice--changethecursortoa//waitcursorwhilethenodespopulatethis.Cursor=Cursors.WaitCursor;//First,we'llloadtheXmldocumentXmlDocumentxDoc=newXmlDocument();xDoc.Load(dlg.FileName);//Now,clearoutthetreeview,//andaddthefirst(root)nodetreeView1.Nodes.Clear();treeView1.Nodes.Add(newTreeNode(xDoc.DocumentElement.Name));TreeNodetNode=newTreeNode();tNode=(TreeNode)treeView1.Nodes[0];//WemakeacalltoaddTreeNode,//wherewe'lladdallofournodesaddTreeNode(xDoc.DocumentElement,tNode);//ExpandthetreeviewtoshowallnodestreeView1.ExpandAll();}catch(XmlExceptionxExc)//ExceptionisthrownisthereisanerrorintheXml{MessageBox.Show(xExc.Message);}catch(Exceptionex)//Generalexception{MessageBox.Show(ex.Message);}finally{this.Cursor=Cursors.Default;//Changethecursorback}}}//ThisfunctioniscalledrecursivelyuntilallnodesareloadedprivatevoidaddTreeNode(XmlNodexmlNode,TreeNodetreeNode){XmlNodexNode;TreeNodetNode;XmlNodeListxNodeList;if(xmlNode.HasChildNodes)//Thecurrentnodehaschildren{xNodeList=xmlNode.ChildNodes;for(intx=0;x<=xNodeList.Count-1;x++)//Loopthroughthechildnodes{xNode=xmlNode.ChildNodes[x];treeNode.Nodes.Add(newTreeNode(xNode.Name));tNode=treeNode.Nodes[x];addTreeNode(xNode,tNode);}}else//Nochildren,soaddtheouterxml(trimmingoffwhitespace)treeNode.Text=xmlNode.OuterXml.Trim();}saveprivateStreamWritersr;publicvoidexportToXml(TreeViewtv,stringfilename){sr=newStreamWriter(filename,false,System.Text.Encoding.UTF8);//Writetheheadersr.WriteLine("<?xmlversion="1.0"encoding="utf-8"?>");//Writeourrootnodesr.WriteLine("<"+treeView1.Nodes[0].Text+">");foreach(TreeNodenodeintv.Nodes){saveNode(node.Nodes);}//Closetherootnodesr.WriteLine("</"+treeView1.Nodes[0].Text+">");sr.Close();}privatevoidsaveNode(TreeNodeCollectiontnc){foreach(TreeNodenodeintnc){//Ifwehavechildnodes,we'llwrite//aparentnode,theniterratethrough//thechildrenif(node.Nodes.Count>0){sr.WriteLine("<"+node.Text+">");saveNode(node.Nodes);sr.WriteLine("</"+node.Text+">");}else//Nochildnodes,sowejustwritethetextsr.WriteLine(node.Text);}}