国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

簡單實現winform編輯器

2020-01-24 00:33:16
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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("http://") + 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后綴名,打開或者是刪除的時候需要先點擊一下文本名。 寫的不足請見諒!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南丹县| 游戏| 莱阳市| 十堰市| 偃师市| 汝州市| 靖宇县| 涟水县| 新竹市| 汝阳县| 丽江市| 福建省| 英德市| 清水县| 若羌县| 平湖市| 莲花县| 铜山县| 旌德县| 承德县| 察哈| 广河县| 雷山县| 台前县| 镶黄旗| 秀山| 张家港市| 香格里拉县| 甘肃省| 阿克| 清流县| 邹城市| 临西县| 周口市| 濮阳县| 侯马市| 法库县| 孝感市| 昭觉县| 建平县| 灵寿县|