簡單實(shí)現(xiàn)winform編輯器
本文實(shí)例為大家分享了winform編輯器的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
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) { //獲取正在點(diǎn)擊的行和列。 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() { //斷開式鏈接查看數(shù)據(jù)庫數(shù)據(jù) 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(); //打開默認(rèn)的文件目錄 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); //循環(huán)判斷傳入的表中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; //往數(shù)據(jù)庫添加 文件名和路徑名 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; //從數(shù)據(jù)庫刪除正在點(diǎn)擊的文件名 com.CommandText = String.Format("delete from TxtBianJiQi where Name='{0}'", ClickRow); com.Connection = con; con.Open(); DialogResult dr = MessageBox.Show("確認(rèn)刪除?", "提示", 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里進(jìn)行填寫文本,里面用到的ADO.NET來鏈接數(shù)據(jù)庫,在新建文本的時候需要寫入.txt后綴名,打開或者是刪除的時候需要先點(diǎn)擊一下文本名。 寫的不足請見諒!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C#窗體控件DataGridView常用設(shè)置
欄 目:C#教程
本文標(biāo)題:簡單實(shí)現(xiàn)winform編輯器
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5535.html
您可能感興趣的文章
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改