先上代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace 记事本
{
public partial class Form : System.Windows.Forms.Form
{
public Form()
{
InitializeComponent();
}
private void 文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
// 先拖拽fontdialog控件,打开字体窗口
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
//设置选中字体
richTextBoxNote.SelectionFont = fontDialog1.Font;
}
}
private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
//打开设置颜色窗体
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
richTextBoxNote.SelectionColor = colorDialog1.Color;
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
//设置选中字体为隶书
richTextBoxNote.Select(richTextBoxNote.SelectionStart, richTextBoxNote.SelectionLength);
richTextBoxNote.SelectionFont = new Font("隶书", richTextBoxNote.SelectionFont.Size);
}
private void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
//设置选中字体为宋体
richTextBoxNote.Select(richTextBoxNote.SelectionStart, richTextBoxNote.SelectionLength);
richTextBoxNote.SelectionFont = new Font("宋体", richTextBoxNote.SelectionFont.Size);
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
//设置选中字体为黑体
richTextBoxNote.Select(richTextBoxNote.SelectionStart, richTextBoxNote.SelectionLength);
richTextBoxNote.SelectionFont = new Font("黑体", richTextBoxNote.SelectionFont.Size);
}
//
//设置选中的字体颜色
private void label3_Click(object sender, EventArgs e)
{
richTextBoxNote.Select(richTextBoxNote.SelectionStart, richTextBoxNote.SelectionLength);
richTextBoxNote.SelectionColor = labelGreen.BackColor;
}
private void label1_Click(object sender, EventArgs e)
{
richTextBoxNote.Select(richTextBoxNote.SelectionStart, richTextBoxNote.SelectionLength);
richTextBoxNote.SelectionColor = labelRed.BackColor;
}
private void label4_Click(object sender, EventArgs e)
{
richTextBoxNote.Select(richTextBoxNote.SelectionStart, richTextBoxNote.SelectionLength);
richTextBoxNote.SelectionColor = labelBlue.BackColor;
}
private void label5_Click(object sender, EventArgs e)
{
richTextBoxNote.Select(richTextBoxNote.SelectionStart, richTextBoxNote.SelectionLength);
richTextBoxNote.SelectionColor = labelPurle.BackColor;
}
//设置字体大小
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
richTextBoxNote.Select(richTextBoxNote.SelectionStart, richTextBoxNote.SelectionLength);
richTextBoxNote.SelectionFont = new Font(richTextBoxNote.SelectionFont.SystemFontName, 20);
}
private void radioButton5_CheckedChanged(object sender, EventArgs e)
{
richTextBoxNote.Select(richTextBoxNote.SelectionStart, richTextBoxNote.SelectionLength);
richTextBoxNote.SelectionFont = new Font(richTextBoxNote.SelectionFont.SystemFontName, 30);
}
private void radioButton6_CheckedChanged(object sender, EventArgs e)
{
richTextBoxNote.Select(richTextBoxNote.SelectionStart, richTextBoxNote.SelectionLength);
richTextBoxNote.SelectionFont = new Font(richTextBoxNote.SelectionFont.SystemFontName, 40);
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBoxNote.Text = "";
label1.Text = "";
this.Text = "";
}
private void ToolStripMenuItemBold_Click(object sender, EventArgs e)
{
if (richTextBoxNote.SelectionFont.Bold == false)
{
//点击按钮加粗,加粗时按钮取消加粗
richTextBoxNote.SelectionFont = new Font(richTextBoxNote.SelectionFont, FontStyle.Bold);
}
else
{
richTextBoxNote.SelectionFont = new Font(richTextBoxNote.SelectionFont, FontStyle.Regular);
}
}
//保存文档,需先拖拽savedialog控件
private void toolStripMenuItemSave_Click(object sender, EventArgs e)
{
if (richTextBoxNote.Text.Trim() == "") //==null没有反应,Tirm去掉空格,输入空格的时候也不能保存
{
MessageBox.Show("空文档不能保存 ", "信息提示", MessageBoxButtons.OKCancel);
}
else
{
if (this.Text == "")
{
//只能存为txt文件
//新建一个对话框
saveFileDialog1.Filter = ("文本文档(*.txt)|*.txt");
//判断用户点击的是保存按钮还是取消按钮
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//保存文件到指定的目录
//获取用户选择的文件及路径
string path = saveFileDialog1.FileName;
this.Text = path;
//保存文件到指定路径
StreamWriter sw = new StreamWriter(path, false); //在指定路径就会建立一个path名的空白文件
sw.WriteLine(richTextBoxNote.Text); //写完这条后,内容没有写到文件中,而是缓冲器中;
sw.Flush(); //清理缓冲,释放缓冲器里的内容;
sw.Close(); //关闭streamwriter对像
}
}
else
{
StreamWriter sw = new StreamWriter(this.Text, false); //在指定路径就会建立一个path名的空白文件
sw.WriteLine(richTextBoxNote.Text); //写完这条后,内容没有写到文件中,而是缓冲器中;
sw.Flush(); //清理缓冲,释放缓冲器里的内容;
sw.Close(); //关闭streamwriter对像
}
}
}
//打开文档,需先拖拽openfiledialog控件
private void ToolStripMenuItemOpen_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = ("文本文档(*.txt)|*.txt");
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//获取打开文件的路径
string path = openFileDialog1.FileName;
StreamReader sr = new StreamReader(path, Encoding.UTF8);
string text = sr.ReadToEnd();
this.Text = path;
richTextBoxNote.Text = text;
sr.Close();
}
}
private void richTextBoxNote_TextChanged(object sender, EventArgs e)
{
label1.Text = "*";
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
if (label1.Text == "*")
{
DialogResult dr = MessageBox.Show("文件未保存,是否要关闭", "信息询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == System.Windows.Forms.DialogResult.Yes)
{
this.Dispose();
}
else
{
e.Cancel = true;
}
}
}
}
}
总结的东西:
1.子窗体和父窗体的调用
2.RichTextBox与TextBox重量级与轻量级的区别吧,大炮打蚊子也不是不可以,但是手枪打坦克,却是很艰难。(没有使用需求,TextBox相对占用资源少)。其它一些 使用TextBox控件换行的转义符为“\r\n”,但是换成RichTextBox控件的话,换行的转义符变为“\n”。
3.几个控件的使用:
3.1 ToolStrip和MenuStrip实际上是相同的控件,因为ToolStrip直接有MenuStrip派生的。也就是说ToolStrip可以做的工作,MenuStrip也能完成。相当于那种子父关系。但是MenuStrip 控件支持多文档界面 (MDI) 和菜单合并、工具提示和文本和图像排序和对齐、拖放操作。
ToolStrip的操作项要比MenuStrip的操作项多。实际应用中,MenuStrip更多的使用在那种菜单项比较多,而ToolStrip则很多时候一般都应用在那些进行增删改查导入导出打印等等的菜单项。
3.2 statustrip 状态栏的使用 可用于表示系统的状态
使用例子: this.toolStripStatusLabel2.Text = DateTime.Now.ToShortDateString();//在任务栏上显示系统的当前日期
3.3 FontDialog和ColorDialog的使用
3.4 savedialog和opendialog的使用
streamwrite,streamread,ReadToEnd();的使用
各种文件的调用,保存。
更多推荐
简单记事本
发布评论