今天繼續寫下小項目記事本。利用xml文件的讀取和寫入,把用戶最近打開的前五個文件存入xml中,然後每次打開程序的時候,從xml中讀取,顯示在最近打開的文件的菜單項中。利用的是.Net中的XmlDocument這個類。
今天做了一些常用的功能的實現,編輯方面的複製,清除,剪切,粘貼,全選,撤消等,其實這就對應着richTextBox這個文本控件的一些方法,只需要一條代碼就夠了。格式方面的字體設置,顏色設置分別利用到了FontDialog,ColorDialog這些對話框,還算簡單。添加時間,並在狀態欄中實時顯示當前的時間,自動換行,字數統計,行數統計,路徑查看等一些小功能的實現。文本框和文本標題欄右鍵菜單的實現。文件方面的另存爲,保存,退出,打開,新建方面的健全(然後聽到色友在講夢話了,哈哈)剩下的能想到的功能還需要實現的是查找替換,工具欄的實現,快捷鍵的設置,顯示行號,格式化cs文件,顯示當前字符的行和列座標,cs文件的運行等等。再多學學咯。微軟確實爲我們封裝了很多的方法,就連ctrlV,ctrlC都不用自己再開發了,一運行一按,就有了。
明天估計要正式動工了。老師們的數據庫討論得差不多了。也分配了各自的任務,其中我分配到的是學生狀態信息維護和學生異動信息的模塊。表應該不是很複雜。第一感覺就是,老師怎麼不分配那個難點的給我,明顯是看不起我。哈哈,太不行了。老師又不知道你的水有多淺,只能按他眼裏自己的主觀看法來判斷咯。即使簡單,你把它快點做好,而且做得有質量,並且能從中得到水平的提升,這纔是最重要的。水漲船高。你現在是個菜鳥,要學的,就是爲飛做準備。明天好好研究下那數據表,想下如何搭建界面,如何開發。
多多學點知識,有太多的知識點,要去學咯。即使只是在.net這一方面。
下面是那個記事本小項目的文件操作類
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Xml;
- namespace notePadV1._0
- {
- public class FileOperator
- {
- private string xmlFilePath;
- private TabControl tabTxt;
- private ContextMenuStrip rightMenu;
- //計算新建文件的數目
- private static int newFileNum = 0;
- //存儲每個文檔的文本框
- private List<RichTextBox> rtbList = new List<RichTextBox>();
- //存儲每個文檔的地址
- private Dictionary<string, string> pathDictionary = new Dictionary<string, string>();
- //查找文件的路徑
- public string FindFilePath(string fileName)
- {
- try
- {
- return pathDictionary[fileName];
- }
- catch
- {
- return null;
- }
- }
- //返回richBox
- public RichTextBox GetRichBox(int num)
- {
- return rtbList[num];
- }
- public FileOperator(TabControl tab,ContextMenuStrip menu)
- {
- tabTxt = tab;
- rightMenu = menu;
- xmlFilePath = @"E:\快盤\notePadV1.0\notePadV1.0\bin\Debug\file.xml";
- }
- //載入最近打開的文件
- public void LoadFile(ToolStripMenuItem menuItem)
- {
- int loadFileNum = 0;
- XmlDocument doc = new XmlDocument();
- try
- {
- doc.Load(xmlFilePath);
- }
- catch (Exception ex)
- {
- MessageBox.Show("載入xml文件出現錯誤:" + ex.ToString());
- }
- XmlNodeList fileList = doc.SelectNodes("/files/file/path");
- if (fileList != null )
- {
- foreach (XmlNode n in fileList)
- {
- if (loadFileNum == 5) break;
- ToolStripMenuItem t = new ToolStripMenuItem(n.InnerText);
- t.Click += new EventHandler(t_Click);
- loadFileNum++;
- menuItem.DropDownItems.Add(t);
- }
- }
- }
- //新建文件
- public void NewFile()
- {
- if (tabTxt.TabPages.Count == 0) newFileNum = 0;
- newFileNum++;
- TabPage tab1 = new TabPage("file" + newFileNum);
- RichTextBox rich = new RichTextBox();
- rich.Dock = DockStyle.Fill;
- rich.ContextMenuStrip = rightMenu;
- rich.TextChanged += new EventHandler(this.richTextBox_TextChanged);
- tab1.Controls.Add(rich);
- rtbList.Add(rich);
- tabTxt.Controls.Add(tab1);
- tabTxt.SelectedTab = tab1;
- }
- //打開文件
- public void OpenFile(string fileName,string filePath)
- {
- TabPage tab1 = new TabPage(fileName);
- try
- {
- pathDictionary.Add(fileName, filePath);
- }
- catch
- {
- MessageBox.Show("打開的文件已經存在程序中");
- return;
- }
- RichTextBox rich = new RichTextBox();
- try
- {
- rich.LoadFile(filePath, RichTextBoxStreamType.PlainText);
- }
- catch
- {
- MessageBox.Show("當前文件不存在");
- return;
- }
- rich.Dock = DockStyle.Fill;
- rich.TextChanged += new EventHandler(this.richTextBox_TextChanged);
- rich.ContextMenuStrip = rightMenu;
- rtbList.Add(rich);
- tab1.Controls.Add(rich);
- tabTxt.Controls.Add(tab1);
- //使打開的窗口爲激活狀態
- tabTxt.SelectedTab = tab1;
- SaveInXml(filePath);
- }
- //將打開的文件保存在xml文件中
- private void SaveInXml(string filePath)
- {
- XmlDocument doc = new XmlDocument();
- doc.Load(xmlFilePath);
- XmlElement root = doc.DocumentElement;
- XmlElement newFile = doc.CreateElement("file");
- XmlElement newDate = doc.CreateElement("date");
- XmlElement newPath = doc.CreateElement("path");
- XmlText date = doc.CreateTextNode(DateTime.Now.ToString());
- XmlText path = doc.CreateTextNode(filePath);
- root.AppendChild(newFile);
- newFile.AppendChild(newDate);
- newFile.AppendChild(newPath);
- newDate.AppendChild(date);
- newPath.AppendChild(path);
- root.InsertBefore(newFile, root.FirstChild);
- doc.Save(xmlFilePath);
- }
- //保存文件
- public void Save()
- {
- if (tabTxt.SelectedTab.Text.Contains('*'))
- {
- // tabTxt.SelectedTab.Text = tabTxt.SelectedTab.Text.Remove(tabTxt.SelectedTab.Text.Length - 1);
- tabTxt.SelectedTab.Text = RemoveUnSave(tabTxt.SelectedTab);
- }
- if (tabTxt.SelectedTab.Text.Contains(".txt") || tabTxt.SelectedTab.Text.Contains(".cs"))
- {
- rtbList[tabTxt.SelectedIndex].SaveFile(pathDictionary[tabTxt.SelectedTab.Text], RichTextBoxStreamType.PlainText);
- return;
- }
- else
- {
- SaveFileDialog save = new SaveFileDialog();
- save.FileName = "file1";
- save.Filter = "文本文件(*.txt)|*.txt|CSharp文件(*.cs)|*.cs";
- if (save.ShowDialog() == DialogResult.OK)
- {
- rtbList[tabTxt.SelectedIndex].SaveFile(save.FileName, RichTextBoxStreamType.PlainText);
- tabTxt.SelectedTab.Text = save.FileName.Substring(save.FileName.LastIndexOf('\\') + 1);
- SaveInXml(save.FileName);
- }
- pathDictionary.Add(tabTxt.SelectedTab.Text, save.FileName);
- }
- }
- //另存爲文件
- public void SaveAs()
- {
- SaveFileDialog save = new SaveFileDialog();
- save.FileName = "file1";
- save.Filter = "文本文件(*.txt)|*.txt|CSharp文件(*.cs)|*.cs";
- if (save.ShowDialog() == DialogResult.OK)
- {
- string filePath = save.FileName;
- rtbList[tabTxt.SelectedIndex].SaveFile(filePath, RichTextBoxStreamType.PlainText);
- pathDictionary.Remove(tabTxt.SelectedTab.Text);
- tabTxt.SelectedTab.Text = save.FileName.Substring(filePath.LastIndexOf('\\') + 1);
- SaveInXml(filePath);
- }
- }
- //關閉文件
- public bool CloseFile()
- {
- if (tabTxt.TabPages.Count == 0)
- {
- return false;
- }
- if (tabTxt.SelectedTab.Text.Contains('*'))
- {
- if (MessageBox.Show("當前文件未保存,是否保存", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
- {
- Save();
- }
- }
- pathDictionary.Remove(tabTxt.SelectedTab.Text);
- rtbList.RemoveAt(tabTxt.SelectedIndex);
- tabTxt.Controls.Remove(tabTxt.SelectedTab);
- return true;
- }
- //當文本發生改變時,發生
- private void richTextBox_TextChanged(object sender, EventArgs e)
- {
- RichTextBox r = sender as RichTextBox;
- TextChange(r);
- }
- //打開最近的文件列表中的文件
- private void t_Click(object sender, EventArgs e)
- {
- ToolStripMenuItem t = sender as ToolStripMenuItem;
- OpenFile(t.Text.Substring(t.Text.LastIndexOf('\\') + 1), t.Text);
- }
- //文本發生改變,顯示未保存方法
- private void TextChange(RichTextBox r)
- {
- string pageTxt = tabTxt.TabPages[rtbList.IndexOf(r)].Text;
- if (pageTxt.Contains('*'))
- {
- return;
- }
- else
- {
- tabTxt.TabPages[rtbList.IndexOf(r)].Text = pageTxt + "*";
- }
- }
- //移除未保存的符號
- private string RemoveUnSave(TabPage t)
- {
- return t.Text.Remove(t.Text.Length - 1);
- }
- }
- }