7月9日的第十天

今天繼續寫下小項目記事本。利用xml文件的讀取和寫入,把用戶最近打開的前五個文件存入xml中,然後每次打開程序的時候,從xml中讀取,顯示在最近打開的文件的菜單項中。利用的是.Net中的XmlDocument這個類。

今天做了一些常用的功能的實現,編輯方面的複製,清除,剪切,粘貼,全選,撤消等,其實這就對應着richTextBox這個文本控件的一些方法,只需要一條代碼就夠了。格式方面的字體設置,顏色設置分別利用到了FontDialog,ColorDialog這些對話框,還算簡單。添加時間,並在狀態欄中實時顯示當前的時間,自動換行,字數統計,行數統計,路徑查看等一些小功能的實現。文本框和文本標題欄右鍵菜單的實現。文件方面的另存爲,保存,退出,打開,新建方面的健全(然後聽到色友在講夢話了,哈哈)剩下的能想到的功能還需要實現的是查找替換,工具欄的實現,快捷鍵的設置,顯示行號,格式化cs文件,顯示當前字符的行和列座標,cs文件的運行等等。再多學學咯。微軟確實爲我們封裝了很多的方法,就連ctrlV,ctrlC都不用自己再開發了,一運行一按,就有了。

明天估計要正式動工了。老師們的數據庫討論得差不多了。也分配了各自的任務,其中我分配到的是學生狀態信息維護和學生異動信息的模塊。表應該不是很複雜。第一感覺就是,老師怎麼不分配那個難點的給我,明顯是看不起我。哈哈,太不行了。老師又不知道你的水有多淺,只能按他眼裏自己的主觀看法來判斷咯。即使簡單,你把它快點做好,而且做得有質量,並且能從中得到水平的提升,這纔是最重要的。水漲船高。你現在是個菜鳥,要學的,就是爲飛做準備。明天好好研究下那數據表,想下如何搭建界面,如何開發。

多多學點知識,有太多的知識點,要去學咯。即使只是在.net這一方面。

