C#程序设计(二十八)----菜单与工具栏

* 程序的版权和版本声明部分
* 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;
            }
        }
    }
}


 

测试结果:

 

 

 

心得经验:

 

可在此基础上继续开发出有关文本处理的相关操作。

 

 

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值