文本编辑器实例
  1 using System;
using System;
2 using System.Collections.Generic;
using System.Collections.Generic;
3 using System.ComponentModel;
using System.ComponentModel;
4 using System.Data;
using System.Data;
5 using System.Drawing;
using System.Drawing;
6 using System.Text;
using System.Text;
7 using System.Windows.Forms;
using System.Windows.Forms;
8 using System.IO;
using System.IO;
9 using System.Drawing.Printing;
using System.Drawing.Printing;
10
11 namespace WindowsApplication1
namespace WindowsApplication1
12 {
{
13 public partial class TextEditor : Form
    public partial class TextEditor : Form
14 {
    {
15 private string filename;
        private string filename;
16 public TextEditor(WindowsApplication1.Main parent)
        public TextEditor(WindowsApplication1.Main parent)
17 {
        {
18 this.MdiParent = parent;
            this.MdiParent = parent;
19 InitializeComponent();
            InitializeComponent();
20 }
        }
21
22 private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
23 {
        {
24
25 if (MessageBox.Show("你确定要退出么?", "退出", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)==DialogResult.Yes )
           if (MessageBox.Show("你确定要退出么?", "退出", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)==DialogResult.Yes )
26 this.Close();
               this.Close();
27
28
29 }
}
30 
        
31 private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
32 {
        {
33 
           
34 openFileDialog1.Title = "打开";
             openFileDialog1.Title = "打开";
35 //openFileDialog1.InitialDirectory=@"d:\program files"; //初始目录,注意不要使用硬编码的目录字符串,可能该目录不存在
            //openFileDialog1.InitialDirectory=@"d:\program files"; //初始目录,注意不要使用硬编码的目录字符串,可能该目录不存在
36 openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //打开系统定义的目录
            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //打开系统定义的目录
37
38 openFileDialog1.Filter = "文本文件 (*.txt)|*.txt|VB程序文件 (*.vb)|*.vb |所有文件 (*.*)|*.*|C#类型文件 (*.cs;*.cd)|*.cs;*.cd";
            openFileDialog1.Filter = "文本文件 (*.txt)|*.txt|VB程序文件 (*.vb)|*.vb |所有文件 (*.*)|*.*|C#类型文件 (*.cs;*.cd)|*.cs;*.cd";
39 openFileDialog1.FilterIndex = 3; //默认选择过滤类型 从1开始
            openFileDialog1.FilterIndex = 3; //默认选择过滤类型 从1开始
40
41 if (openFileDialog1.ShowDialog() == DialogResult.OK)
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
42 {
            {
43 filename=openFileDialog1.FileName;
                filename=openFileDialog1.FileName;
44 //    listBox1.Items.Add(openFileDialog1.FileName);
                //    listBox1.Items.Add(openFileDialog1.FileName);
45 //foreach (string fl in openFileDialog1.FileNames)
                //foreach (string fl in openFileDialog1.FileNames)
46 //{
                //{
47 //    listBox1.Items.Add(fl);
                //    listBox1.Items.Add(fl);
48 //}
                //}
49
50 OpenFile();
                OpenFile();
51
52 GetFileName();
                GetFileName();
53 
                
54 }
            }
55 }
        }
56
57 private void OpenFile()
        private void OpenFile()
58 {
        {
59 try
            try
60 {
            {
61 
                
62 textBox1.Clear();
                textBox1.Clear();
63 textBox1.Text = File.ReadAllText(filename, Encoding.Default);
                textBox1.Text = File.ReadAllText(filename, Encoding.Default);
64 //textBox1.Text = File.ReadAllText(filename);
                //textBox1.Text = File.ReadAllText(filename);
65 //StringBuilder strb = new StringBuilder();
                //StringBuilder strb = new StringBuilder();
66
67 //using (StreamReader sr = new StreamReader(filename))
                //using (StreamReader sr = new StreamReader(filename))
68 //{
                //{
69 //    while (sr.Peek() >= 0)
                //    while (sr.Peek() >= 0)
70 //    {
                //    {
71 //        //Console.WriteLine(sr.ReadLine());
                //        //Console.WriteLine(sr.ReadLine());
72 //        strb.Append(sr.ReadLine() );
                //        strb.Append(sr.ReadLine() );
73 //    }
                //    }
74 //}
                //}
75 //textBox1.Text = strb.ToString();
                //textBox1.Text = strb.ToString();
76 }
            }
77 catch (IOException ex)
            catch (IOException ex)
78 {
            {
79 MessageBox.Show(ex.Message, "TextEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                MessageBox.Show(ex.Message, "TextEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
80 }
            }
81 
           
82 }
        }
83
84 private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
85 {
        {
86 if (!(filename == null))
            if (!(filename == null))
87 SaveFile();
                SaveFile();
88 else
            else
89 {
            {
90
91 if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
92 {
                {
93 filename = saveFileDialog1.FileName;
                    filename = saveFileDialog1.FileName;
94 SaveFile();
                    SaveFile();
95 }
                }
96 }
            }
97 }
        }
98
99 private void SaveFile()
        private void SaveFile()
100 {
        {
101 try
             try
102 {
            {
103 File.WriteAllText(filename, textBox1.Text);
                File.WriteAllText(filename, textBox1.Text);
104 }
            }
105 catch (IOException ex)
            catch (IOException ex)
106 {
            {
107 MessageBox.Show(ex.Message, "TextEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                MessageBox.Show(ex.Message, "TextEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
108 }
            }
109 }
        }
110
111 private void TextEditor_Load(object sender, EventArgs e)
        private void TextEditor_Load(object sender, EventArgs e)
112 {
        {
113 //textBox1.Clear();
            //textBox1.Clear();
114 //filename = "未命名.txt";
            //filename = "未命名.txt";
115 }
        }
116
117 private void toolStripMenuItem1_Click(object sender, EventArgs e)
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
118 {
        {
119 if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
120 {
            {
121 filename = saveFileDialog1.FileName;
                filename = saveFileDialog1.FileName;
122 SaveFile();
                SaveFile();
123 }
            }
124 }
        }
125
126 private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
127 {
        {
128 
           
129 textBox1.Clear();
                    textBox1.Clear();
130 filename = null;
                    filename = null;
131 this.Text = "TextEditor";
                    this.Text = "TextEditor";
132 
               
133 }
        }
134
135 private void GetFileName()
        private void GetFileName()
136 {
        {
137 FileInfo fi = new FileInfo(filename);
            FileInfo fi = new FileInfo(filename);
138 this.Text = fi.Name + " -TextEditor";
            this.Text = fi.Name + " -TextEditor";
139 }
        }
140
141 private string[] lines;
        private string[] lines;
142 private int linesPrinted;
        private int linesPrinted;
143
144 private void OnPrintPage(object sender, PrintPageEventArgs e)
        private void OnPrintPage(object sender, PrintPageEventArgs e)
145 {
        {
146 int x = e.MarginBounds.Left ;
            int x = e.MarginBounds.Left ;
147 int y = e.MarginBounds.Top ;
            int y = e.MarginBounds.Top ;
148
149 while (linesPrinted < lines.Length)
            while (linesPrinted < lines.Length)
150 {
            {
151 e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial", 10), Brushes.Black, x, y);
                e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial", 10), Brushes.Black, x, y);
152 y += 15;
                y += 15;
153 if (y >= e.PageBounds.Bottom )
                if (y >= e.PageBounds.Bottom )
154 {
                {
155 e.HasMorePages = true;
                    e.HasMorePages = true;
156 return;
                    return;
157 }
                }
158 }
            }
159
160 linesPrinted = 0;
            linesPrinted = 0;
161 e.HasMorePages = false;
            e.HasMorePages = false;
162 }
        }
163
164 
     
165
166 private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
        private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
167 {
        {
168 try
            try
169 {
            {
170 if (printDialog1.ShowDialog() == DialogResult.OK)
                if (printDialog1.ShowDialog() == DialogResult.OK)
171 {
                {
172 printDocument1.Print();
                    printDocument1.Print();
173 }
                }
174 }
            }
175 catch (InvalidPrinterException ex)
            catch (InvalidPrinterException ex)
176 {
            {
177 MessageBox.Show(ex.Message, "Text Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                MessageBox.Show(ex.Message, "Text Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
178 }
            }
179
180 }
        }
181
182 private void OnBeginPrint(object sender, PrintEventArgs e)
        private void OnBeginPrint(object sender, PrintEventArgs e)
183 {
        {
184 char[] param ={ '\n' };
            char[] param ={ '\n' };
185 lines = textBox1.Text.Split(param);
            lines = textBox1.Text.Split(param);
186 int i = 0;
            int i = 0;
187 char[] trimParam ={ '\r' };
            char[] trimParam ={ '\r' };
188 foreach (string s in lines)
            foreach (string s in lines)
189 {
            {
190 lines[i++] = s.TrimEnd(trimParam);
                lines[i++] = s.TrimEnd(trimParam);
191 }
            }
192 }
        }
193
194 private void OnEndPrint(object sender, PrintEventArgs e)
        private void OnEndPrint(object sender, PrintEventArgs e)
195 {
        {
196 lines = null;
            lines = null;
197 }
        }
198
199 private void 页面设置UToolStripMenuItem_Click(object sender, EventArgs e)
        private void 页面设置UToolStripMenuItem_Click(object sender, EventArgs e)
200 {
        {
201 pageSetupDialog1.ShowDialog();
            pageSetupDialog1.ShowDialog();
202 }
        }
203
204 private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
        private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
205 {
        {
206 printPreviewDialog1.ShowDialog();
            printPreviewDialog1.ShowDialog();
207 
           
208 }
        }
209 
       
210 }
    }
211 }
}
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.ComponentModel;
using System.ComponentModel;4
 using System.Data;
using System.Data;5
 using System.Drawing;
using System.Drawing;6
 using System.Text;
using System.Text;7
 using System.Windows.Forms;
using System.Windows.Forms;8
 using System.IO;
using System.IO;9
 using System.Drawing.Printing;
using System.Drawing.Printing;10

11
 namespace WindowsApplication1
namespace WindowsApplication112
 {
{13
 public partial class TextEditor : Form
    public partial class TextEditor : Form14
 {
    {15
 private string filename;
        private string filename;16
 public TextEditor(WindowsApplication1.Main parent)
        public TextEditor(WindowsApplication1.Main parent)17
 {
        {18
 this.MdiParent = parent;
            this.MdiParent = parent;19
 InitializeComponent();
            InitializeComponent();20
 }
        }21

22
 private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)23
 {
        {24

25
 if (MessageBox.Show("你确定要退出么?", "退出", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)==DialogResult.Yes )
           if (MessageBox.Show("你确定要退出么?", "退出", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)==DialogResult.Yes )26
 this.Close();
               this.Close();27

28

29
 }
}30
 
        31
 private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)32
 {
        {33
 
           34
 openFileDialog1.Title = "打开";
             openFileDialog1.Title = "打开";35
 //openFileDialog1.InitialDirectory=@"d:\program files"; //初始目录,注意不要使用硬编码的目录字符串,可能该目录不存在
            //openFileDialog1.InitialDirectory=@"d:\program files"; //初始目录,注意不要使用硬编码的目录字符串,可能该目录不存在36
 openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //打开系统定义的目录
            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //打开系统定义的目录37

38
 openFileDialog1.Filter = "文本文件 (*.txt)|*.txt|VB程序文件 (*.vb)|*.vb |所有文件 (*.*)|*.*|C#类型文件 (*.cs;*.cd)|*.cs;*.cd";
            openFileDialog1.Filter = "文本文件 (*.txt)|*.txt|VB程序文件 (*.vb)|*.vb |所有文件 (*.*)|*.*|C#类型文件 (*.cs;*.cd)|*.cs;*.cd";39
 openFileDialog1.FilterIndex = 3; //默认选择过滤类型 从1开始
            openFileDialog1.FilterIndex = 3; //默认选择过滤类型 从1开始40

41
 if (openFileDialog1.ShowDialog() == DialogResult.OK)
            if (openFileDialog1.ShowDialog() == DialogResult.OK)42
 {
            {43
 filename=openFileDialog1.FileName;
                filename=openFileDialog1.FileName;44
 //    listBox1.Items.Add(openFileDialog1.FileName);
                //    listBox1.Items.Add(openFileDialog1.FileName);45
 //foreach (string fl in openFileDialog1.FileNames)
                //foreach (string fl in openFileDialog1.FileNames)46
 //{
                //{47
 //    listBox1.Items.Add(fl);
                //    listBox1.Items.Add(fl);48
 //}
                //}49

50
 OpenFile();
                OpenFile();51

52
 GetFileName();
                GetFileName();53
 
                54
 }
            }55
 }
        }56

57
 private void OpenFile()
        private void OpenFile()58
 {
        {59
 try
            try60
 {
            {61
 
                62
 textBox1.Clear();
                textBox1.Clear();63
 textBox1.Text = File.ReadAllText(filename, Encoding.Default);
                textBox1.Text = File.ReadAllText(filename, Encoding.Default);64
 //textBox1.Text = File.ReadAllText(filename);
                //textBox1.Text = File.ReadAllText(filename);65
 //StringBuilder strb = new StringBuilder();
                //StringBuilder strb = new StringBuilder();66

67
 //using (StreamReader sr = new StreamReader(filename))
                //using (StreamReader sr = new StreamReader(filename))68
 //{
                //{69
 //    while (sr.Peek() >= 0)
                //    while (sr.Peek() >= 0)70
 //    {
                //    {71
 //        //Console.WriteLine(sr.ReadLine());
                //        //Console.WriteLine(sr.ReadLine());72
 //        strb.Append(sr.ReadLine() );
                //        strb.Append(sr.ReadLine() );73
 //    }
                //    }74
 //}
                //}75
 //textBox1.Text = strb.ToString();
                //textBox1.Text = strb.ToString();76
 }
            }77
 catch (IOException ex)
            catch (IOException ex)78
 {
            {79
 MessageBox.Show(ex.Message, "TextEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                MessageBox.Show(ex.Message, "TextEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);80
 }
            }81
 
           82
 }
        }83

84
 private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)85
 {
        {86
 if (!(filename == null))
            if (!(filename == null))87
 SaveFile();
                SaveFile();88
 else
            else89
 {
            {90

91
 if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)92
 {
                {93
 filename = saveFileDialog1.FileName;
                    filename = saveFileDialog1.FileName;94
 SaveFile();
                    SaveFile();95
 }
                }96
 }
            }97
 }
        }98

99
 private void SaveFile()
        private void SaveFile()100
 {
        {101
 try
             try102
 {
            {103
 File.WriteAllText(filename, textBox1.Text);
                File.WriteAllText(filename, textBox1.Text);104
 }
            }105
 catch (IOException ex)
            catch (IOException ex)106
 {
            {107
 MessageBox.Show(ex.Message, "TextEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                MessageBox.Show(ex.Message, "TextEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);108
 }
            }109
 }
        }110

111
 private void TextEditor_Load(object sender, EventArgs e)
        private void TextEditor_Load(object sender, EventArgs e)112
 {
        {113
 //textBox1.Clear();
            //textBox1.Clear();114
 //filename = "未命名.txt";
            //filename = "未命名.txt";115
 }
        }116

117
 private void toolStripMenuItem1_Click(object sender, EventArgs e)
        private void toolStripMenuItem1_Click(object sender, EventArgs e)118
 {
        {119
 if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)120
 {
            {121
 filename = saveFileDialog1.FileName;
                filename = saveFileDialog1.FileName;122
 SaveFile();
                SaveFile();123
 }
            }124
 }
        }125

126
 private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)127
 {
        {128
 
           129
 textBox1.Clear();
                    textBox1.Clear();130
 filename = null;
                    filename = null;131
 this.Text = "TextEditor";
                    this.Text = "TextEditor";132
 
               133
 }
        }134

135
 private void GetFileName()
        private void GetFileName()136
 {
        {137
 FileInfo fi = new FileInfo(filename);
            FileInfo fi = new FileInfo(filename);138
 this.Text = fi.Name + " -TextEditor";
            this.Text = fi.Name + " -TextEditor";139
 }
        }140

141
 private string[] lines;
        private string[] lines;142
 private int linesPrinted;
        private int linesPrinted;143

144
 private void OnPrintPage(object sender, PrintPageEventArgs e)
        private void OnPrintPage(object sender, PrintPageEventArgs e)145
 {
        {146
 int x = e.MarginBounds.Left ;
            int x = e.MarginBounds.Left ;147
 int y = e.MarginBounds.Top ;
            int y = e.MarginBounds.Top ;148

149
 while (linesPrinted < lines.Length)
            while (linesPrinted < lines.Length)150
 {
            {151
 e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial", 10), Brushes.Black, x, y);
                e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial", 10), Brushes.Black, x, y);152
 y += 15;
                y += 15;153
 if (y >= e.PageBounds.Bottom )
                if (y >= e.PageBounds.Bottom )154
 {
                {155
 e.HasMorePages = true;
                    e.HasMorePages = true;156
 return;
                    return;157
 }
                }158
 }
            }159

160
 linesPrinted = 0;
            linesPrinted = 0;161
 e.HasMorePages = false;
            e.HasMorePages = false;162
 }
        }163

164
 
     165

166
 private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
        private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)167
 {
        {168
 try
            try169
 {
            {170
 if (printDialog1.ShowDialog() == DialogResult.OK)
                if (printDialog1.ShowDialog() == DialogResult.OK)171
 {
                {172
 printDocument1.Print();
                    printDocument1.Print();173
 }
                }174
 }
            }175
 catch (InvalidPrinterException ex)
            catch (InvalidPrinterException ex)176
 {
            {177
 MessageBox.Show(ex.Message, "Text Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                MessageBox.Show(ex.Message, "Text Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);178
 }
            }179

180
 }
        }181