下面是那個記事本小項目的文件操作類

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows.Forms;  
  6. using System.Xml;  
  7.  
  8. namespace notePadV1._0  
  9. {  
  10.     public class FileOperator  
  11.     {  
  12.         private string xmlFilePath;  
  13.         private TabControl tabTxt;  
  14.         private ContextMenuStrip rightMenu;  
  15.         //計算新建文件的數目   
  16.         private static int newFileNum = 0;  
  17.         //存儲每個文檔的文本框  
  18.         private List<RichTextBox> rtbList = new List<RichTextBox>();  
  19.         //存儲每個文檔的地址  
  20.         private Dictionary<stringstring> pathDictionary = new Dictionary<stringstring>();  
  21.          
  22.         //查找文件的路徑  
  23.         public  string FindFilePath(string fileName)  
  24.         {  
  25.             try 
  26.             {  
  27.                 return pathDictionary[fileName];  
  28.             }  
  29.             catch 
  30.             {  
  31.                 return null;  
  32.             }  
  33.         }  
  34.         //返回richBox  
  35.         public RichTextBox GetRichBox(int num)  
  36.         {  
  37.             return rtbList[num];  
  38.         }  
  39.           
  40.         public FileOperator(TabControl tab,ContextMenuStrip menu)  
  41.         {  
  42.             tabTxt = tab;  
  43.             rightMenu = menu;  
  44.             xmlFilePath = @"E:\快盤\notePadV1.0\notePadV1.0\bin\Debug\file.xml";  
  45.         }  
  46.         //載入最近打開的文件  
  47.         public void LoadFile(ToolStripMenuItem menuItem)  
  48.         {  
  49.             int loadFileNum = 0;  
  50.             XmlDocument doc = new XmlDocument();  
  51.             try 
  52.             {  
  53.                 doc.Load(xmlFilePath);  
  54.             }  
  55.             catch (Exception ex)  
  56.             {  
  57.  
  58.                 MessageBox.Show("載入xml文件出現錯誤:" + ex.ToString());  
  59.             }  
  60.             XmlNodeList fileList = doc.SelectNodes("/files/file/path");  
  61.             if (fileList != null )  
  62.             {  
  63.                 foreach (XmlNode n in fileList)  
  64.                 {  
  65.                     if (loadFileNum == 5) break;  
  66.                     ToolStripMenuItem t = new ToolStripMenuItem(n.InnerText);  
  67.                     t.Click += new EventHandler(t_Click);  
  68.                     loadFileNum++;  
  69.                     menuItem.DropDownItems.Add(t);  
  70.                 }  
  71.             }  
  72.         }  
  73.         
  74.         //新建文件  
  75.         public void NewFile()  
  76.         {  
  77.             if (tabTxt.TabPages.Count == 0) newFileNum = 0;  
  78.             newFileNum++;  
  79.             TabPage tab1 = new TabPage("file" + newFileNum);  
  80.             RichTextBox rich = new RichTextBox();  
  81.             rich.Dock = DockStyle.Fill;  
  82.             rich.ContextMenuStrip = rightMenu;  
  83.             rich.TextChanged += new EventHandler(this.richTextBox_TextChanged);  
  84.             tab1.Controls.Add(rich);  
  85.             rtbList.Add(rich);  
  86.             tabTxt.Controls.Add(tab1);  
  87.             tabTxt.SelectedTab = tab1;  
  88.         }  
  89.         //打開文件  
  90.         public  void OpenFile(string fileName,string filePath)  
  91.         {  
  92.  
  93.             
  94.                 TabPage tab1 = new TabPage(fileName);  
  95.                 try 
  96.                 {  
  97.                     pathDictionary.Add(fileName, filePath);  
  98.                 }  
  99.                 catch 
  100.                 {  
  101.                     MessageBox.Show("打開的文件已經存在程序中");  
  102.                     return;  
  103.                 }  
  104.  
  105.                 RichTextBox rich = new RichTextBox();  
  106.                 try 
  107.                 {  
  108.                     rich.LoadFile(filePath, RichTextBoxStreamType.PlainText);  
  109.                 }  
  110.                 catch 
  111.                 {  
  112.                     MessageBox.Show("當前文件不存在");  
  113.                     return;  
  114.                 }  
  115.                 rich.Dock = DockStyle.Fill;  
  116.                 rich.TextChanged += new EventHandler(this.richTextBox_TextChanged);  
  117.                 rich.ContextMenuStrip = rightMenu;  
  118.                 rtbList.Add(rich);  
  119.                  
  120.                 tab1.Controls.Add(rich);  
  121.                 tabTxt.Controls.Add(tab1);  
  122.                 //使打開的窗口爲激活狀態  
  123.                 tabTxt.SelectedTab = tab1;  
  124.                 SaveInXml(filePath);  
  125.  
  126.               
  127.         }  
  128.         //將打開的文件保存在xml文件中  
  129.         private void SaveInXml(string filePath)  
  130.         {  
  131.             XmlDocument doc = new XmlDocument();  
  132.             doc.Load(xmlFilePath);  
  133.             XmlElement root = doc.DocumentElement;  
  134.             XmlElement newFile = doc.CreateElement("file");  
  135.             XmlElement newDate = doc.CreateElement("date");  
  136.             XmlElement newPath = doc.CreateElement("path");  
  137.  
  138.             XmlText date = doc.CreateTextNode(DateTime.Now.ToString());  
  139.             XmlText path = doc.CreateTextNode(filePath);  
  140.  
  141.             root.AppendChild(newFile);  
  142.             newFile.AppendChild(newDate);  
  143.             newFile.AppendChild(newPath);  
  144.  
  145.             newDate.AppendChild(date);  
  146.             newPath.AppendChild(path);  
  147.  
  148.             root.InsertBefore(newFile, root.FirstChild);  
  149.             doc.Save(xmlFilePath);  
  150.  
  151.         }  
  152.         //保存文件  
  153.         public void Save()  
  154.         {  
  155.             if (tabTxt.SelectedTab.Text.Contains('*'))  
  156.             {  
  157.                 // tabTxt.SelectedTab.Text = tabTxt.SelectedTab.Text.Remove(tabTxt.SelectedTab.Text.Length - 1);  
  158.                 tabTxt.SelectedTab.Text = RemoveUnSave(tabTxt.SelectedTab);  
  159.             }  
  160.             if (tabTxt.SelectedTab.Text.Contains(".txt") || tabTxt.SelectedTab.Text.Contains(".cs"))  
  161.             {  
  162.                 rtbList[tabTxt.SelectedIndex].SaveFile(pathDictionary[tabTxt.SelectedTab.Text], RichTextBoxStreamType.PlainText);  
  163.                 return;  
  164.             }  
  165.             else 
  166.             {  
  167.                 SaveFileDialog save = new SaveFileDialog();  
  168.                 save.FileName = "file1";  
  169.                 save.Filter = "文本文件(*.txt)|*.txt|CSharp文件(*.cs)|*.cs";  
  170.                 if (save.ShowDialog() == DialogResult.OK)  
  171.                 {  
  172.                     rtbList[tabTxt.SelectedIndex].SaveFile(save.FileName, RichTextBoxStreamType.PlainText);  
  173.                     tabTxt.SelectedTab.Text = save.FileName.Substring(save.FileName.LastIndexOf('\\') + 1);  
  174.                     SaveInXml(save.FileName);  
  175.                 }  
  176.                 pathDictionary.Add(tabTxt.SelectedTab.Text, save.FileName);  
  177.             }  
  178.  
  179.         }  
  180.         //另存爲文件  
  181.         public void SaveAs()  
  182.         {  
  183.             SaveFileDialog save = new SaveFileDialog();  
  184.             save.FileName = "file1";  
  185.             save.Filter = "文本文件(*.txt)|*.txt|CSharp文件(*.cs)|*.cs";  
  186.             if (save.ShowDialog() == DialogResult.OK)  
  187.             {  
  188.                 string filePath = save.FileName;  
  189.                 rtbList[tabTxt.SelectedIndex].SaveFile(filePath, RichTextBoxStreamType.PlainText);  
  190.                 pathDictionary.Remove(tabTxt.SelectedTab.Text);  
  191.                 tabTxt.SelectedTab.Text = save.FileName.Substring(filePath.LastIndexOf('\\') + 1);  
  192.                 SaveInXml(filePath);  
  193.             }  
  194.             
  195.         }  
  196.         //關閉文件  
  197.         public bool CloseFile()  
  198.         {  
  199.             if (tabTxt.TabPages.Count == 0)  
  200.             {  
  201.                 return false;  
  202.             }  
  203.             if (tabTxt.SelectedTab.Text.Contains('*'))  
  204.             {  
  205.                 if (MessageBox.Show("當前文件未保存,是否保存""提示", MessageBoxButtons.YesNo) == DialogResult.Yes)  
  206.                 {  
  207.                     Save();  
  208.                 }  
  209.  
  210.             }  
  211.             pathDictionary.Remove(tabTxt.SelectedTab.Text);  
  212.             rtbList.RemoveAt(tabTxt.SelectedIndex);  
  213.             tabTxt.Controls.Remove(tabTxt.SelectedTab);  
  214.             return true;  
  215.         }  
  216.  
  217.         //當文本發生改變時,發生  
  218.         private void richTextBox_TextChanged(object sender, EventArgs e)  
  219.         {  
  220.             RichTextBox r = sender as RichTextBox;  
  221.             TextChange(r);  
  222.  
  223.  
  224.         }  
  225.         //打開最近的文件列表中的文件  
  226.         private void t_Click(object sender, EventArgs e)  
  227.         {  
  228.             ToolStripMenuItem t = sender as ToolStripMenuItem;  
  229.             OpenFile(t.Text.Substring(t.Text.LastIndexOf('\\') + 1), t.Text);  
  230.  
  231.         }  
  232.         //文本發生改變,顯示未保存方法  
  233.         private  void TextChange(RichTextBox r)  
  234.         {  
  235.             string pageTxt = tabTxt.TabPages[rtbList.IndexOf(r)].Text;  
  236.             if (pageTxt.Contains('*'))  
  237.             {  
  238.                 return;  
  239.             }  
  240.             else 
  241.             {  
  242.                 tabTxt.TabPages[rtbList.IndexOf(r)].Text = pageTxt + "*";  
  243.             }  
  244.         }  
  245.         //移除未保存的符號  
  246.         private string RemoveUnSave(TabPage t)  
  247.         {  
  248.             return t.Text.Remove(t.Text.Length - 1);  
  249.         }  
  250.  
  251.  
  252.     }  

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章