EditTxt.cs
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:11k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. //Add by myself
  7. using System.IO;
  8. namespace FileManager
  9. {
  10. /// <summary>
  11. /// EditTxt 的摘要说明。
  12. /// </summary>
  13. public class EditTxt : System.Windows.Forms.Form
  14. {
  15. private System.Windows.Forms.Label label1;
  16. public  System.Windows.Forms.Label lbFullName;
  17. private System.Windows.Forms.MainMenu mainMenu1;
  18. private System.Windows.Forms.RichTextBox txtContent;
  19. private System.Windows.Forms.OpenFileDialog dlgOpenFile;
  20. private System.Windows.Forms.SaveFileDialog dlgSaveFile;
  21. private System.Windows.Forms.MenuItem menuItem1;
  22. private System.Windows.Forms.MenuItem menuItem7;
  23. private System.Windows.Forms.MenuItem OpenMenu;
  24. private System.Windows.Forms.MenuItem NewMenu;
  25. private System.Windows.Forms.MenuItem SaveMenu;
  26. private System.Windows.Forms.MenuItem SaveAsMenu;
  27. private System.Windows.Forms.MenuItem ExitMenu;
  28. private System.Windows.Forms.MenuItem MenuBold;
  29. private System.Windows.Forms.MenuItem MenuItalic;
  30. private System.Windows.Forms.MenuItem MenuUnderline;
  31. /// <summary>
  32. /// 必需的设计器变量。
  33. /// </summary>
  34. private System.ComponentModel.Container components = null;
  35. public EditTxt()
  36. {
  37. //
  38. // Windows 窗体设计器支持所必需的
  39. //
  40. InitializeComponent();
  41. //
  42. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  43. //
  44. }
  45. /// <summary>
  46. /// 清理所有正在使用的资源。
  47. /// </summary>
  48. protected override void Dispose( bool disposing )
  49. {
  50. if( disposing )
  51. {
  52. if(components != null)
  53. {
  54. components.Dispose();
  55. }
  56. }
  57. base.Dispose( disposing );
  58. }
  59. #region Windows 窗体设计器生成的代码
  60. /// <summary>
  61. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  62. /// 此方法的内容。
  63. /// </summary>
  64. private void InitializeComponent()
  65. {
  66. this.label1 = new System.Windows.Forms.Label();
  67. this.lbFullName = new System.Windows.Forms.Label();
  68. this.mainMenu1 = new System.Windows.Forms.MainMenu();
  69. this.menuItem1 = new System.Windows.Forms.MenuItem();
  70. this.OpenMenu = new System.Windows.Forms.MenuItem();
  71. this.NewMenu = new System.Windows.Forms.MenuItem();
  72. this.SaveMenu = new System.Windows.Forms.MenuItem();
  73. this.SaveAsMenu = new System.Windows.Forms.MenuItem();
  74. this.ExitMenu = new System.Windows.Forms.MenuItem();
  75. this.menuItem7 = new System.Windows.Forms.MenuItem();
  76. this.MenuBold = new System.Windows.Forms.MenuItem();
  77. this.MenuItalic = new System.Windows.Forms.MenuItem();
  78. this.MenuUnderline = new System.Windows.Forms.MenuItem();
  79. this.txtContent = new System.Windows.Forms.RichTextBox();
  80. this.dlgOpenFile = new System.Windows.Forms.OpenFileDialog();
  81. this.dlgSaveFile = new System.Windows.Forms.SaveFileDialog();
  82. this.SuspendLayout();
  83. // 
  84. // label1
  85. // 
  86. this.label1.Location = new System.Drawing.Point(24, 40);
  87. this.label1.Name = "label1";
  88. this.label1.Size = new System.Drawing.Size(112, 23);
  89. this.label1.TabIndex = 0;
  90. this.label1.Text = "当前的文本路径:";
  91. // 
  92. // lbFullName
  93. // 
  94. this.lbFullName.Location = new System.Drawing.Point(168, 40);
  95. this.lbFullName.Name = "lbFullName";
  96. this.lbFullName.Size = new System.Drawing.Size(336, 23);
  97. this.lbFullName.TabIndex = 1;
  98. // 
  99. // mainMenu1
  100. // 
  101. this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  102.   this.menuItem1,
  103.   this.menuItem7});
  104. // 
  105. // menuItem1
  106. // 
  107. this.menuItem1.Index = 0;
  108. this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  109.   this.OpenMenu,
  110.   this.NewMenu,
  111.   this.SaveMenu,
  112.   this.SaveAsMenu,
  113.   this.ExitMenu});
  114. this.menuItem1.Text = "编辑";
  115. // 
  116. // OpenMenu
  117. // 
  118. this.OpenMenu.Index = 0;
  119. this.OpenMenu.Text = "打开";
  120. this.OpenMenu.Click += new System.EventHandler(this.OpenMenu_Click);
  121. // 
  122. // NewMenu
  123. // 
  124. this.NewMenu.Index = 1;
  125. this.NewMenu.Text = "新建";
  126. this.NewMenu.Click += new System.EventHandler(this.NewMenu_Click);
  127. // 
  128. // SaveMenu
  129. // 
  130. this.SaveMenu.Index = 2;
  131. this.SaveMenu.Text = "保存";
  132. this.SaveMenu.Click += new System.EventHandler(this.SaveMenu_Click);
  133. // 
  134. // SaveAsMenu
  135. // 
  136. this.SaveAsMenu.Index = 3;
  137. this.SaveAsMenu.Text = "另存为...";
  138. this.SaveAsMenu.Click += new System.EventHandler(this.SaveAsMenu_Click);
  139. // 
  140. // ExitMenu
  141. // 
  142. this.ExitMenu.Index = 4;
  143. this.ExitMenu.Text = "退出";
  144. this.ExitMenu.Click += new System.EventHandler(this.ExitMenu_Click);
  145. // 
  146. // menuItem7
  147. // 
  148. this.menuItem7.Index = 1;
  149. this.menuItem7.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  150.   this.MenuBold,
  151.   this.MenuItalic,
  152.   this.MenuUnderline});
  153. this.menuItem7.Text = "选项";
  154. // 
  155. // MenuBold
  156. // 
  157. this.MenuBold.Index = 0;
  158. this.MenuBold.Text = "Bold";
  159. this.MenuBold.Click += new System.EventHandler(this.MenuBold_Click);
  160. // 
  161. // MenuItalic
  162. // 
  163. this.MenuItalic.Index = 1;
  164. this.MenuItalic.Text = "Italic";
  165. this.MenuItalic.Click += new System.EventHandler(this.MenuItalic_Click);
  166. // 
  167. // MenuUnderline
  168. // 
  169. this.MenuUnderline.Index = 2;
  170. this.MenuUnderline.Text = "Underline";
  171. this.MenuUnderline.Click += new System.EventHandler(this.MenuUnderline_Click);
  172. // 
  173. // txtContent
  174. // 
  175. this.txtContent.Location = new System.Drawing.Point(24, 80);
  176. this.txtContent.Name = "txtContent";
  177. this.txtContent.Size = new System.Drawing.Size(480, 376);
  178. this.txtContent.TabIndex = 2;
  179. this.txtContent.Text = "";
  180. // 
  181. // dlgOpenFile
  182. // 
  183. this.dlgOpenFile.Filter = "Text Document(*.txt)|*.txt";
  184. this.dlgOpenFile.FilterIndex = 2;
  185. // 
  186. // dlgSaveFile
  187. // 
  188. this.dlgSaveFile.Filter = "Text Document(*.txt)|*.txt";
  189. this.dlgSaveFile.FilterIndex = 2;
  190. // 
  191. // EditTxt
  192. // 
  193. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  194. this.ClientSize = new System.Drawing.Size(528, 449);
  195. this.Controls.Add(this.txtContent);
  196. this.Controls.Add(this.lbFullName);
  197. this.Controls.Add(this.label1);
  198. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
  199. this.MaximizeBox = false;
  200. this.Menu = this.mainMenu1;
  201. this.Name = "EditTxt";
  202. this.Text = "EditTxt";
  203. this.Load += new System.EventHandler(this.EditTxt_Load);
  204. this.ResumeLayout(false);
  205. }
  206. #endregion
  207. private void EditTxt_Load(object sender, System.EventArgs e)
  208. {
  209. try {
  210. //Read the text file
  211. FileStream fs= new FileStream(lbFullName.Text,FileMode.Open,FileAccess.Read);
  212. StreamReader reader= new StreamReader(fs);
  213. //Display the file in txtContent
  214. txtContent.Text=reader.ReadToEnd();
  215. fs.Close();
  216. }
  217. catch(Exception ex) {
  218. MessageBox.Show(ex.Message);
  219. }
  220. }
  221. private void OpenMenu_Click(object sender, System.EventArgs e)
  222. {
  223. try {
  224. if(dlgOpenFile.ShowDialog()==DialogResult.OK) {
  225. string fileName=dlgOpenFile.FileName;
  226. FileStream fs= new FileStream(fileName,FileMode.Open,FileAccess.Read);
  227. StreamReader reader= new StreamReader(fs);
  228. lbFullName.Text=fileName;
  229. txtContent.Text=reader.ReadToEnd();
  230. fs.Close();
  231. }
  232. }
  233. catch(Exception ex) {
  234. MessageBox.Show(ex.Message);
  235. }
  236. }
  237. private void NewMenu_Click(object sender, System.EventArgs e)
  238. {
  239. //for test
  240. try {
  241. DialogResult ret=MessageBox.Show("是否保存当前文件?","是否保存",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
  242. if(ret==DialogResult.Yes) 
  243. {
  244. FileStream fs=new FileStream(lbFullName.Text,FileMode.Open,FileAccess.Write);
  245. StreamWriter writer=new StreamWriter(fs);
  246. //write
  247. writer.Write(txtContent.Text);
  248. writer.Flush();
  249. fs.Close();
  250. }
  251. lbFullName.Text="";
  252. this.txtContent.Clear();
  253. }
  254. catch(Exception ex) {
  255. MessageBox.Show(ex.Message);
  256. //test
  257. if(dlgSaveFile.ShowDialog()==DialogResult.OK) 
  258. {
  259.     string fileName=dlgSaveFile.FileName;
  260. Stream stream=File.OpenWrite(fileName);
  261. using(StreamWriter writer=new StreamWriter(stream)) 
  262. {
  263. writer.Write(txtContent.Text);
  264. writer.Flush();
  265. }
  266. MessageBox.Show("文件"+fileName+"保存成功");
  267. lbFullName.Text=fileName;
  268. }
  269. }
  270. }
  271. private void SaveMenu_Click(object sender, System.EventArgs e)
  272. {
  273. try {
  274. FileStream fs= new FileStream(lbFullName.Text,FileMode.Open,FileAccess.Write);
  275. StreamWriter writer=new StreamWriter(fs);
  276. //write into file
  277. writer.Write(txtContent.Text);
  278. writer.Flush();
  279. fs.Close();
  280. MessageBox.Show("文件"+lbFullName.Text+"保存成功");
  281. }
  282. catch(Exception ex) {
  283. MessageBox.Show(ex.Message);
  284. if(dlgSaveFile.ShowDialog()==DialogResult.OK) 
  285. {
  286. string fileName=dlgSaveFile.FileName;
  287. Stream stream=File.OpenWrite(fileName);
  288. using(StreamWriter writer=new StreamWriter(stream)) 
  289. {
  290. writer.Write(txtContent.Text);
  291. writer.Flush();
  292. }
  293. MessageBox.Show("文件"+fileName+"保存成功");
  294. lbFullName.Text=fileName;
  295. }
  296. }
  297. }
  298. private void SaveAsMenu_Click(object sender, System.EventArgs e)
  299. {
  300. if(dlgSaveFile.ShowDialog()==DialogResult.OK) {
  301. string fileName=dlgSaveFile.FileName;
  302. try {
  303. Stream stream=File.OpenWrite(fileName);
  304. using(StreamWriter writer=new StreamWriter(stream)) {
  305. writer.Write(txtContent.Text);
  306. writer.Flush();
  307. }
  308. MessageBox.Show("文件"+fileName+"保存成功");
  309. }
  310. catch(Exception ex) {
  311. MessageBox.Show(ex.Message);
  312. }
  313. }
  314. }
  315. private void ExitMenu_Click(object sender, System.EventArgs e)
  316. {
  317. this.Close();
  318. }
  319. private void MenuBold_Click(object sender, System.EventArgs e)
  320. {
  321. Font newFont= new Font(txtContent.SelectionFont,(txtContent.SelectionFont.Bold?txtContent.SelectionFont.Style&~FontStyle.Bold:txtContent.SelectionFont.Style|FontStyle.Bold));
  322. txtContent.SelectionFont= newFont;
  323. }
  324. private void MenuItalic_Click(object sender, System.EventArgs e)
  325. {
  326. Font newFont= new Font(txtContent.SelectionFont,(txtContent.SelectionFont.Italic?txtContent.SelectionFont.Style&~FontStyle.Italic:txtContent.SelectionFont.Style|FontStyle.Italic));
  327. txtContent.SelectionFont= newFont;
  328. }
  329. private void MenuUnderline_Click(object sender, System.EventArgs e)
  330. {
  331. Font newFont= new Font(txtContent.SelectionFont,(txtContent.SelectionFont.Underline?txtContent.SelectionFont.Style&~FontStyle.Underline:txtContent.SelectionFont.Style|FontStyle.Underline));
  332. txtContent.SelectionFont= newFont;
  333. }
  334. }
  335. }