C#每天抄一点(10):多文档程序
12 Oct 2011今天这个小程序花了好几个小时才弄完,现在是晚上11点10分,估计今天的jQuery笔记写不了了,明天补上!
此程序相当于一个记事本,缺点是保存部分我没写好,不能正确保存,时间关系,不研究了,先放上。
不多说,上图,上代码。
001 /*
002 * Created by SharpDevelop.
003 * User: Lazynight
004 * Date: 2011/10/12
005 * Time: 14:03
006 *
007 * To change this template use Tools | Options | Coding | Edit Standard Headers.
008 */
009 using System;
010 using System.Collections.Generic;
011 using System.Drawing;
012 using System.Windows.Forms;
013 using System.ComponentModel;
014 using System.Linq;
015 using System.IO;
016
017 namespace Lazy10_多文档程序
018 {
019
020 public partial class MainForm : Form
021 {
022 Form1 Lazy_Form= new Form1(); //新建 Lazy_Form 对象实例
023 public StringReader Lazy_SReader; //字符串流对象,一行一行读取文本
024 public int x=1; //记录新建子窗口个数
025
026 public MainForm()
027 {
028 InitializeComponent();
029 }
030
031 private void MainFormLoad(object sender, EventArgs e)
032 {
033 }
034
035 private void 新建ToolStripMenuItemClick(object sender, EventArgs e)
036 {
037 Form1 Lazy_Form= new Form1();
038 Lazy_Form.MdiParent = this; //设置此窗体为子窗体
039 Lazy_Form.Text = “新建文件”+x.ToString(); // 设置子窗体标题
040 toolStripStatusLabel1.Text=“状态:新建文档”;//状态栏提示信息
041 x=x+1;
042 Lazy_Form.Show();
043 }
044
045 void 打开ToolStripMenuItemClick(object sender, EventArgs e)
046 {
047 openFileDialog1.Filter=“文本文件(*.txt)|*.txt”; //设置打开对话框的文件类型
048 openFileDialog1.Title=“打开”; //设置打开对话框的标题
049 if(openFileDialog1.ShowDialog()==DialogResult.OK)
050 {
051 string Lazy_strname = openFileDialog1 .FileName; //提取打开文件的文件名
052 Lazy_Form.Text=openFileDialog1.FileName; //设置子窗体名
053 Lazy_Form.richTextBox1.Clear(); //打开文件
054 Lazy_Form.richTextBox1.LoadFile(openFileDialog1.FileName,RichTextBoxStreamType.PlainText);
055 Lazy_Form.MdiParent=this; //设置窗体为子窗体
056 Lazy_Form.Show();
057 toolStripStatusLabel1.Text=“状态:打开文件”;
058 }
059 }
060
061 void 保存ToolStripMenuItemClick(object sender, EventArgs e)
062 {
063 saveFileDialog1.Filter=“文本文件(*.txt)|*.txt”; //设置保存文件类型
064 saveFileDialog1.FilterIndex=2;
065 if(saveFileDialog1.ShowDialog()==DialogResult.OK) //保存文件
066 {
067 FileInfo finfo = new FileInfo(this.saveFileDialog1.FileName);
068 Lazy_Form.richTextBox1.SaveFile(finfo.FullName, RichTextBoxStreamType.RichText);
069 }
070 }
071
072 void 退出ToolStripMenuItemClick(object sender, EventArgs e)
073 {
074 this.Close();
075 Application.Exit();
076 }
077
078 void 剪切ToolStripMenuItemClick(object sender, EventArgs e)
079 {
080 int Lazy_CutInt=Lazy_Form.richTextBox1.SelectionStart;
081 Clipboard.SetDataObject(Lazy_Form.richTextBox1.SelectedText);
082 Lazy_Form.richTextBox1.Text=Lazy_Form.richTextBox1.Text.Substring(,Lazy_CutInt)+Lazy_Form.richTextBox1.Text.Substring(Lazy_CutInt,Lazy_Form.richTextBox1.SelectedText.Length);
083 Lazy_Form.richTextBox1.SelectionStart=Lazy_CutInt;
084 }
085
086 void 复制ToolStripMenuItemClick(object sender, EventArgs e)
087 {
088 Clipboard.SetDataObject(Lazy_Form.richTextBox1.SelectedText);
089 }
090
091 void 粘贴ToolStripMenuItemClick(object sender, EventArgs e)
092 {
093 IDataObject Lazy_idata=Clipboard.GetDataObject();
094 if(Lazy_idata.GetDataPresent(DataFormats.Text))
095 {
096 string PasteStr=(string)Lazy_idata.GetData(DataFormats.Text);
097 int pasted=Lazy_Form.richTextBox1.SelectionStart;
098 Lazy_Form.richTextBox1.Text=Lazy_Form.richTextBox1.Text.Substring(,pasted)+PasteStr+Lazy_Form.richTextBox1.Text.Substring(pasted);
099 Lazy_Form.richTextBox1.SelectionStart=pasted+PasteStr.Length;
100 }
101 }
102
103 void 全选ToolStripMenuItemClick(object sender, EventArgs e)
104 {
105 Lazy_Form.richTextBox1.SelectAll();
106 }
107
108 void 字体ToolStripMenuItemClick(object sender, EventArgs e)
109 {
110 fontDialog1.ShowDialog();
111 fontDialog1.AllowVerticalFonts=true;
112 fontDialog1.FixedPitchOnly=true;
113 fontDialog1.ShowApply=true;
114 fontDialog1.ShowEffects=true;
115 if(Lazy_Form.richTextBox1.SelectedText==“”)
116 {
117 Lazy_Form.richTextBox1.Font=fontDialog1.Font;
118 }
119 else
120 {
121 Lazy_Form.richTextBox1.SelectionFont=fontDialog1.Font;
122 }
123 }
124
125 void 层叠ToolStripMenuItemClick(object sender, EventArgs e)
126 {
127 this.LayoutMdi(MdiLayout.Cascade);
128 }
129
130 void 横向平铺ToolStripMenuItemClick(object sender, EventArgs e)
131 {
132 this.LayoutMdi(MdiLayout.TileHorizontal);
133 }
134
135 void 纵向平铺ToolStripMenuItemClick(object sender, EventArgs e)
136 {
137 this.LayoutMdi(MdiLayout.TileVertical);
138 }
139
140 void 图标排列ToolStripMenuItemClick(object sender, EventArgs e)
141 {
142 this.LayoutMdi(MdiLayout.ArrangeIcons);
143 }
144 }
145 }
002 * Created by SharpDevelop.
003 * User: Lazynight
004 * Date: 2011/10/12
005 * Time: 14:03
006 *
007 * To change this template use Tools | Options | Coding | Edit Standard Headers.
008 */
009 using System;
010 using System.Collections.Generic;
011 using System.Drawing;
012 using System.Windows.Forms;
013 using System.ComponentModel;
014 using System.Linq;
015 using System.IO;
016
017 namespace Lazy10_多文档程序
018 {
019
020 public partial class MainForm : Form
021 {
022 Form1 Lazy_Form= new Form1(); //新建 Lazy_Form 对象实例
023 public StringReader Lazy_SReader; //字符串流对象,一行一行读取文本
024 public int x=1; //记录新建子窗口个数
025
026 public MainForm()
027 {
028 InitializeComponent();
029 }
030
031 private void MainFormLoad(object sender, EventArgs e)
032 {
033 }
034
035 private void 新建ToolStripMenuItemClick(object sender, EventArgs e)
036 {
037 Form1 Lazy_Form= new Form1();
038 Lazy_Form.MdiParent = this; //设置此窗体为子窗体
039 Lazy_Form.Text = “新建文件”+x.ToString(); // 设置子窗体标题
040 toolStripStatusLabel1.Text=“状态:新建文档”;//状态栏提示信息
041 x=x+1;
042 Lazy_Form.Show();
043 }
044
045 void 打开ToolStripMenuItemClick(object sender, EventArgs e)
046 {
047 openFileDialog1.Filter=“文本文件(*.txt)|*.txt”; //设置打开对话框的文件类型
048 openFileDialog1.Title=“打开”; //设置打开对话框的标题
049 if(openFileDialog1.ShowDialog()==DialogResult.OK)
050 {
051 string Lazy_strname = openFileDialog1 .FileName; //提取打开文件的文件名
052 Lazy_Form.Text=openFileDialog1.FileName; //设置子窗体名
053 Lazy_Form.richTextBox1.Clear(); //打开文件
054 Lazy_Form.richTextBox1.LoadFile(openFileDialog1.FileName,RichTextBoxStreamType.PlainText);
055 Lazy_Form.MdiParent=this; //设置窗体为子窗体
056 Lazy_Form.Show();
057 toolStripStatusLabel1.Text=“状态:打开文件”;
058 }
059 }
060
061 void 保存ToolStripMenuItemClick(object sender, EventArgs e)
062 {
063 saveFileDialog1.Filter=“文本文件(*.txt)|*.txt”; //设置保存文件类型
064 saveFileDialog1.FilterIndex=2;
065 if(saveFileDialog1.ShowDialog()==DialogResult.OK) //保存文件
066 {
067 FileInfo finfo = new FileInfo(this.saveFileDialog1.FileName);
068 Lazy_Form.richTextBox1.SaveFile(finfo.FullName, RichTextBoxStreamType.RichText);
069 }
070 }
071
072 void 退出ToolStripMenuItemClick(object sender, EventArgs e)
073 {
074 this.Close();
075 Application.Exit();
076 }
077
078 void 剪切ToolStripMenuItemClick(object sender, EventArgs e)
079 {
080 int Lazy_CutInt=Lazy_Form.richTextBox1.SelectionStart;
081 Clipboard.SetDataObject(Lazy_Form.richTextBox1.SelectedText);
082 Lazy_Form.richTextBox1.Text=Lazy_Form.richTextBox1.Text.Substring(,Lazy_CutInt)+Lazy_Form.richTextBox1.Text.Substring(Lazy_CutInt,Lazy_Form.richTextBox1.SelectedText.Length);
083 Lazy_Form.richTextBox1.SelectionStart=Lazy_CutInt;
084 }
085
086 void 复制ToolStripMenuItemClick(object sender, EventArgs e)
087 {
088 Clipboard.SetDataObject(Lazy_Form.richTextBox1.SelectedText);
089 }
090
091 void 粘贴ToolStripMenuItemClick(object sender, EventArgs e)
092 {
093 IDataObject Lazy_idata=Clipboard.GetDataObject();
094 if(Lazy_idata.GetDataPresent(DataFormats.Text))
095 {
096 string PasteStr=(string)Lazy_idata.GetData(DataFormats.Text);
097 int pasted=Lazy_Form.richTextBox1.SelectionStart;
098 Lazy_Form.richTextBox1.Text=Lazy_Form.richTextBox1.Text.Substring(,pasted)+PasteStr+Lazy_Form.richTextBox1.Text.Substring(pasted);
099 Lazy_Form.richTextBox1.SelectionStart=pasted+PasteStr.Length;
100 }
101 }
102
103 void 全选ToolStripMenuItemClick(object sender, EventArgs e)
104 {
105 Lazy_Form.richTextBox1.SelectAll();
106 }
107
108 void 字体ToolStripMenuItemClick(object sender, EventArgs e)
109 {
110 fontDialog1.ShowDialog();
111 fontDialog1.AllowVerticalFonts=true;
112 fontDialog1.FixedPitchOnly=true;
113 fontDialog1.ShowApply=true;
114 fontDialog1.ShowEffects=true;
115 if(Lazy_Form.richTextBox1.SelectedText==“”)
116 {
117 Lazy_Form.richTextBox1.Font=fontDialog1.Font;
118 }
119 else
120 {
121 Lazy_Form.richTextBox1.SelectionFont=fontDialog1.Font;
122 }
123 }
124
125 void 层叠ToolStripMenuItemClick(object sender, EventArgs e)
126 {
127 this.LayoutMdi(MdiLayout.Cascade);
128 }
129
130 void 横向平铺ToolStripMenuItemClick(object sender, EventArgs e)
131 {
132 this.LayoutMdi(MdiLayout.TileHorizontal);
133 }
134
135 void 纵向平铺ToolStripMenuItemClick(object sender, EventArgs e)
136 {
137 this.LayoutMdi(MdiLayout.TileVertical);
138 }
139
140 void 图标排列ToolStripMenuItemClick(object sender, EventArgs e)
141 {
142 this.LayoutMdi(MdiLayout.ArrangeIcons);
143 }
144 }
145 }
转载请注明:于哲的博客 » C#每天抄一点(10):多文档程序