c#中的几种Dialog
程序员文章站
2022-03-04 13:26:33
1、OpenFileDialog 1 private void FileOpen_Click(object sender, EventArgs e) 2 { 3 OpenFileDialog openFile = new OpenFileDialog();//创建OpenFileDialog对象 4 ......
1、openfiledialog
1 private void fileopen_click(object sender, eventargs e) 2 { 3 openfiledialog openfile = new openfiledialog();//创建openfiledialog对象 4 5 openfile.initialdirectory = @"e:\";//打开初始目录 6 openfile.title = "选择打开文件";//窗体标题 7 openfile.filter = "txt files (*.txt)|*.txt|all files (*.*)|*.*";//过滤条件 8 openfile.filterindex = 2;//获取第二个过滤条件开始的文件 9 openfile.multiselect = true;//是否多选 10 11 if (openfile.showdialog() == dialogresult.ok)//页面弹出判断是否点击确定按钮 12 { 13 //没勾选多选时 14 //string filename = openfile.filename; 15 //string name=openfile.safefilename; 16 17 //勾选多选时 18 for (int i = 0; i < openfile.safefilenames.length; i++)//获取文件名,拓展名 19 { 20 rictbo.text += openfile.safefilenames[i] + "\r\n"; 21 } 22 for (int i = 0; i < openfile.filenames.length; i++)//获取文件全部路径 23 { 24 rictbo.text += openfile.filenames[i] + "\r\n"; 25 } 26 } 27 }
2、savefiledialog
savefiledialog与openfiledialog属性基本相同就简单写了
1 private void button1_click(object sender, eventargs e) 2 { 3 savefiledialog savefile = new savefiledialog(); 4 5 savefile.initialdirectory= @"e:\";//打开初始目录 6 savefile.title = "选择保存文件"; 7 savefile.filter = "txt files (*.txt)|*.txt|all files (*.*)|*.*|图片(.jpg)|*.jpg";//过滤条件 8 savefile.filterindex = 1;//获取第二个过滤条件开始的文件拓展名 9 savefile.filename = "新建";//默认保存名称 10 11 if (savefile.showdialog()==dialogresult.ok)//页面弹出判断是否点击确定按钮 12 { 13 string txt = rictbo.text; 14 //写入 15 file.writealltext(savefile.filename, txt); 16 } 17 } 18 }
3、folderbrowserdialog
这个使用的不多日后用到再添加
1 private void button2_click(object sender, eventargs e) 2 { 3 folderbrowserdialog dialog = new folderbrowserdialog(); 4 dialog.description = "选择匹配目录"; ;//左上角提示 5 string path = string.empty; 6 7 if (dialog.showdialog() == dialogresult.ok) 8 { 9 path = dialog.selectedpath;//获取选中文件路径 10 } 11 }
4、fontdialog
1 private void button3_click(object sender, eventargs e) 2 { 3 fontdialog fontdialog = new fontdialog(); 4 5 fontdialog.showcolor=true;//显示颜色选择 6 fontdialog.font = rictbo.font; 7 fontdialog.color = rictbo.forecolor; 8 9 if (fontdialog.showdialog()==dialogresult.ok)//页面弹出判断是否点击确定按钮 10 { 11 rictbo.font = fontdialog.font;//字体 12 rictbo.forecolor = fontdialog.color;//字体颜色 13 } 14 }
5、colordialog
1 private void color_click(object sender, eventargs e) 2 { 3 colordialog colordialog = new colordialog(); 4 5 //colordialog.allowfullopen = false;是否启用自定义颜色 6 colordialog.color = rictbo.forecolor; 7 if (colordialog.showdialog()==dialogresult.ok) 8 { 9 rictbo.forecolor = colordialog.color; 10 } 11 }
上一篇: C# 委托链(多播委托)
下一篇: C# Array数组是引用类型