* 程序的版权和版本声明部分
* Copyright (c) 2012, 烟台大学计算机学院学生
* All rights reserved.
* Copyright (c) 2012, 烟台大学计算机学院学生
* All rights reserved.
* 作 者: 刘镇
* 完成日期: 2012 年 11 月 18 日
* 版 本 号: 3.028
* 对任务及求解方法的描述部分
* 问题描述:工具栏上有两个按钮,分别对应“打开文本文件”、“保存文本文件”。菜单和工具栏具体功能实现可参照前面实验内容。
*代码部分:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Win10_7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 打开文本文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.openFile();
}
private void openFile()
{
this.openFileDialog1.Filter = "*.txt|*.txt";
this.openFileDialog1.ShowDialog();
string file = this.openFileDialog1.FileName;
if (string.IsNullOrEmpty(file)) return;
//以下为写字符到文本文件,需要添加System.IO引用
//创建一个文件流
FileStream fs = new FileStream(file, FileMode.Open,
FileAccess.Read);
//创建一个StreamWriter对象
StreamReader sr = new StreamReader(fs);
this.textBox1.Text = sr.ReadToEnd();
//释放StreamWriter对象,文件流对象
sr.Dispose();
fs.Dispose();
}
private void saveFile()
{
this.saveFileDialog1.Filter = "*.txt|*.txt";
this.saveFileDialog1.ShowDialog();
string file = this.saveFileDialog1.FileName;
if (string.IsNullOrEmpty(file)) return;
//以下为写字符到文本文件,需要添加System.IO引用
//创建一个文件流
FileStream fs = new FileStream(file, FileMode.OpenOrCreate,
FileAccess.Write);
//创建一个StreamWriter对象
StreamWriter sw = new StreamWriter(fs);
sw.Write(this.textBox1.Text);
//释放StreamWriter对象,文件流对象
sw.Dispose();
fs.Dispose();
this.textBox1.Clear();
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void 保存文本文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.saveFile();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
this.saveFile();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
this.openFile();
}
private void 打开文本文件(object sender, EventArgs e)
{
}
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
this.fontDialog1.ShowDialog();
this.textBox1.Font = this.fontDialog1.Font;
}
private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 背景颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.colorDialog1.AllowFullOpen = true;
this.colorDialog1.FullOpen = true;
this.colorDialog1.ShowHelp = true;
this.colorDialog1.Color = Color.DarkBlue;
if (this.colorDialog1.ShowDialog() != DialogResult.Cancel)
{
this.BackColor = colorDialog1.Color;
}
}
private void 输入框颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.colorDialog1.AllowFullOpen = true;
this.colorDialog1.FullOpen = true;
this.colorDialog1.ShowHelp = true;
this.colorDialog1.Color = Color.DarkBlue;
if (this.colorDialog1.ShowDialog() != DialogResult.Cancel)
{
this.textBox1.BackColor = colorDialog1.Color;
}
}
}
}
测试结果:
心得经验:
可在此基础上继续开发出有关文本处理的相关操作。