记事本源码

本文详细介绍了使用记事本进行文本编辑的各种增强功能与操作,包括复制、剪切、粘贴、保存、打开、新建等基本操作,以及自定义字体大小、颜色和样式,实现自动换行等功能。此外,还提供了文件保存与加载、打印功能的实现方法,并通过代码实例展示了如何集成额外的交互元素如工具栏和状态栏,以及如何利用各种控件如颜色对话框和字体对话框来丰富编辑体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.Drawing.Printing;
namespace 记事本
{
  public partial class Form1 : Form//可以更简单一些,因为有些操作是重复了的//
  {
  public Form1()
  {
  InitializeComponent();
  }


  private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
  {
  richTextBox1.Copy();
  }


  private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
  {
  richTextBox1.Paste();
  }


  private void 保存SToolStripButton_Click(object sender, EventArgs e)
  {
  saveFileDialog1.Filter = "文本文档.TXT|*.txt|.rtf文档|*.rtf";
  saveFileDialog1.ShowDialog();
  MessageBox.Show(saveFileDialog1.FileName);
  try
  {
  if(saveFileDialog1 .ShowDialog ()==DialogResult .OK )
  richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
  }
  catch(Exception ex)
  {
  MessageBox.Show(ex .Message );
  }
  }


  private void 打开OToolStripButton_Click(object sender, EventArgs e)
  {
  openFileDialog1.Filter = "文本文档.TXT|*.txt|.rtf文档|*.rtf";// Filter 文件筛选器//
  openFileDialog1.ShowDialog();
  MessageBox.Show(openFileDialog1 .FileName );
  try
  {  
  if(openFileDialog1 .ShowDialog ()==DialogResult .OK )
  richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText);
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message);
  }
  }


  private void Form1_Load(object sender, EventArgs e)
  {
  for (int i = 8; i < 78; i++)
  {
  toolStripComboBox1.Items.Add(i);
  }


}


  private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
  {
  richTextBox1.Text = "";
  }


private void 新建NToolStripButton_Click(object sender, EventArgs e)
  {
  richTextBox1.Text = "";
  }


  private void 剪切UToolStripButton_Click(object sender, EventArgs e)
  {
  richTextBox1.Cut();
  }


  private void 复制CToolStripButton_Click(object sender, EventArgs e)
  {
  richTextBox1.Copy();
  }


  private void 粘贴PToolStripButton_Click(object sender, EventArgs e)
  {
  richTextBox1.Paste();
  }


  private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
  {
  openFileDialog1.Filter = "文本文档.TXT|*.txt|.rtf文档|*.rtf";// Filter 文件筛选器//
  openFileDialog1.ShowDialog();
  MessageBox.Show(openFileDialog1.FileName);
  try
  {
  if(openFileDialog1 .ShowDialog ()==DialogResult .OK )
  richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText);
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message);
  }
  }


  private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
  {
  saveFileDialog1.Filter = "文本文档.TXT|*.txt|.rtf文档|*.rtf";
  saveFileDialog1.ShowDialog();
  MessageBox.Show(saveFileDialog1.FileName);
  try
  {  
  if(saveFileDialog1 .ShowDialog ()==DialogResult .OK )
  richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message);
  }
  }


  private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
  {
  richTextBox1.Paste();
  }


  private void toolStripComboBox1_TextChanged(object sender, EventArgs e)
  {
  float f = Convert.ToSingle(toolStripComboBox1.SelectedItem.ToString());
  richTextBox1.SelectionFont = new Font("字号", f);
  }


  private void 红色ToolStripMenuItem_Click(object sender, EventArgs e)
  {
    
  richTextBox1 .SelectionColor = Color.Red;
  }


  private void 绿色ToolStripMenuItem_Click(object sender, EventArgs e)
  {
  richTextBox1.SelectionColor = Color.Green;
  }


  private void 更多ToolStripMenuItem_Click(object sender, EventArgs e)
  {
  colorDialog1.ShowDialog();
  if(colorDialog1 .ShowDialog ()== DialogResult .OK)
  richTextBox1.SelectionColor = colorDialog1.Color;
  }


  private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
  {
  saveFileDialog1.Filter = "文本文档.TXT|*.txt|.rtf文档|*.rtf";
  saveFileDialog1.ShowDialog();
  MessageBox.Show(saveFileDialog1.FileName);
  try
  {
  if(saveFileDialog1 .ShowDialog() ==DialogResult .OK )
  richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message);
  }
  }


  private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
  {
  printDocument1.Print();
  }


  private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
  {
  e.Graphics.DrawString(richTextBox1 .Text ,new Font("宋体",15),Brushes .Green ,20,20);
  }


  private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
  {
  AboutBox1 ab = new AboutBox1();
  ab.Show();
  }


  private void timer1_Tick(object sender, EventArgs e)
  {
  toolStripStatusLabel1.Text = "系统时间:" + DateTime.Now.ToLocalTime();
  progressBar1.PerformStep();
  }


  private void 工具栏ToolStripMenuItem_Click(object sender, EventArgs e)
  {
  if (工具栏ToolStripMenuItem.Checked == true)
  {
  工具栏ToolStripMenuItem.Checked = false;
  toolStrip1.Visible = false;
  }
  else
  {
  工具栏ToolStripMenuItem.Checked = true ;
  toolStrip1.Visible = true ;
  }


  }


  private void 状态栏ToolStripMenuItem_Click(object sender, EventArgs e)
  {
  if (状态栏ToolStripMenuItem.Checked == true)
  {
  状态栏ToolStripMenuItem.Checked = false;
  statusStrip1.Visible = false;
  }
  else
  {
  状态栏ToolStripMenuItem.Checked = true ;
  statusStrip1.Visible = true;
  }
  }


  private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
  {


  fontDialog1.ShowDialog();
  fontDialog1.ShowColor = true;
  if (fontDialog1.ShowDialog() == DialogResult.OK)
  {
  richTextBox1.SelectionFont = fontDialog1.Font;
  richTextBox1.SelectionColor = fontDialog1.Color;
  }
  }


  private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
  {
  if (自动换行ToolStripMenuItem.Checked == false)
  {
  自动换行ToolStripMenuItem.Checked = true;
  richTextBox1.WordWrap = true;
  }
  else
  {
  自动换行ToolStripMenuItem.Checked =false ;
  richTextBox1.WordWrap = false;
  }
  }


}
}
//要想用运行这些代码,必须要添加必要的控件哦//
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值