简单实现winform编辑器
更新时间:2020年6月25日 11:18 点击:1519
本文实例为大家分享了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后缀名,打开或者是删除的时候需要先点击一下文本名。 写的不足请见谅!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
相关文章
- 本篇文章是对C#中WinForm控件之Dock顺序调整进行了详细的分析介绍,需要的朋友参考下...2020-06-25
- 这篇文章主要介绍了C# Winform中实现主窗口打开登录窗口关闭的方法,这在需要用户名密码的软件项目中是必用的一个技巧,要的朋友可以参考下...2020-06-25
- 下面小编就为大家带来一篇C# winform打开Excel文档的方法总结(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要给大家介绍C# winform快捷键设置技巧,涉及到C winform快捷键相关知识,对C winform知识感兴趣的朋友可以参考下本篇文章...2020-06-25
c#中Winform实现多线程异步更新UI(进度及状态信息)
本篇文章主要介绍了c#中Winform实现多线程异步更新UI(进度及状态信息) ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25- 这篇文章主要给大家介绍了关于c# winform异步不卡界面的实现方法,文中通过示例代码介绍的非常详细,对大家学习或者使用c#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-06-25
C# WinForm程序处理后台繁忙导致前台控件假死现象解决方法
这篇文章主要介绍了C# WinForm程序处理后台繁忙导致前台控件假死现象解决方法,本文通过Application.DoEvents()解决这个问题,并讲解了Application.DoEvents()的作用,需要的朋友可以参考下...2020-06-25- 这篇文章主要介绍了C#中winform使用相对路径读取文件的方法,实例分析了C#使用相对路径读取文件的技巧与实际应用,需要的朋友可以参考下...2020-06-25
winform中的ListBox和ComboBox绑定数据用法实例
这篇文章主要介绍了winform中的ListBox和ComboBox绑定数据用法,实例分析了将集合数据绑定到ListBox和ComboBox控件的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25- 这篇文章主要介绍了C#在Winform开发中使用Grid++报表,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要为大家详细介绍了C# WinForm程序设计简单计算器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
C# WinForm控件对透明图片重叠时出现图片不透明的简单解决方法
这篇文章主要介绍了C# WinForm控件对透明图片重叠时出现图片不透明的简单解决方法,结合实例形式分析了WinForm图片重叠后造成图片不透明的原因与相应的解决方法,需要的朋友可以参考下...2020-06-25- 在工作中遇到这样一个问题,在系统使用过程中,输入法会变灰导致无法使用输入法输入文字,就好像输入法被禁用了没有启用似的。对此,在这里做个备录...2020-06-25
- 这篇文章主要介绍了C#实现Winform中打开网页页面的方法,涉及WinForm中WebBrowser的相关使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
C#实现winform中RichTextBox在指定光标位置插入图片的方法
这篇文章主要介绍了C#实现winform中RichTextBox在指定光标位置插入图片的方法,涉及RichTextBox控件及剪切板的相关操作技巧,非常简单实用,需要的朋友可以参考下...2020-06-25C# Winform 调用系统接口操作 INI 配置文件的代码
封装了一小段代码, 调用系统接口, 操作配置文件. 一般用于 .ini 文件, 或者其它键值对格式的配置文件...2020-06-25- 这篇文章主要介绍了WinForm调用百度地图接口用法,结合具体实例形式简单分析了WinForm WebBrower控件与前端百度接口交互的相关操作技巧,需要的朋友可以参考下...2020-06-25
- 在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式...2020-06-25
- 这篇文章主要介绍了C# WinForm中禁止改变窗口大小的方法,需要把FormBorderStyle和MaximizeBox的值固定即可,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了C# inForm编程获取文件物理路径的方法,获取的物理路径是软件即软件安装所在目录,需要的朋友可以参考下...2020-06-25