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.Data.SqlClient;
using System.IO;

namespace winformDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //让textBox2隐藏
            this.textBox2.Visible = false;
            //让dataGridView1表中的最后一行空值隐藏掉
            this.dataGridView1.AllowUserToAddRows = false;
        }
        SqlConnection con = new SqlConnection();
        SqlCommand com = new SqlCommand();
        OpenFileDialog open = new OpenFileDialog();
        /// <summary>
        /// 行
        /// </summary>
        string ClickRow = "";
        /// <summary>
        /// 列
        /// </summary>
        string ClickCells = "";
        /// <summary>
        /// 行和列相加的字符串
        /// </summary>

string SqlLanding = "server=.;uid=sa;pwd=123456789;database=myfirstDemo";
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //获取正在点击的行和列。
            ClickRow = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            ClickCells = this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
        }

private void Form1_Load(object sender, EventArgs e)
        {
            SelectInfo();
        }
        public void SelectInfo()
        {
            //断开式链接查看数据库数据
            con.ConnectionString = SqlLanding;
            com.CommandText = "select Name as 文件名,TxtLuJing as 文件路径 from TxtBianJiQi";
            com.Connection = con;
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(com);
            sda.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0];
        }
        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string Filepath = ClickCells + ClickRow;
            this.textBox2.Visible = true;
            try
            {
                //只读流;
                FileStream fss = new FileStream(Filepath, FileMode.OpenOrCreate, FileAccess.Read);
                StreamReader sww = new StreamReader(fss, Encoding.Default);
                textBox2.Text = sww.ReadToEnd();
                sww.Close();
                fss.Close();
            }
            catch (Exception ex)
            {
                //如果没有选择路径提示出一句话;
                MessageBox.Show("查看路径错误:" + ex.Message);
            }
        }

private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string Filepath = ClickCells + ClickRow;
            try
            {
                //只写流;
                FileStream fss = new FileStream(Filepath, FileMode.Create, FileAccess.Write);
                StreamWriter sww = new StreamWriter(fss, Encoding.Default);
                sww.Write(textBox2.Text);
                sww.Close();
                fss.Close();
                MessageBox.Show("保存成功!");
            }
            catch (Exception ex)
            {
                //如果没有选择路径提示出一句话;
                MessageBox.Show("保存路径错误:" + ex.Message);
            }
            this.textBox2.Visible = false;
        }

private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.textBox2.Text = "";
            string localFilePath = "";
            string fileNameExt = "";
            string flie = "";
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            //打开默认的文件目录
            saveFileDialog.InitialDirectory = "D:\\\\Text\\";
            //文件后缀名
            saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            saveFileDialog.FilterIndex = 2;
            string LuJing = saveFileDialog.InitialDirectory;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                flie = saveFileDialog.FileName;
                //文件目录名
                localFilePath = saveFileDialog.FileName.ToString();
                //截取文件名字
                fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);
            }
            string sql = "select name from TxtBianJiQi";
            SqlCommand co = new SqlCommand(sql, con);
            SqlDataAdapter da = new SqlDataAdapter(co);
            DataSet dss = new DataSet();
            da.Fill(dss);
            //循环判断传入的表中name
            for (int i = 0; i < dss.Tables[0].Rows.Count; i++)
            {
                //定一个变量去接获取出来name
                string ss = dss.Tables[0].Rows[i][0].ToString();
                //判断对话框里输入的值是否与查出来的name相同
                if (fileNameExt == ss)
                {
                    MessageBox.Show("文件已更改!");
                    return;
                }
            }
            try
            {
                //只写流
                FileStream fs = new FileStream(flie, FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);//对话框另存为。
                sw.Write(textBox2.Text);
                sw.Flush();
                fs.Close();
                con.ConnectionString = SqlLanding;
                //往数据库添加 文件名和路径名 sql语句
                com.CommandText = String.Format("insert into TxtBianJiQi(Name,TxtLuJing)values('{0}','{1}')", fileNameExt, LuJing);
                com.Connection = con;
                con.Open();
                int insertInto = Convert.ToInt32(com.ExecuteScalar());
                if (insertInto > 0)
                {
                    MessageBox.Show("操作失败!请重试。");
                }
                else
                {
                    MessageBox.Show("添加成功!");
                    this.textBox2.Visible = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加日志失败:" + ex.Message);
            }
            con.Close();
            SelectInfo();
        }

private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            con.ConnectionString = SqlLanding;
            //从数据库删除正在点击的文件名
            com.CommandText = String.Format("delete from TxtBianJiQi where Name='{0}'", ClickRow);
            com.Connection = con;
            con.Open();
            DialogResult dr = MessageBox.Show("确认删除?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (dr == DialogResult.OK)
            {
                int insertInto = Convert.ToInt32(com.ExecuteScalar());
                if (insertInto > 0)
                {
                    MessageBox.Show("操作失误!!");
                }
                else
                {
                    //File.Delete(ClickCells + ClickRow);删除Windows里的文件,括号里是要删除文档的路径。
                    File.Delete(ClickCells + ClickRow);
                    MessageBox.Show("删除成功!");
                }
            }
            con.Close();
            SelectInfo();
        }

