时间:2022-12-06 13:35:35 | 栏目:.NET代码 | 点击:次
MessageBox.Show(<字符串> Text, <字符串> Title, <整型> nType,MessageBoxIcon);
MessageBox.Show("用户名或者密码不能为空");
MessageBox.Show("用户名或者密码不能为空","登录提示");
MessageBox.Show("用户名或者密码不能为空","登录提示",MessageBoxButtons.OKCancel);
MessageBox.Show("用户名或者密码不能为空","登录提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Exclamation);
除PrintPreviewDialog外,所有的对话框都继承于抽象类CommonDialog。
基本属性
示例
System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
dlg.Title = "打开文件";
dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Templates);
dlg.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
dlg.FilterIndex = 2;
dlg.RestoreDirectory = true;
if (dlg.ShowDialog() == DialogResult.OK)
{
if (dlg.FileName != "") //如果dlg.Multiselect=true;可以是dlg.FileNames
{
MessageBox.Show("你选择了" + dlg.FileName);
}
}
属性
示例
System.IO.Stream stream;
System.Windows.Forms.SaveFileDialog saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((stream = saveFileDialog1.OpenFile()) != null)
{
// Code to write the stream goes here.
stream.Close();
}
}
AutoScrollMargin 获取或设置自动滚动边距的大小。
AutoScrollMinSize 获取或设置自动滚动的最小尺寸。
DialogResult 获取或设置窗体的对话框结果。
Document 获取或设置要预览的文档。
HelpButton 获取或设置一个值,该值指示是否应在窗体的标题框中显示“帮助”按钮。
AllowPrintToFile 禁止或使用"打印到文件"复选框
AllowSelection 禁止或使用"选定内容"单选框
AllowSomePages 禁止或使用"页"单选按钮
Document 从中获取打印机设置的PrintDocument
private void printPreviewButton_Click(object sender, EventArgs e)
{
StreamReader streamToPrint = new StreamReader("PrintMe.Txt");
try
{
PrintDocument pd = new PrintDocument(streamToPrint); //假定为默认打印机
if (storedPageSettings != null)
{
pd.DefaultPageSettings = storedPageSettings;
}
PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Document = pd;
dlg.ShowDialog();
}
finally
{
streamToPrint.Close();
}
}
private void printButton_Click(object sender, EventArgs e)
{
StreamReader streamToPrint = new StreamReader("PrintMe.Txt");
try
{
PrintDocument pd = new PrintDocument(streamToPrint);
PrintDialog dlg = new PrintDialog();
dlg.Document = pd;
DialogResult result = dlg.ShowDialog();
if (result == DialogResult.OK)
pd.Print();
}
finally
{
streamToPrint.Close();
}
}
打开模态窗口后,只要不关闭该窗口,鼠标焦点或者光标就会一直停留在该窗口上。只有关闭该窗口后,调用窗口才能继续。
模态窗口关闭后,仍可以读取模态窗口中的信息,如窗口的返回状态等,以后还可以使用ShowDialog()使其可见。
打开非模态窗口后,仍可以操作调用窗口。后面的代码立即执行。
关闭非模态窗口,该窗口将不复存在,会释放窗口的所有资源,所以无法得到该窗口的任何信息。常用Hide()方法(等效于Visible=false)然后调用Show()方法使其可见。
public Form1(string para)//获取参数
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterParent;//启动位置,父窗口中央
this.MaximizeBox = false;
this.MinimizeBox = false;
this.ShowIcon = false;//不显示图标
this.ControlBox = false;
this.ShowInTaskbar = false;
this.FormBorderStyle = FormBorderStyle.FixedDialog;//边框样式为固定对话框
this.btnOK.DialogResult = DialogResult.OK;//"Enter"为OK按钮
this.btnCancel.DialogResult = DialogResult.Cancel;//“ESC”为Cancel按钮
this.textBox1.Text = para;
}
public string ReturnText //定义一个公共属性,供调用窗口Form1使用
{
get { return this.textBox1.Text + "b"; }
}
private void Form1_Load(object sender, EventArgs e)
{
if (this.Owner.Name != "Form1")//Owner为调用窗体,即调用改对话框的窗体
MessageBox.Show("非法调用");
}
private void BtnOK_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.Trim().Length == 0)
MessageBox.Show("无输入");
this.textBox1.Focus();
this.DialogResult = DialogResult.None;//阻止隐藏对话框,对话框不消失
}
Form f2 = new Form2("a");
if (f2.ShowDialog(this) == DialogResult.OK)//对应Form2中的Owner,this为给对话框窗体传值
this.textBox1.Text = f2.ReturnText;
f2.Close();
f2.Dispose();