首页 > 代码库 > 对话框控件--2016年12月11日

对话框控件--2016年12月11日

打开对话框-----OpenFileDialog

 1             //打开文件对话框   2             OpenFileDialog openFile = new OpenFileDialog();   3             openFile.Title = "请选择要打开的文件";  //对话框标题   4             openFile.Multiselect = true; //设置对话框可以多选   5             openFile.InitialDirectory = @"C:\Users\tufeiax\Desktop";//获取对话框的初始目录   6             openFile.Filter = "文本文件|*.txt|图片文件|*.jdg|所有文件|*.*";   7             openFile.ShowDialog();   8    9             try  10             {  11                 //获取对话框选择的文件和路径  12                 string path = openFile.FileName;  13                 //创建文件操作对象  14                 using (FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read))  15                 {  16                     byte[] buffer = new byte[1024 * 1024 * 5];  17                     //实际读取的字节数  18                     int r = fsRead.Read(buffer, 0, buffer.Length);  19                     textBox1.Text = Encoding.Default.GetString(buffer);  20                 }  21             }  22             catch (Exception)  23             {  24                   25                 return;  26             }  

保存对话框------SaveFileDialog

 1            SaveFileDialog save = new SaveFileDialog();   2            save.Title = "请选择要保存的路径";   3            save.InitialDirectory = @"C:\Users\tufeiax\Desktop";   4            save.Filter = "文本文件|*.txt|图片文件|*.jdg|所有文件|*.*";   5            save.ShowDialog();   6    7            string path = save.FileName;   8            try    9         {             10             using(FileStream fsWrite = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Write))  11                {  12                    byte[] buffer = Encoding.Default.GetBytes(textBox1.Text);  13                    fsWrite.Write(buffer,0,buffer.Length);      14                }  15         }  16         catch (Exception)  17         {         18             return;  19         }  20            MessageBox.Show("保存成功!");  

字体对话框(FontDialog)颜色对话框(ColorDialog)

1            //创建字体对话框对象  2            FontDialog font = new FontDialog();  3            font.ShowDialog();  4            textBox1.Font = font.Font; 

5 //创建颜色对话框对象 6 ColorDialog cl = new ColorDialog(); 7 cl.ShowDialog(); 8 textBox1.ForeColor = cl.Color;

 

打印控件    PageSetupDialog、PrintDialog、PrintDocument(围绕)、PrintPreviewControl、PrintPreviewDialog

技术分享
 1         #region 打印设置,打印,打印预览 2         private void toolStripMenuItem3_Click(object sender, EventArgs e) 3         { 4             pageSetupDialog1.Document = printDocument1; 5             if (pageSetupDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) 6             { 7                 printDocument1.DefaultPageSettings = pageSetupDialog1.PageSettings; 8             } 9         }10         private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)11         {12             printDialog1.Document = printDocument1;13             if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)14             {15                 printDocument1.Print();16             }17         }18 19         private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)20         {21             printPreviewDialog1.Document = printDocument1;22             printPreviewDialog1.ShowDialog();23         }24 25         private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)26         {27             e.Graphics.DrawString(richTextBox1.Text, richTextBox1.Font, new SolidBrush(Color.Black), 20, 20);28         }29 30         private void toolStripMenuItem1_Click(object sender, EventArgs e)31         {32             richTextBox1.SelectedText = "";33         }34         #endregion
使用

 

 

 

 

 

 

 

.

 

对话框控件--2016年12月11日