private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

}
}
就是写了一个挺简单的在winform里进行填写文本,里面用到的ADO.NET来链接数据库,在新建文本的时候需要写入.txt后缀名,打开或者是删除的时候需要先点击一下文本名。 写的不足请见谅!

简单的winform编辑器的更多相关文章

  1. SimpleMarkdown - 一款简单的Markdown编辑器

    源码地址: https://github.com/zhuangZhou/SimpleMarkdown 预览地址: http://hawkzz.com:8000 作者网站:http://hawkzz.c ...

  2. python使用wxPython创建一个简单的文本编辑器。

    ubuntu下通过'sudo apt-get install python-wxtools'下载wxPython.load和save函数用于加载和保存文件内容,button通过Bind函数绑定这两个函 ...

  3. 原生JS实现简单富文本编辑器2

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 原生JS实现简单富文本编辑器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. [原创]用python写了一个简单的markdown编辑器

    以前我常用openoffice记录东西,最喜欢它的当然是在linux上能用了,还有里面的公式输入,前几天才了解markdown这个东东,初步了解发现它正是我需要的东西,可以用它随心所欲地记录些东西,而 ...

  6. 学习vi和vim编辑(3):一个简单的文本编辑器(2)

    然后文章,继续评论vi编辑简单的文本编辑命令. 本文主要是删除的文字.复制,运动命令. 删除文本: 正如上一篇文章中讲过的,对于删除命令("d")也具有"(command ...

  7. Tkinter制作简单的python编辑器

    想要制作简单的python脚本编辑器,其中文字输入代码部分使用Tkinter中的Text控件即可实现. 但是问题是,如何实现高亮呢?参考python自带的编辑器:python27/vidle文件夹中的 ...

  8. C语言实例解析精粹学习笔记——39(简单的文本编辑器)

    实例说明: 编辑一个简单的单行文本编辑器,编辑命令有以下几种:(E.Q.R.I.D) 只有自己在完全空白的情况下编写出来的程序,才是真正自己会的程序,现在所做的,不过是程序的搬运工,把书上的程序搬到网 ...

  9. 基于JQuery的简单富文本编辑器

    利用jQuery实现最简单的编辑器 我试了很多种方法,目前最快捷能够实现及其简单的编辑可以使用 document.execCommand("ForeColor", "fa ...

随机推荐

  1. 快速搭建微信小程序开发环境

    1.工具软件: 注:本文介绍的工具软件已分享到百度云盘,直接下载并按照本文介绍安装即可. 开发工具 v0.7 百度云链接: https://pan.baidu.com/s/1jIQ7i8A密码: aq ...

  2. iphone 自学常用网址

    https://www.gitbook.com/book/numbbbbb/-the-swift-programming-language-/details https://github.com/ip ...

  3. python+selenium 定位隐藏元素

    定位隐藏要素的原理:页面主要通过“display:none”来控制元素不可见.所以我们需要通过javaScript修改display的值得值为display="block,来实现元素定位的. ...

  4. 关于android分辨率兼容问题

    关于手机分辨率相关术语和概念 屏幕尺寸:实际的物理尺寸,屏幕的对角线测量.为了方便,android把所有的屏幕尺寸分为了4个广义的大小:小,正常,大,特大. 屏幕密度:屏幕的物理面积内像素的数量,通常 ...

  5. Java_日志接口实现

    日志的接口实现:改用slf4j实现日志功能 为什么要使用slf4j,而不是用log4j或者其他日志框架? 因为slf4j只是规定了一堆实现了日志的接口,并不关心日志怎么实现,这样就可以让项目脱离对日志 ...

  6. LeetCode 795. Number of Subarrays with Bounded Maximum

    问题链接 LeetCode 795 题目解析 给定一个数组A,左右范围L.R.求子数组的数量,要求:子数组最大值在L.R之间. 解题思路 子数组必须连续,利用最大值R对数组进行分段,设定变量 left ...

  7. C#-WebForm-★ASP.NET中的母版页★

    何为母版页,顾名思义母版就是模版,就像在PPT里面的板式或主题一样,大框架已经有了,我们的任务就是向里面添加具体的内容.这样我们制作的所有幻灯片的外观大体都是一样的. 在ASP.NET中母版页有两种作 ...

  8. 嵌入式C语言自我修养 05:零长度数组

    5.1 什么是零长度数组 顾名思义,零长度数组就是长度为0的数组. ANSI C 标准规定:定义一个数组时,数组的长度必须是一个常数,即数组的长度在编译的时候是确定的.在ANSI C 中定义一个数组的 ...

  9. day2: python3.5学习——逻辑判断

    1. 简单的用户名和密码输入 username = "Helen"password = "123abc" _username = input("use ...

  10. 生成allure测试报告之后,服务器端口无法访问查看生成的report,可能是这样引起的。

    1. 检查防火墙 2. 如果机器有安装ADsafe,请关闭adsafe后重试