文本编辑器实例

简介:
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 {
 13InBlock.gif    public partial class TextEditor : Form
 14ExpandedSubBlockStart.gif    {
 15InBlock.gif        private string filename;
 16InBlock.gif        public TextEditor(WindowsApplication1.Main parent)
 17ExpandedSubBlockStart.gif        {
 18InBlock.gif            this.MdiParent = parent;
 19InBlock.gif            InitializeComponent();
 20ExpandedSubBlockEnd.gif        }

 21InBlock.gif
 22InBlock.gif        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
 23ExpandedSubBlockStart.gif        {
 24InBlock.gif
 25InBlock.gif           if (MessageBox.Show("你确定要退出么?""退出", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)==DialogResult.Yes )
 26InBlock.gif               this.Close();
 27InBlock.gif
 28InBlock.gif
 29ExpandedSubBlockEnd.gif}

 30InBlock.gif        
 31InBlock.gif        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
 32ExpandedSubBlockStart.gif        {
 33InBlock.gif           
 34InBlock.gif             openFileDialog1.Title = "打开";
 35InBlock.gif            //openFileDialog1.InitialDirectory=@"d:\program files"; //初始目录,注意不要使用硬编码的目录字符串,可能该目录不存在
 36InBlock.gif            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //打开系统定义的目录
 37InBlock.gif
 38InBlock.gif            openFileDialog1.Filter = "文本文件 (*.txt)|*.txt|VB程序文件 (*.vb)|*.vb |所有文件 (*.*)|*.*|C#类型文件 (*.cs;*.cd)|*.cs;*.cd";
 39InBlock.gif            openFileDialog1.FilterIndex = 3//默认选择过滤类型 从1开始
 40InBlock.gif
 41InBlock.gif            if (openFileDialog1.ShowDialog() == DialogResult.OK)
 42ExpandedSubBlockStart.gif            {
 43InBlock.gif                filename=openFileDialog1.FileName;
 44InBlock.gif                //    listBox1.Items.Add(openFileDialog1.FileName);
 45InBlock.gif                //foreach (string fl in openFileDialog1.FileNames)
 46InBlock.gif                //{
 47InBlock.gif                //    listBox1.Items.Add(fl);
 48InBlock.gif                //}
 49InBlock.gif
 50InBlock.gif                OpenFile();
 51InBlock.gif
 52InBlock.gif                GetFileName();
 53InBlock.gif                
 54ExpandedSubBlockEnd.gif            }

 55ExpandedSubBlockEnd.gif        }

 56InBlock.gif
 57InBlock.gif        private void OpenFile()
 58ExpandedSubBlockStart.gif        {
 59InBlock.gif            try
 60ExpandedSubBlockStart.gif            {
 61InBlock.gif                
 62InBlock.gif                textBox1.Clear();
 63InBlock.gif                textBox1.Text = File.ReadAllText(filename, Encoding.Default);
 64InBlock.gif                //textBox1.Text = File.ReadAllText(filename);
 65InBlock.gif                //StringBuilder strb = new StringBuilder();
 66InBlock.gif
 67InBlock.gif                //using (StreamReader sr = new StreamReader(filename))
 68InBlock.gif                //{
 69InBlock.gif                //    while (sr.Peek() >= 0)
 70InBlock.gif                //    {
 71InBlock.gif                //        //Console.WriteLine(sr.ReadLine());
 72InBlock.gif                //        strb.Append(sr.ReadLine() );
 73InBlock.gif                //    }
 74InBlock.gif                //}
 75InBlock.gif                //textBox1.Text = strb.ToString();
 76ExpandedSubBlockEnd.gif            }

 77InBlock.gif            catch (IOException ex)
 78ExpandedSubBlockStart.gif            {
 79InBlock.gif                MessageBox.Show(ex.Message, "TextEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
 80ExpandedSubBlockEnd.gif            }

 81InBlock.gif           
 82ExpandedSubBlockEnd.gif        }

 83InBlock.gif
 84InBlock.gif        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
 85ExpandedSubBlockStart.gif        {
 86InBlock.gif            if (!(filename == null))
 87InBlock.gif                SaveFile();
 88InBlock.gif            else
 89ExpandedSubBlockStart.gif            {
 90InBlock.gif
 91InBlock.gif                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
 92ExpandedSubBlockStart.gif                {
 93InBlock.gif                    filename = saveFileDialog1.FileName;
 94InBlock.gif                    SaveFile();
 95ExpandedSubBlockEnd.gif                }

 96ExpandedSubBlockEnd.gif            }

 97ExpandedSubBlockEnd.gif        }

 98InBlock.gif
 99InBlock.gif        private void SaveFile()
100ExpandedSubBlockStart.gif        {
101InBlock.gif             try
102ExpandedSubBlockStart.gif            {
103InBlock.gif                File.WriteAllText(filename, textBox1.Text);
104ExpandedSubBlockEnd.gif            }

105InBlock.gif            catch (IOException ex)
106ExpandedSubBlockStart.gif            {
107InBlock.gif                MessageBox.Show(ex.Message, "TextEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
108ExpandedSubBlockEnd.gif            }

109ExpandedSubBlockEnd.gif        }

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

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

124ExpandedSubBlockEnd.gif        }

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

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

140InBlock.gif
141InBlock.gif        private string[] lines;
142InBlock.gif        private int linesPrinted;
143InBlock.gif
144InBlock.gif        private void OnPrintPage(object sender, PrintPageEventArgs e)
145ExpandedSubBlockStart.gif        {
146InBlock.gif            int x = e.MarginBounds.Left ;
147InBlock.gif            int y = e.MarginBounds.Top ;
148InBlock.gif
149InBlock.gif            while (linesPrinted < lines.Length)
150ExpandedSubBlockStart.gif            {
151InBlock.gif                e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial"10), Brushes.Black, x, y);
152InBlock.gif                y += 15;
153InBlock.gif                if (y >= e.PageBounds.Bottom )
154ExpandedSubBlockStart.gif                {
155InBlock.gif                    e.HasMorePages = true;
156InBlock.gif                    return;
157ExpandedSubBlockEnd.gif                }

158ExpandedSubBlockEnd.gif            }

159InBlock.gif
160InBlock.gif            linesPrinted = 0;
161InBlock.gif            e.HasMorePages = false;
162ExpandedSubBlockEnd.gif        }

163InBlock.gif
164InBlock.gif     
165InBlock.gif
166InBlock.gif        private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
167ExpandedSubBlockStart.gif        {
168InBlock.gif            try
169ExpandedSubBlockStart.gif            {
170InBlock.gif                if (printDialog1.ShowDialog() == DialogResult.OK)
171ExpandedSubBlockStart.gif                {
172InBlock.gif                    printDocument1.Print();
173ExpandedSubBlockEnd.gif                }

174ExpandedSubBlockEnd.gif            }

175InBlock.gif            catch (InvalidPrinterException ex)
176ExpandedSubBlockStart.gif            {
177InBlock.gif                MessageBox.Show(ex.Message, "Text Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
178ExpandedSubBlockEnd.gif            }

179InBlock.gif
180ExpandedSubBlockEnd.gif        }

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

192ExpandedSubBlockEnd.gif        }

193InBlock.gif
194InBlock.gif        private void OnEndPrint(object sender, PrintEventArgs e)
195ExpandedSubBlockStart.gif        {
196InBlock.gif            lines = null;
197ExpandedSubBlockEnd.gif        }

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

203InBlock.gif
204InBlock.gif        private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
205ExpandedSubBlockStart.gif        {
206InBlock.gif            printPreviewDialog1.ShowDialog();
207InBlock.gif           
208ExpandedSubBlockEnd.gif        }

209InBlock.gif       
210ExpandedSubBlockEnd.gif    }

211ExpandedBlockEnd.gif}
本文转自tiasys博客园博客,原文链接:http://www.cnblogs.com/tiasys/archive/2006/12/27/605236.html,如需转载请自行联系原作者
相关文章
|
Oracle 安全 关系型数据库
实例 centos8 系统通过 snaps 安装markdown编辑器 typora
CentOS8 系统 安装markdown 编辑器 typora
592 0
NotePad++或其他编辑器正则批量替换实例
NotePad++或其他编辑器正则批量替换实例
239 0
|
前端开发 容器
GEF入门实例_总结_06_为编辑器添加内容
一、前言 本文承接上一节:GEF入门实例_总结_05_显示一个空白编辑器 在上一节我们为我们的插件添加了一个空白的编辑器,这一节我们将为此编辑器添加内容。   二、GEF的MVC模式 在此只简单总结一下,后面会详细介绍。
1425 0
GEF入门实例_总结_05_显示一个空白编辑器
一、前言 本文承接上一节:GEF入门实例_总结_04_Eclipse插件启动流程分析 在第三节( GEF入门实例_总结_03_显示菜单和工具栏  ),我们创建了菜单和工具栏。 这一节,我们来实现:点击菜单后,打开一个编辑器。
1481 0
|
定位技术 索引
【Cocosd2d实例教程二】地图编辑器Tiled的安装使用
(转载请注明出处:http://blog.csdn.net/buptgshengod)    我们知道cocos2d是一个基于2d效果的游戏引擎,那么如果制作一个2d手机游戏我们需要创建相应的游戏画面,而cocos2d支持的游戏画面就是通过Tiled来制作的。这是一款通过通过像素来拼凑画面的软件,界面如下图所示 下面讲一下安装以及使用。 第一步:下载Tiled,地址:www.ma
1321 0
|
Web App开发 XML API
超强文本编辑器SciTE配置方法详细实例
关于scite文本编辑器的说明 scite,也就是SCIntilla based Text Editor, 基于SCIntilla编辑组件的文本编辑器。我们见到的许多文本编辑器都是基于SCIntilla编辑组件的。
1729 0
|
2月前
|
开发工具
vi编辑器,现在vi\vim是文本文件进行编辑的最佳选择,Vim是vi的加强的版本,兼容vi的所有指令,vim编辑器有三种工作模式,一开始进入的是命令模式,命令模式i是插入的意思,两下y+p复制内容
vi编辑器,现在vi\vim是文本文件进行编辑的最佳选择,Vim是vi的加强的版本,兼容vi的所有指令,vim编辑器有三种工作模式,一开始进入的是命令模式,命令模式i是插入的意思,两下y+p复制内容
|
3月前
|
开发工具
Vim 编辑器:高效文本编辑的瑞士军刀
**Vim 概览:** Vim 是一个功能丰富的文本编辑器,以其高度可定制性著称。文章介绍了 Vim 的高效使用技巧,包括快捷打开文件、命令行模式下的常用命令、查找与替换、删除和复制文本。还讨论了配置 `.vimrc` 文件以自定义设置,如改变 leader 键、设置缩进和高亮,并展示了安装插件如 vim-airline 和 vim-snazzy 的方法。通过这些技巧,用户能提升 Vim 使用效率。
41 5