博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文本编辑器实例
阅读量:6291 次
发布时间:2019-06-22

本文共 4920 字,大约阅读时间需要 16 分钟。

1
None.gif
using  System;
  2
None.gif
using  System.Collections.Generic;
  3
None.gif
using  System.ComponentModel;
  4
None.gif
using  System.Data;
  5
None.gif
using  System.Drawing;
  6
None.gif
using  System.Text;
  7
None.gif
using  System.Windows.Forms;
  8
None.gif
using  System.IO;
  9
None.gif
using  System.Drawing.Printing;
 10
None.gif
 11
None.gif
namespace  WindowsApplication1
 12
ExpandedBlockStart.gif {
 13
InBlock.gif    
public partial 
class TextEditor : Form
 14
ExpandedSubBlockStart.gif    {
 15
InBlock.gif        
private 
string filename;
 16
InBlock.gif        
public TextEditor(WindowsApplication1.Main parent)
 17
ExpandedSubBlockStart.gif        {
 18
InBlock.gif            
this.MdiParent = parent;
 19
InBlock.gif            InitializeComponent();
 20
ExpandedSubBlockEnd.gif        }
 21
InBlock.gif
 22
InBlock.gif        
private 
void 退出XToolStripMenuItem_Click(
object sender, EventArgs e)
 23
ExpandedSubBlockStart.gif        {
 24
InBlock.gif
 25
InBlock.gif           
if (MessageBox.Show("你确定要退出么?", "退出", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)==DialogResult.Yes )
 26
InBlock.gif               
this.Close();
 27
InBlock.gif
 28
InBlock.gif
 29
ExpandedSubBlockEnd.gif}
 30
InBlock.gif        
 31
InBlock.gif        
private 
void 打开OToolStripMenuItem_Click(
object sender, EventArgs e)
 32
ExpandedSubBlockStart.gif        {
 33
InBlock.gif           
 34
InBlock.gif             openFileDialog1.Title = "打开";
 35
InBlock.gif            
//
openFileDialog1.InitialDirectory=@"d:\program files"; 
//
初始目录,注意不要使用硬编码的目录字符串,可能该目录不存在
 36
InBlock.gif            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
//
打开系统定义的目录
 37
InBlock.gif
 38
InBlock.gif            openFileDialog1.Filter = "文本文件 (*.txt)|*.txt|VB程序文件 (*.vb)|*.vb |所有文件 (*.*)|*.*|C#类型文件 (*.cs;*.cd)|*.cs;*.cd";
 39
InBlock.gif            openFileDialog1.FilterIndex = 3; 
//
默认选择过滤类型 从1开始
 40
InBlock.gif
 41
InBlock.gif            
if (openFileDialog1.ShowDialog() == DialogResult.OK)
 42
ExpandedSubBlockStart.gif            {
 43
InBlock.gif                filename=openFileDialog1.FileName;
 44
InBlock.gif                
//
    listBox1.Items.Add(openFileDialog1.FileName);
 45
InBlock.gif                
//
foreach (string fl in openFileDialog1.FileNames)
 46
InBlock.gif                
//
{
 47
InBlock.gif                
//
    listBox1.Items.Add(fl);
 48
InBlock.gif                
//
}
 49
InBlock.gif
 50
InBlock.gif                OpenFile();
 51
InBlock.gif
 52
InBlock.gif                GetFileName();
 53
InBlock.gif                
 54
ExpandedSubBlockEnd.gif            }
 55
ExpandedSubBlockEnd.gif        }
 56
InBlock.gif
 57
InBlock.gif        
private 
void OpenFile()
 58
ExpandedSubBlockStart.gif        {
 59
InBlock.gif            
try
 60
ExpandedSubBlockStart.gif            {
 61
InBlock.gif                
 62
InBlock.gif                textBox1.Clear();
 63
InBlock.gif                textBox1.Text = File.ReadAllText(filename, Encoding.Default);
 64
InBlock.gif                
//
textBox1.Text = File.ReadAllText(filename);
 65
InBlock.gif                
//
StringBuilder strb = new StringBuilder();
 66
InBlock.gif
 67
InBlock.gif                
//
using (StreamReader sr = new StreamReader(filename))
 68
InBlock.gif                
//
{
 69
InBlock.gif                
//
    while (sr.Peek() >= 0)
 70
InBlock.gif                
//
    {
 71
InBlock.gif                
//
        
//
Console.WriteLine(sr.ReadLine());
 72
InBlock.gif                
//
        strb.Append(sr.ReadLine() );
 73
InBlock.gif                
//
    }
 74
InBlock.gif                
//
}
 75
InBlock.gif                
//
textBox1.Text = strb.ToString();
 76
ExpandedSubBlockEnd.gif            }
 77
InBlock.gif            
catch (IOException ex)
 78
ExpandedSubBlockStart.gif            {
 79
InBlock.gif                MessageBox.Show(ex.Message, "TextEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
 80
ExpandedSubBlockEnd.gif            }
 81
InBlock.gif           
 82
ExpandedSubBlockEnd.gif        }
 83
InBlock.gif
 84
InBlock.gif        
private 
void 保存SToolStripMenuItem_Click(
object sender, EventArgs e)
 85
ExpandedSubBlockStart.gif        {
 86
InBlock.gif            
if (!(filename == 
null))
 87
InBlock.gif                SaveFile();
 88
InBlock.gif            
else
 89
ExpandedSubBlockStart.gif            {
 90
InBlock.gif
 91
InBlock.gif                
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
 92
ExpandedSubBlockStart.gif                {
 93
InBlock.gif                    filename = saveFileDialog1.FileName;
 94
InBlock.gif                    SaveFile();
 95
ExpandedSubBlockEnd.gif                }
 96
ExpandedSubBlockEnd.gif            }
 97
ExpandedSubBlockEnd.gif        }
 98
InBlock.gif
 99
InBlock.gif        
private 
void SaveFile()
100
ExpandedSubBlockStart.gif        {
101
InBlock.gif             
try
102
ExpandedSubBlockStart.gif            {
103
InBlock.gif                File.WriteAllText(filename, textBox1.Text);
104
ExpandedSubBlockEnd.gif            }
105
InBlock.gif            
catch (IOException ex)
106
ExpandedSubBlockStart.gif            {
107
InBlock.gif                MessageBox.Show(ex.Message, "TextEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
108
ExpandedSubBlockEnd.gif            }
109
ExpandedSubBlockEnd.gif        }
110
InBlock.gif
111
InBlock.gif        
private 
void TextEditor_Load(
object sender, EventArgs e)
112
ExpandedSubBlockStart.gif        {
113
InBlock.gif            
//
textBox1.Clear();
114
InBlock.gif            
//
filename = "未命名.txt";
115
ExpandedSubBlockEnd.gif        }
116
InBlock.gif
117
InBlock.gif        
private 
void toolStripMenuItem1_Click(
object sender, EventArgs e)
118
ExpandedSubBlockStart.gif        {
119
InBlock.gif            
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
120
ExpandedSubBlockStart.gif            {
121
InBlock.gif                filename = saveFileDialog1.FileName;
122
InBlock.gif                SaveFile();
123
ExpandedSubBlockEnd.gif            }
124
ExpandedSubBlockEnd.gif        }
125
InBlock.gif
126
InBlock.gif        
private 
void 新建NToolStripMenuItem_Click(
object sender, EventArgs e)
127
ExpandedSubBlockStart.gif        {
128
InBlock.gif           
129
InBlock.gif                    textBox1.Clear();
130
InBlock.gif                    filename = 
null;
131
InBlock.gif                    
this.Text = "TextEditor";
132
InBlock.gif               
133
ExpandedSubBlockEnd.gif        }
134
InBlock.gif
135
InBlock.gif        
private 
void GetFileName()
136
ExpandedSubBlockStart.gif        {
137
InBlock.gif            FileInfo fi = 
new FileInfo(filename);
138
InBlock.gif            
this.Text = fi.Name + " -TextEditor";
139
ExpandedSubBlockEnd.gif        }
140
InBlock.gif
141
InBlock.gif        
private 
string[] lines;
142
InBlock.gif        
private 
int linesPrinted;
143
InBlock.gif
144
InBlock.gif        
private 
void OnPrintPage(
object sender, PrintPageEventArgs e)
145
ExpandedSubBlockStart.gif        {
146
InBlock.gif            
int x = e.MarginBounds.Left ;
147
InBlock.gif            
int y = e.MarginBounds.Top ;
148
InBlock.gif
149
InBlock.gif            
while (linesPrinted < lines.Length)
150
ExpandedSubBlockStart.gif            {
151
InBlock.gif                e.Graphics.DrawString(lines[linesPrinted++], 
new Font("Arial", 10), Brushes.Black, x, y);
152
InBlock.gif                y += 15;
153
InBlock.gif                
if (y >= e.PageBounds.Bottom )
154
ExpandedSubBlockStart.gif                {
155
InBlock.gif                    e.HasMorePages = 
true;
156
InBlock.gif                    
return;
157
ExpandedSubBlockEnd.gif                }
158
ExpandedSubBlockEnd.gif            }
159
InBlock.gif
160
InBlock.gif            linesPrinted = 0;
161
InBlock.gif            e.HasMorePages = 
false;
162
ExpandedSubBlockEnd.gif        }
163
InBlock.gif
164
InBlock.gif     
165
InBlock.gif
166
InBlock.gif        
private 
void 打印PToolStripMenuItem_Click(
object sender, EventArgs e)
167
ExpandedSubBlockStart.gif        {
168
InBlock.gif            
try
169
ExpandedSubBlockStart.gif            {
170
InBlock.gif                
if (printDialog1.ShowDialog() == DialogResult.OK)
171
ExpandedSubBlockStart.gif                {
172
InBlock.gif                    printDocument1.Print();
173
ExpandedSubBlockEnd.gif                }
174
ExpandedSubBlockEnd.gif            }
175
InBlock.gif            
catch (InvalidPrinterException ex)
176
ExpandedSubBlockStart.gif            {
177
InBlock.gif                MessageBox.Show(ex.Message, "Text Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
178
ExpandedSubBlockEnd.gif            }
179
InBlock.gif
180
ExpandedSubBlockEnd.gif        }
181
InBlock.gif
182
InBlock.gif        
private 
void OnBeginPrint(
object sender, PrintEventArgs e)
183
ExpandedSubBlockStart.gif        {
184
ExpandedSubBlockStart.gif            
char[] param ={ '\n' };
185
InBlock.gif            lines = textBox1.Text.Split(param);
186
InBlock.gif            
int i = 0;
187
ExpandedSubBlockStart.gif            
char[] trimParam ={ '\r' };
188
InBlock.gif            
foreach (
string s 
in lines)
189
ExpandedSubBlockStart.gif            {
190
InBlock.gif                lines[i++] = s.TrimEnd(trimParam);
191
ExpandedSubBlockEnd.gif            }
192
ExpandedSubBlockEnd.gif        }
193
InBlock.gif
194
InBlock.gif        
private 
void OnEndPrint(
object sender, PrintEventArgs e)
195
ExpandedSubBlockStart.gif        {
196
InBlock.gif            lines = 
null;
197
ExpandedSubBlockEnd.gif        }
198
InBlock.gif
199
InBlock.gif        
private 
void 页面设置UToolStripMenuItem_Click(
object sender, EventArgs e)
200
ExpandedSubBlockStart.gif        {
201
InBlock.gif            pageSetupDialog1.ShowDialog();
202
ExpandedSubBlockEnd.gif        }
203
InBlock.gif
204
InBlock.gif        
private 
void 打印预览ToolStripMenuItem_Click(
object sender, EventArgs e)
205
ExpandedSubBlockStart.gif        {
206
InBlock.gif            printPreviewDialog1.ShowDialog();
207
InBlock.gif           
208
ExpandedSubBlockEnd.gif        }
209
InBlock.gif       
210
ExpandedSubBlockEnd.gif    }
211
ExpandedBlockEnd.gif}
本文转自tiasys博客园博客,原文链接:http://www.cnblogs.com/tiasys/archive/2006/12/27/605236.html,如需转载请自行联系原作者
你可能感兴趣的文章
LYSE-模块
查看>>
Date Picker和UITool Bar控件简单介绍
查看>>
sql server 实现多表连接查询
查看>>
HTTP 1.1与HTTP 1.0的比较
查看>>
如何在命令行脚本中启动带参数的Windows服务
查看>>
abstract vs interface
查看>>
nodejs笔记1 ----关于express不是本地命令
查看>>
python debug
查看>>
docker-machine 远程安装docker
查看>>
最全的常用正则表达式大全——包括校验数字、字符、一些特殊的需求等等
查看>>
Java Web之Servlet中response、request乱码问题解决
查看>>
felx屏蔽文本框输入回车
查看>>
[转载].NET商业软件源码保护
查看>>
第58件事 借势文案创作实例
查看>>
域名什么意思?
查看>>
ArcEngine创建要素类01
查看>>
Algs4-1.4.43大小可变的数组与链表
查看>>
hdoj 1058 Humble Numbers(dp)
查看>>
Falsk的模板分配和蓝图、定制错误信息、 和补充
查看>>
03:创建容器常用选项
查看>>