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

本文介绍如何使用C#语言在Windows Forms应用程序中实现文件的打开与保存功能,包括通过对话框选择文件、读取文件内容并显示,以及将编辑后的内容保存到指定文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * (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();
        }

    }
}

运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值