zoukankan      html  css  js  c++  java
  • 东秦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; //还原鼠标形状
            }
            //----------------
        }
    }
    

  • 相关阅读:
    vs.net 2005 C# WinForm GroupBOX 的BUG?尝试读取或写入受保护的内存。这通常指示其他内存已损坏
    Git安装及基本使用
    c++实现将表达式转换为逆波兰表达式
    2015年倒数第6周学习报告
    读过的书及读后感
    c++实现队列
    链表插入排序(insertion-sort-list)
    test
    [转]maven入门
    几个学习Maven不错的网址
  • 原文地址:https://www.cnblogs.com/alfredsun/p/4467242.html
Copyright © 2011-2022 走看看