流:
1 private void 打开OToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 openFileDialog1.Filter = "文本文件|*.txt|全部文件|*.*";//规定文件格式,固定写法。 4 DialogResult dr = openFileDialog1.ShowDialog(); 5 if (dr == DialogResult.OK) 6 { 7 label1.Text = openFileDialog1.FileName; 8 StreamReader sr = new StreamReader(openFileDialog1.FileName);//stream就是流,用来文件传输,streamreader是读取文件 9 textBox1.Text = sr.ReadToEnd();//读到最后10 //richTextBox1.Text = sr.ReadToEnd();11 sr.Close();//流只有一条,用完要关闭12 }13 }14 string path = "";15 private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)16 {17 if (path == "")//判断文件保存路径是否存在,如果存在直接保存。18 {19 saveFileDialog1.FileName = "新建文本文件.txt";20 saveFileDialog1.ShowDialog();21 path = saveFileDialog1.FileName;22 }23 StreamWriter sw = new StreamWriter(path);//保存文件24 sw.Write(textBox1.Text);25 sw.Close();26 }
打印控件用法:
1 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 2 { 3 Font f = new Font("宋体",14); 4 Brush b = new SolidBrush(Color.Black); 5 PointF p = new PointF(10,10); 6 e.Graphics.DrawString(textBox1.Text, f, b, p);//如果要打印,首先要创建一个printDocument来绘制打印对象 7 } 8 9 10 11 private void 打印设置ToolStripMenuItem_Click(object sender, EventArgs e)12 {13 pageSetupDialog1.Document = printDocument1;//制定打印的对象是自己创建的打印对象14 pageSetupDialog1.ShowDialog();15 }16 private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)17 {18 //printPreviewControl1.Document = printDocument1;19 printPreviewDialog1.Document = printDocument1;20 printPreviewDialog1.ShowDialog();21 }22 23 private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)24 {25 printDialog1.Document = printDocument1;26 printDialog1.ShowDialog();27 }