本文实例为大家分享了winform编辑器的具体实现代码,供大家参考,具体内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
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后缀名,打开或者是删除的时候需要先点击一下文本名。 写的不足请见谅!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/shigezhuang/archive/2017/08/21/7406131.html