欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中的一些对话框问题处理

程序员文章站 2022-04-19 10:36:48
1. 对于打开文件对话框处理 #region 打开文件对话框 string StrPath; OpenFileDialog Flag = new OpenFileDialog(); Flag.Multiselect = true;//设置多选 Flag.Title = "打开文件"; //获取或设置 ......

1. 对于打开文件对话框处理

#region 打开文件对话框
string strpath;
openfiledialog flag = new openfiledialog();
flag.multiselect = true;//设置多选
flag.title = "打开文件"; //获取或设置文件对话框标题
flag.filterindex = 2;//设置默认显示文件类型为csv文件(*.csv)|*.csv
flag.initialdirectory = "d:\\"; //设置初始路径
flag.filter = "csv files (*.csv)|*.csv|all files (* .*)|* .*"; //设置“另存为文件类型”或“文件类型”框中出现的选择内容
flag.restoredirectory = true; //设置对话框是否记忆上次打开的目录
if (flag.showdialog() == dialogresult.ok)
{
strpath = flag.filename;
messagebox.show(strpath);
}
#endregion

 

2. 保存文件的对话框处理

#region 保存文件对话框


streamwriter mystream;
savefiledialog saveflag = new savefiledialog();
saveflag.filename = "保存";//设置默认文件名
saveflag.defaultext = "csv";//设置默认格式(可以不设)
saveflag.addextension = true;//设置自动在文件名中添加扩展名
saveflag.filter = "csv files (*.csv)|*.csv|all files (* .*)|* .*";
saveflag.restoredirectory = true;
if (saveflag.showdialog() == dialogresult.ok)
{
string strname;
strname = saveflag.filename;
mystream = new streamwriter(saveflag.filename);
mystream.write(textbox1.text);
mystream.flush();
mystream.close();
}

#endregion

3.颜色对话框的处理

#region 颜色对话框
colordialog colordialog1 = new colordialog();
colordialog1.allowfullopen = false;
colordialog1.color = color.red;
colordialog1.showhelp = true;
if (colordialog1.showdialog() == dialogresult.ok)
{
textbox1.backcolor = colordialog1.color;
}
#endregion

 

4.字体的对话框处理

#region 字体对话框
fontdialog fontdialog = new fontdialog();
fontdialog.font = textbox1.font;
fontdialog.color = textbox1.forecolor;
if (fontdialog.showdialog() != dialogresult.cancel)
{
textbox1.font = fontdialog.font;
textbox1.forecolor = fontdialog.color;
}
#endregion