本题主要考察各种控件的应用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO; namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void richTextBox1_TextChanged(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = "当前已编辑" + richTextBox1.Text.Length + "个字。";
} private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "文本文件|*.txt";
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
FileName = openFileDialog1.FileName;
this.Text=openFileDialog1.SafeFileName+"—FUCK牌记事本";
StreamReader sr = new StreamReader(openFileDialog1.FileName,System.Text.Encoding.Default);
richTextBox1.Text = sr.ReadToEnd();
sr.Close();
}
} private string FileName;
private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "文本文件|*.txt|Word文档|*.doc";
saveFileDialog1.FileName = "新建文本文档";
DialogResult dr = saveFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);
sw.Write(richTextBox1.Text);
sw.Flush();
}
} private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
if (FileName != "")
{
StreamWriter sw = new StreamWriter(FileName);
sw.Write(richTextBox1.Text);
sw.Flush();
}
else
{
另存为AToolStripMenuItem_Click(另存为AToolStripMenuItem, null);
}
} private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
} private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font f = new Font("微软雅黑",);
Brush b =new SolidBrush(Color.Black);
e.Graphics.DrawString(richTextBox1.Text,f,b,,);
} private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
pageSetupDialog1.Document = printDocument1;
pageSetupDialog1.ShowDialog();
} private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
} private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument1;
DialogResult dr = printDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
printDocument1.Print();
}
} private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Cut();
} private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Copy();
} private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
} private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Undo();
} private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.SelectAll();
} private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.WordWrap)
{
richTextBox1.WordWrap = false;
自动换行ToolStripMenuItem.Checked = false;
}
else
{
richTextBox1.WordWrap = true;
自动换行ToolStripMenuItem.Checked = true;
}
} private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectedText != "")
{
DialogResult dr = fontDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
richTextBox1.SelectionFont = fontDialog1.Font;
richTextBox1.SelectionColor = fontDialog1.Color;
}
}
else
{
DialogResult dr = fontDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
richTextBox1.Font = fontDialog1.Font;
richTextBox1.ForeColor = fontDialog1.Color;
}
}
}
}
}
源代码
设计界面