对话框中我们常用了以下几种:
1、文件对话框(FileDialog) 它又常用到两个:
打开文件对话框(OpenFileDialog)
保存文件对话(SaveFileDialog)
2、字体对话框(FontDialog)
3、颜色对话框(ColorDialog)
4、打印预浏对话框(PrintPreviewDialog)
5、页面设置(PrintDialog)
6、打印对话框(PrintDialog)
还有更多,有时间的网友可以看看MSDN。下面我们一个个来介绍。在介绍的过程中我用到了一个自己开发的类:File,主要是文件操作的。
文件对话框(FileDialog)
一、打开文件对话框(OpenFileDialog)
1、 OpenFileDialog控件有以下基本属性
InitialDirectory | 对话框的初始目录 |
Filter | 要在对话框中显示的文件筛选器,例如,"文本文件(*.txt)|*.txt|所有文件(*.*)||*.*" |
FilterIndex | 在对话框中选择的文件筛选器的索引,如果选第一项就设为1 |
RestoreDirectory | 控制对话框在关闭之前是否恢复当前目录 |
FileName | 第一个在对话框中显示的文件或最后一个选取的文件 |
Title | 将显示在对话框标题栏中的字符 |
AddExtension | 是否自动添加默认扩展名 |
CheckPathExists | 在对话框返回之前,检查指定路径是否存在 |
DefaultExt | 默认扩展名 |
DereferenceLinks | 在从对话框返回前是否取消引用快捷方式 |
ShowHelp | 启用"帮助"按钮 |
ValiDateNames | 控制对话框检查文件名中是否不含有无效的字符或序列 |
2、 OpenFileDialog控件有以下常用事件
FileOk | 当用户点击"打开"或"保存"按钮时要处理的事件 |
HelpRequest | 当用户点击"帮助"按钮时要处理的事件 |
3、OpenFileDialog的效果如下:
可以用以下代码来实现上面这个对话框:
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
其中有用到了File()这个类,是我在程序用来执行文件操作,自己写的,在最后附了这个类的源码。有兴趣的网友可以自己分析。
二、保存文件对话框(SaveFileDialog)
保存文件对话框控件有两种情况,一就是保存,二就是另存为,保存很简单,就是在文件已经打开的情况下的,再把文件写一篇,在这里我们主要讲另存为 的情况(SaveAs)。
1,SaveFileDialog控件的属性
Filter | 要在对话框中显示的文件筛选器,例如,"文本文件(*.txt)|*.txt|所有文件(*.*)|*.*" |
FilterIndex | 在对话框中选择的文件筛选器的索引,如果选第一项就设为1 |
RestoreDirectory | 控制对话框在关闭之前是否恢复当前目录 |
AddExtension | 是否自动添加默认扩展名 |
CheckFileExists | |
CheckPathExists | 在对话框返回之前,检查指定路径是否存在 |
Container | 控制在将要创建文件时,是否提示用户。只有在ValidateNames为真值时,才适用。 |
DefaultExt | 缺省扩展名 |
DereferenceLinks | 在从对话框返回前是否取消引用快捷方式 |
FileName | 第一个在对话框中显示的文件或最后一个选取的文件 |
InitialDirector | 对话框的初始目录 |
OverwritePrompt | 控制在将要在改写现在文件时是否提示用户,只有在ValidateNames为真值时,才适用 |
ShowHelp | 启用"帮助"按钮 |
Title | 将显示在对话框标题栏中的字符 |
ValidateNames | 控制对话框检查文件名中是否不含有无效的字符或序列 |
2、SaveFileDialog事件如下:
FileOk | 当用户点击"打开"或"保存"按钮时要处理的事件 |
HelpRequest | 当用户点击"帮助"按钮时要处理的事件 |
3、SaveFileDialog的效果如下:
4、用下例代码可以实现
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
其实这些都可以在VS.NET的IDE环境中直接完成,为了说明问题,我还是一条条列也来了。当中用到了一个File的类库,下面是源程序:
File.cs
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
字体对话框(FontDialog)
在文字处理中,我们常用到字体,现在我们来做一个最常见的字体对话框。
一、 字体对话框(FontDialog)常用属性
ShowColor | 控制是否显示颜色选项 |
AllowScriptChange | 是否显示字体的字符集 |
Font | 在对话框显示的字体 |
AllowVerticalFonts | 是否可选择垂直字体 |
Color | 在对话框中选择的颜色 |
FontMustExist | 当字体不存在时是否显示错误 |
MaxSize | 可选择的最大字号 |
MinSize | 可选择的最小字号 |
ScriptsOnly | 显示排除OEM和Symbol字体 |
ShowApply | 是否显示"应用"按钮 |
ShowEffects | 是否显示下划线、删除线、字体颜色选项 |
ShowHelp | 是否显示"帮助"按钮 |
二、 字体对话框(FontDialog)的事件
Apply | 当点击"应用"按钮时要处理的事件 |
HelpRequest | 当点击"帮助"按钮时要处理的事件 |
三、 字体对话框(FontDialog)的效果
四、 实现代码
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
上面代码将选定的文本设置为当前FontDialog对话框中的字体。
颜色对话框(ColorDialog)
颜色拾取框也是我们常见的对话框之一,下面我们来看看在C#中是如何操作颜色对话框的呢?
一、 颜色对话框(ColorDialog)的常见属性
AllowFullOpen | 禁止和启用"自定义颜色"按钮 |
FullOpen | 是否最先显示对话框的"自定义颜色"部份 |
ShowHelp | 是否显示"帮助"按钮 |
Color | 在对话框中显示的颜色 |
AnyColor | 显示可选择任何颜色 |
CustomColors | 是否显示自定义颜色 |
SolidColorOnly | 是否只能选择纯色 |
二、 颜色对话框(ColorDialog)的效果如下:
三、 实现代码见下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
实现颜色对话框(ColorDialog)很容易吧,其实不只是颜色对话框,C#也是很容易的,只要用心去学,都容易的。讲完了颜色对话框(ColorDialog),我们 来讲打印和页面设置.
页面设置(PageSetupDialog) 其实页面设置(PageSetupDialog)没有太多的讲,既然讲到这里,我还是把PageSetupDialog中常用的属性列出来吧 一、页面设置(PageSetupDialog)常见属性
二、面设置(PageSetupDialog)的效果如下 打印预浏及打印 一, 在.net环境中,说到打印,就不能不说PrintDocumet这个类,PrintDocument属于System.Drawing.Printing这个名字空间,PrintDocument这个类是 实现打印的核心代码。 void printDocument_PrintPage(o b j e c t sender, System.Drawing.Printing.PrintPageEventArgs e)
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 在这里可以完成整个打印任务。把printDocument_PrintPage构造好后,就可以打印和打印预浏。 二、打印预浏对话框(PrintPreviewDialog) void printPreviewBTN_Click(o b j e c t sender, System.EventArgs e)
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 很简单的,只要你把PrintDocument_PrintPage()写好了,这里就很容易了。 三、打印对话框(PrintDialog)
2、效果如下:
void printDialogBTN_Click(o b j e c t sender, System.EventArgs e)
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |