35.通用对话框
不同的windows应用软件常常使用功能相同的对话框,比如,打开,保存,打印等对话框,这些称为通用对话框。我们来学习一下6中通用对话框,分别为:消息框(Messagebox),打开文件对话框(OpenFileDialog),保存文件对话框(SaveFiledialog),颜色对话框(ColorDialog),字体对话框(FontDialog)和打印对话框(Printdialog)。
1.消息框
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;
namespace WindowsFormsApplication25
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult result;
if (this.textBox1.Text == "guojun66")
{
MessageBox.Show(this, "口令正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
result = MessageBox.Show(this,"口令错误","提示",MessageBoxButtons.RetryCancel,MessageBoxIcon.Information);
if (result == DialogResult.Retry)
{
this.textBox1.Text = "";
this.textBox1.Focus();
}
else
{
Application.Exit();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
2.打开和保存文件。
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;
namespace WindowsFormsApplication26
{
public partial class Form1 : Form
{
OpenFileDialog openFileDialog = new OpenFileDialog();
SaveFileDialog saveFileDialog = new SaveFileDialog();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
MessageBox.Show("打开的文件\n" + openFileDialog.FileName,"打开文件",
MessageBoxButtons.OK,MessageBoxIcon.Information);
}
private void button2_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("保存的文件\n" + saveFileDialog.FileName, "保存文件",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
3.颜色选择对话框
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;
namespace WindowsFormsApplication27
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
colorDialog.CustomColors = new int[]
{
0x0000ff,0xff0000,0xff00ff,0xffff00,0xffffff
};
colorDialog.AllowFullOpen = false;
if(colorDialog.ShowDialog() == DialogResult.OK)
{
BackColor = colorDialog.Color;
}
}
}
}
4.字体对话框
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;
namespace WindowsFormsApplication28
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FontDialog fontdialog = new FontDialog();
fontdialog.ShowColor = true;
fontdialog.AllowScriptChange = true;
fontdialog.AllowVectorFonts = true;
fontdialog.ShowEffects = true;
if (fontdialog.ShowDialog() == DialogResult.OK)
{
this.textBox1.Font = fontdialog.Font;
this.textBox1.ForeColor = fontdialog.Color;
}
}
}
}
5.打印对话框
不常见,就不介绍了。