您的位置:首页 > 其它

第二次作业:WinForm可视化设计--目标一:记事本开发

2015-04-21 19:02 495 查看
 1、作业要求:

       编写一个简单记事本,实现对文本文档文件进行新建,打开,保存,修改等功能。

 2、程序代码:

     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 MyNotePadSolution

{

    public partial class FrmEditor : Form

    {

        private string OriginalContent = "";

        private string _FileName = "";

        public string FileName

        {

            get

            {

                return _FileName;

            }

            set

            {

                _FileName = value;

                Text = Path.GetFileName(value) + "我的记事本";

            }

        }

        public FrmEditor()

        {

            InitializeComponent();

        }

        private void lalTimer_Click(object sender, EventArgs e)

        {

        }

        private void timer1_Tick(object sender, EventArgs e)

        {

            lblTimer.Text = DateTime.Now.ToString();

        }

        private void FrmEditor_Load(object sender, EventArgs e)

        {

            lblTimer.Text = "";

            lblInfo.Text = "";

            Text = "无标题—我的记事本";

        }

        private void btnOpen_Click(object sender, EventArgs e)

        {

            OpenFile();

        }

        private void OpenFile()

        {

            if (openFileDialog1.ShowDialog() == DialogResult.OK)

            {

                FileName = openFileDialog1.FileName;

                try

                {

                    OriginalContent = File.ReadAllText(FileName);

                    txtEditor.Text = OriginalContent;

                }

                catch (Exception)

                {

                    lblInfo.Text = "无法打开文件!";

                }

            }

        }

        private void textEditor_TextChanged(object sender, EventArgs e)

        {

 
4000
      }

        private void btnSave_Click(object sender, EventArgs e)

        {

            SaveFile();

        

        }

        private void SaveFile()

        {

            bool ShouldSave = false;

            if (FileName != "")

            {

                if (txtEditor.Text != OriginalContent && MessageBox.Show("文件已修改,保存吗?", "保存文件", MessageBoxButtons.YesNo) == DialogResult.Yes)

                {

                    ShouldSave = true;

                }

            }

            else

            {

                if (txtEditor.Text != "" && saveFileDialog1.ShowDialog() == DialogResult.OK)

                {

                    FileName = saveFileDialog1.FileName;

                    ShouldSave = true;

                }

            }

            if (ShouldSave)

            {

                try

                {

                    File.WriteAllText(FileName, txtEditor.Text);

                    OriginalContent = txtEditor.Text;

                    lblInfo.Text = "文件已保存";

                }

                catch (Exception)

                {

                    lblInfo.Text = "文件保存失败";

                }

            }

        }

        private void FrmEditor_FormClosing(object sender, FormClosingEventArgs e)

        {

            SaveFile();

        }

    }

}

3、运行结果截图:

4、体会与感想:

   这是第二次作业中最简单的目标一,虽然只是目标一,但我觉得是三个目标最难的一个。比较好的是有金旭亮老师的视频做为基础,所以编写起来方便。通过这次作业,收获如下:

      a、充分理解的“解决方案”和“项目”之间的关系,原来为了方便大程序的编写,一个解决方案下面可以有很多个小项目。

      b、对openFileDialog,saveFileDialog打开文件这个类有了了解,以前认为打开文件好"高大上",经过这次作业后才发现原来.net自带的一个功
              能,希望以后多加运用以熟悉。

      c、对各种窗体,各种控件的一些属性有了更深刻的理解。例如:Anchor,openFileDialog控件的Filter属性······

      d、学习到了编写程序尽量利用重构这一个思想,使程序更简单,更简洁。

       
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: