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; //还原鼠标形状
            }
            //----------------
        }
    }
    

  • 相关阅读:
    阴影及定位
    选择器高级、样式及布局
    css的导入与基础选择器
    html知识
    ORM
    python实现进度条
    MySQL单表查询
    一、HTTP
    mysql4
    练习——MySQL
  • 原文地址:https://www.cnblogs.com/alfredsun/p/4467242.html
Copyright © 2011-2022 走看看