182
 private void OnBeginPrint(object sender, PrintEventArgs e)
        private void OnBeginPrint(object sender, PrintEventArgs e)183
 {
        {184
 char[] param ={ '\n' };
            char[] param ={ '\n' };185
 lines = textBox1.Text.Split(param);
            lines = textBox1.Text.Split(param);186
 int i = 0;
            int i = 0;187
 char[] trimParam ={ '\r' };
            char[] trimParam ={ '\r' };188
 foreach (string s in lines)
            foreach (string s in lines)189
 {
            {190
 lines[i++] = s.TrimEnd(trimParam);
                lines[i++] = s.TrimEnd(trimParam);191
 }
            }192
 }
        }193

194
 private void OnEndPrint(object sender, PrintEventArgs e)
        private void OnEndPrint(object sender, PrintEventArgs e)195
 {
        {196
 lines = null;
            lines = null;197
 }
        }198

199
 private void 页面设置UToolStripMenuItem_Click(object sender, EventArgs e)
        private void 页面设置UToolStripMenuItem_Click(object sender, EventArgs e)200
 {
        {201
 pageSetupDialog1.ShowDialog();
            pageSetupDialog1.ShowDialog();202
 }
        }203

204
 private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
        private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)205
 {
        {206
 printPreviewDialog1.ShowDialog();
            printPreviewDialog1.ShowDialog();207
 
           208
 }
        }209
 
       210
 }
    }211
 }
} 
                    
                     
                    
                 
                    
                


 
     
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号