东秦C#课设002-简单的文本编辑器

//加入的拖拽属性失败,dropenter声明方法待查。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace 实验2_文本编辑器
{
    public partial class Form1 : Form
    {
        public Form1()
            {
                InitializeComponent();
                
            }
        //------------------
       
        //-----------------

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "打开文件";
            ofd.Filter = "文本文件(*.txt)|*.txt"; ofd.ShowDialog();
            String fileName = ofd.FileName; if (File.Exists(fileName))
            {
                FileStream fs = new FileStream(fileName, FileMode.Open);
                StreamReader sr = new StreamReader(fs, Encoding.Default); 
                richTextBox1.Text = sr.ReadToEnd(); 
                sr.Close(); 
                fs.Close();
            } 
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog(); sfd.Title = "保存文件";
            sfd.Filter = "文本文件(*.txt)|*.txt"; sfd.ShowDialog();

            String fileName = sfd.FileName; if (File.Exists(fileName))
            {
                DialogResult result = MessageBox.Show("文件已存在,是否覆盖", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes)
                {
                    FileStream fs = new FileStream(fileName, FileMode.Create); StreamWriter sw = new StreamWriter(fs, Encoding.Default); sw.WriteLine(richTextBox1.Text); sw.Close(); fs.Close();
                }
            }
            else
            {
                FileStream fs = new FileStream(fileName, FileMode.Create);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                sw.WriteLine(richTextBox1.Text); 
                sw.Close(); 
                fs.Close();
            } 
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        //----------------
        private void richTextBox1_DragEnter(object sender, EventArgs e)
        {
            if (((DragEventArgs)e).Data.GetDataPresent(DataFormats.FileDrop))
            {
                ((DragEventArgs)e).Effect = DragDropEffects.Link;
                this.richTextBox1.Cursor = System.Windows.Forms.Cursors.Arrow;  //指定鼠标形状(更好看)
            }
            else
            {
                ((DragEventArgs)e).Effect = DragDropEffects.None;
            }


        }

        private void textBox1_DragDrop(object sender, DragEventArgs e)
        {
            //GetValue(0) 为第1个文件全路径
             //DataFormats 数据的格式,下有多个静态属性都为string型,除FileDrop格式外还有Bitmap,Text,WaveAudio等格式
            string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            richTextBox1.Text = path;
            this.richTextBox1.Cursor = System.Windows.Forms.Cursors.IBeam; //还原鼠标形状
        }
        //----------------
    }
}

posted @ 2015-04-14 14:58  夜歌乘年少  阅读(309)  评论(0编辑  收藏  举报