本文实例为大家分享了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,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编辑器全部内容,希望文章能够帮你解决简单实现winform编辑器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)