using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CommonDlgs
{
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent( );
}
//依次显示4个不同类型的消息框。
private void btnMsgBox_Click(object sender, EventArgs e)
{
MessageBox.Show("这是第一个消息框,只有确认按钮"); //显示最简单的MessageBox
MessageBox.Show("这是第二个消息框,有标题,只有确认按钮", "第二个消息框"); //显示有文本和标题的MessageBox
//显示具有文本、标题、确定和取消按钮的MessageBox
MessageBox.Show("这是第三个消息框,有标题,只有确认和取消按钮",
"第三个消息框", MessageBoxButtons.OKCancel);
//显示具有文本、标题、确定和取消按钮、告警图标的MessageBox
MessageBox.Show("这是第四个消息框,有标题,只有确认和取消按钮,告警图标",
"第四个消息框", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
}
private void btnOpenFile_Click(object sender, EventArgs e)
{
OpenFileDialog ofdlg = new OpenFileDialog( ); //创建OpenFileDialog对象
ofdlg.Filter = "文本文件(*.txt)|*.TXT|Word文件(*.doc)|*.DOC"; //只选择TXT和DOC扩展名文件
ofdlg.Title = "选择文本文件或Word文件"; //设置对话框的标题
if(ofdlg.ShowDialog() == DialogResult.OK) //显示对话框,并等待返回
{
this.tbOpenFileName.Text = ofdlg.FileName; //如果用户选择了文件则显示到界面
}
else
{
this.tbOpenFileName.Text = "还没有选择要打开的文件"; //没有选择文件,则显示默认提示
}
}
private void btnSetColor_Click(object sender, EventArgs e)
{
ColorDialog cdlg = new ColorDialog( ); //创建ColorDialog对象
cdlg.Color = btnSetColor.ForeColor; //设置默认颜色为btnSetColor当前前景色
if (cdlg.ShowDialog( ) == DialogResult.OK) //显示对话框,并等待返回
{
this.btnSetColor.ForeColor = cdlg.Color; //选择了新的颜色,则更新btnSetColor前景色
}
}
private void btnSaveFile_Click(object sender, EventArgs e)
{
SaveFileDialog sfdlg = new SaveFileDialog( ); //创建SaveFileDialog对象
sfdlg.Filter = "文本文件(*.txt)|*.TXT"; //默认扩展名为*.TXT
sfdlg.Title = "请选择或输入要保存的文本文件";
if(sfdlg.ShowDialog() == DialogResult.OK) //显示对话框,并等待返回
{
this.tbSaveFileName.Text = sfdlg.FileName; //如果用户选择了文件则显示到界面
}
else
{
this.tbSaveFileName.Text = "还没有选择要保存的文件"; //没有选择文件,则显示默认提示
}
}
private void btnSetFont_Click(object sender, EventArgs e)
{
FontDialog fdlg = new FontDialog( ); //创建FontDialog对象
fdlg.Font = btnSetFont.Font; //设置默认字体为btnSetFont当前字体
if (fdlg.ShowDialog( ) == DialogResult.OK) //显示对话框,并等待返回
{
this.btnSetFont.Font = fdlg.Font; //选择了新的字体,则更新btnSetFont的字体
}
}
}
}
控件标注代码如下:
namespace CommonDlgs
{
partial class Form1
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// "disposing">如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose( );
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent( )
{
this.btnMsgBox = new System.Windows.Forms.Button( );
this.btnOpenFile = new System.Windows.Forms.Button( );
this.tbOpenFileName = new System.Windows.Forms.TextBox( );
this.btnSaveFile = new System.Windows.Forms.Button( );
this.tbSaveFileName = new System.Windows.Forms.TextBox( );
this.btnSetColor = new System.Windows.Forms.Button( );
this.btnSetFont = new System.Windows.Forms.Button( );
this.SuspendLayout( );
//
// btnMsgBox
//
this.btnMsgBox.Location = new System.Drawing.Point(12, 12);
this.btnMsgBox.Name = "btnMsgBox";
this.btnMsgBox.Size = new System.Drawing.Size(130, 23);
this.btnMsgBox.TabIndex = 0;
this.btnMsgBox.Text = "显示MessageBox()";
this.btnMsgBox.UseVisualStyleBackColor = true;
this.btnMsgBox.Click += new System.EventHandler(this.btnMsgBox_Click);
//
// btnOpenFile
//
this.btnOpenFile.Location = new System.Drawing.Point(12, 42);
this.btnOpenFile.Name = "btnOpenFile";
this.btnOpenFile.Size = new System.Drawing.Size(130, 23);
this.btnOpenFile.TabIndex = 1;
this.btnOpenFile.Text = "打开文件";
this.btnOpenFile.UseVisualStyleBackColor = true;
this.btnOpenFile.Click += new System.EventHandler(this.btnOpenFile_Click);
//
// tbOpenFileName
//
this.tbOpenFileName.Location = new System.Drawing.Point(12, 71);
this.tbOpenFileName.Name = "tbOpenFileName";
this.tbOpenFileName.Size = new System.Drawing.Size(439, 21);
this.tbOpenFileName.TabIndex = 2;
//
// btnSaveFile
//
this.btnSaveFile.Location = new System.Drawing.Point(12, 100);
this.btnSaveFile.Name = "btnSaveFile";
this.btnSaveFile.Size = new System.Drawing.Size(130, 23);
this.btnSaveFile.TabIndex = 1;
this.btnSaveFile.Text = "保存文件";
this.btnSaveFile.UseVisualStyleBackColor = true;
this.btnSaveFile.Click += new System.EventHandler(this.btnSaveFile_Click);
//
// tbSaveFileName
//
this.tbSaveFileName.Location = new System.Drawing.Point(12, 129);
this.tbSaveFileName.Name = "tbSaveFileName";
this.tbSaveFileName.Size = new System.Drawing.Size(439, 21);
this.tbSaveFileName.TabIndex = 2;
//
// btnSetColor
//
this.btnSetColor.Location = new System.Drawing.Point(171, 12);
this.btnSetColor.Name = "btnSetColor";
this.btnSetColor.Size = new System.Drawing.Size(132, 23);
this.btnSetColor.TabIndex = 3;
this.btnSetColor.Text = "设置颜色";
this.btnSetColor.UseVisualStyleBackColor = true;
this.btnSetColor.Click += new System.EventHandler(this.btnSetColor_Click);
//
// btnSetFont
//
this.btnSetFont.Location = new System.Drawing.Point(309, 12);
this.btnSetFont.Name = "btnSetFont";
this.btnSetFont.Size = new System.Drawing.Size(132, 23);
this.btnSetFont.TabIndex = 3;
this.btnSetFont.Text = "设置字体";
this.btnSetFont.UseVisualStyleBackColor = true;
this.btnSetFont.Click += new System.EventHandler(this.btnSetFont_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(463, 165);
this.Controls.Add(this.btnSetFont);
this.Controls.Add(this.btnSetColor);
this.Controls.Add(this.tbSaveFileName);
this.Controls.Add(this.btnSaveFile);
this.Controls.Add(this.tbOpenFileName);
this.Controls.Add(this.btnOpenFile);
this.Controls.Add(this.btnMsgBox);
this.Name = "Form1";
this.Text = "通用对话框实例";
this.ResumeLayout(false);
this.PerformLayout( );
}
#endregion
private System.Windows.Forms.Button btnMsgBox;
private System.Windows.Forms.Button btnOpenFile;
private System.Windows.Forms.TextBox tbOpenFileName;
private System.Windows.Forms.Button btnSaveFile;
private System.Windows.Forms.TextBox tbSaveFileName;
private System.Windows.Forms.Button btnSetColor;
private System.Windows.Forms.Button btnSetFont;
}
}