欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

简单实现winform编辑器

程序员文章站 2023-12-09 17:22:57
本文实例为大家分享了winform编辑器的具体实现代码,供大家参考,具体内容如下 using system; using system.collection...

本文实例为大家分享了winform编辑器的具体实现代码,供大家参考,具体内容如下

简单实现winform编辑器

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后缀名,打开或者是删除的时候需要先点击一下文本名。 写的不足请见谅!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。