C#—实验10.3—工具栏和状态栏

/*
 * (1)在窗体上放置一个菜单、一个工具栏控件。具体操作如名字。
 * (2)statusStrip的操作。
 */
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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text="编辑时间:"+DateTime.Now.ToString();
        }

        private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openfile();
        }

        private void openfile()
        {
            openFileDialog1.InitialDirectory = @"I:\";  //对话框显示的初始目录    
            openFileDialog1.DefaultExt = ".txt";  //获取或设置文件的拓展名    
            openFileDialog1.Filter = "所有文件|*.*|文本文件(*.txt)|*.txt";  //设定筛选器字符串    
            openFileDialog1.FilterIndex = 1; //设置显示的过滤字符串的索引    
            openFileDialog1.Title = "打开文件";   //设置对话框的标题    
            if (openFileDialog1.ShowDialog() == DialogResult.OK)  //显示“打开”对话框    
            {
                StreamReader sr = new StreamReader(openFileDialog1.FileName, Encoding.GetEncoding("gb2312"));
                FileInfo fileinfo = new FileInfo(openFileDialog1.FileName);
                this.Text = fileinfo.Name;        //设置窗体标题  
                richTextBox1.Text = sr.ReadToEnd();
                sr.Close();
            }
        }

        private void 保存文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            savefile();
        }

        private void savefile()
        {
            saveFileDialog1.InitialDirectory = @"I:\";  //对话框显示的初始目录    
            saveFileDialog1.AddExtension = true;  //是否自动在文件中添加拓展名    
            saveFileDialog1.DefaultExt = ".txt";  //获取或设置文件的拓展名    
            saveFileDialog1.Filter = "所有文件|*.*|文本文件(*.txt)|*.txt";  //设定筛选器字符串    
            saveFileDialog1.FilterIndex = 1; //设置显示的过滤字符串的索引    
            saveFileDialog1.Title = "保存文件";   //设置对话框的标题    
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)  //显示“保存”对话框    
            {
                StreamWriter sw = new StreamWriter(saveFileDialog1.FileName, true, Encoding.GetEncoding("gb2312"));
                sw.Write(richTextBox1.Text);
                sw.Close();
                FileInfo fileinfo = new FileInfo(saveFileDialog1.FileName);
                this.Text = fileinfo.Name;        //设置窗体标题  
            }  
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Name)
            {
                case "toolStripLabel1":
                    openfile();
                    break;
                case "toolStripLabel2":
                    savefile();
                    break;
            }
        }

        private void 结束程序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

    }
}

运行结果:


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值