窗体-对话框,记事本

对话框确认和取消选项:

Dialogresult dr=Messbox.show("确定","确定要改?",MessboxBoxButtons.OKCancl);

if(dr==Dialogresult.OK)

{

   //事件

记事本打开文本文档:

private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "文本文件|*.txt";
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
string s = openFileDialog1.FileName;
StreamReader sr = new StreamReader(s, Encoding.Default);
textBox1.Text = sr.ReadToEnd();
string[] ss = s.Split('\\');
this.Text = ss[ss.Length - 1] + "-记事本";
}
}

保存:

private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "文本文件|*.txt";
saveFileDialog1.FileName = "文本文档|*.txt";
DialogResult dr = saveFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
string s = saveFileDialog1.FileName;
StreamWriter sw = new StreamWriter(s);
sw.Write(textBox1.Text);
sw.Flush();
}
}

打印:          

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
string s = textBox1.Text;
Font f = new System.Drawing.Font("宋体", 24);
Brush b = new SolidBrush(Color.Green);
e.Graphics.DrawString(s, f, b, 10, 10);

------------------------------------------------------------------------------//设置打印的对象

private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
pageSetupDialog1.Document = printDocument1;  //每个方法都要设置打印的对象,为 printDocument1,事件中选择printPage
DialogResult dr = pageSetupDialog1.ShowDialog();
if (dr == DialogResult.OK) { }
}

-----------------------------------------------------------------------------//设置页面

private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument1;
DialogResult dr = printDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
printDocument1.Print();
}
}

------------------------------------------------------------------------------//打印选项,比如选择打印机

private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.Show();
}

------------------------------------------------------------------------------//打印预览

posted on 2017-11-27 08:52  段了的弦  阅读(189)  评论(0编辑  收藏  举报