Form1.cs
资源名称:Notebook.rar [点击查看]
上传用户:hueiseng
上传日期:2022-07-26
资源大小:156k
文件大小:16k
源码类别:
C#编程
开发平台:
Visual C++
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace 笔记本
- {
- public partial class Form1 : Form
- {
- public string check;
- public Form1()
- {
- InitializeComponent();
- toolStripButton1.Tag = 新建ToolStripMenuItem;
- toolStripButton2.Tag = 打开ToolStripMenuItem;
- }
- private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (text != richTextBox1.Text)
- {
- if (richTextBox1.Text.Length > 0)
- {
- DialogResult result = MessageBox.Show("文本内容己改变,想保存文件吗?", "提示信息", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
- if (result == DialogResult.Yes)
- {
- if (path == null || openFileDialog1.ReadOnlyChecked)
- {
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
- richTextBox1.Text = "";
- }
- }
- else
- {
- richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
- richTextBox1.Text = "";
- }
- }
- if (result == DialogResult.No)
- richTextBox1.Text = "";
- }
- }
- }
- private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
- }
- }
- private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- saveFileDialog1.DefaultExt = "*.rtf";
- saveFileDialog1.Filter = "RTF Files|*.rtf";
- if (path == null || openFileDialog1.ReadOnlyChecked)
- {
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
- }
- }
- else
- {
- richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
- }
- }
- private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- saveFileDialog1.ShowDialog();
- // saveFileDialog1.DefaultExt = "*.rtf";
- //saveFileDialog1.Filter = "RTF Files|*.rtf";
- if (richTextBox1.Text.Length == 0)
- {
- DialogResult result = MessageBox.Show("文本内为空想保存文件吗?", "提示信息", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
- if (result == DialogResult.Yes)
- {
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
- richTextBox1.Text = "";
- }
- }
- }
- else
- {
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
- richTextBox1.Text = "";
- }
- }
- }
- private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- pageSetupDialog1.Document = printDocument1;
- pageSetupDialog1.ShowDialog();
- }
- private void 打印ToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- pageSetupDialog1.Document = printDocument1;
- printDocument1.Print();
- }
- private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (richTextBox1.Text!= null)
- {
- string now = richTextBox1.Text;
- if (path != null)
- {
- richTextBox1.Clear();
- richTextBox1.LoadFile(path, RichTextBoxStreamType.PlainText);
- }
- if (path == null ||openFileDialog1.ReadOnlyChecked || (path != null && richTextBox1.Text != now))
- {
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- richTextBox1.Clear();
- richTextBox1.Text = now;
- richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
- richTextBox1.Text = "";
- }
- }
- }
- Close();
- }
- private void 撤ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- richTextBox1.Undo();
- }
- private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (richTextBox1.SelectedText != null)
- {
- 剪切ToolStripMenuItem.Enabled = true;
- richTextBox1.Cut();
- }
- }
- private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (richTextBox1.SelectedText != null)
- {
- richTextBox1.Copy();
- }
- }
- private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (richTextBox1.SelectedText != null)
- {
- richTextBox1.Copy();
- }
- }
- private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- richTextBox1.SelectedText = "";
- }
- private void 编辑ToolStripMenuItem_Click_1(object sender, EventArgs e)
- {
- 剪切ToolStripMenuItem.Enabled = false;
- if (richTextBox1.SelectedText.Length > 0)
- {
- 剪切ToolStripMenuItem.Enabled = true;
- }
- else
- {
- 剪切ToolStripMenuItem.Enabled = false;
- }
- 复制ToolStripMenuItem.Enabled = false;
- if (richTextBox1.SelectedText.Length > 0)
- {
- 复制ToolStripMenuItem.Enabled = true;
- }
- else
- {
- 复制ToolStripMenuItem.Enabled = false;
- }
- 删除ToolStripMenuItem.Enabled = false;
- if (richTextBox1.SelectedText.Length > 0)
- {
- 删除ToolStripMenuItem.Enabled = true;
- }
- else
- {
- 删除ToolStripMenuItem.Enabled = false;
- }
- 查找ToolStripMenuItem1.Enabled = false;
- if (richTextBox1.Text.Length > 0)
- {
- 查找ToolStripMenuItem1.Enabled = true;
- }
- else
- {
- 查找ToolStripMenuItem1.Enabled = false;
- }
- }
- private void 打印预览ToolStripMenuItem_Click_1(object sender, EventArgs e)
- {
- pageSetupDialog1.Document = printDocument1;
- printPreviewDialog1.ShowDialog();
- }
- private void 查找下一个ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (richTextBox1.SelectedText != null)
- {
- int findplace=richTextBox1.SelectionStart+richTextBox1.SelectedText.Length;
- findplace = richTextBox1.Text.IndexOf(richTextBox1.SelectedText,findplace);
- if (findplace == -1)
- MessageBox.Show("没有搜索项");
- else
- richTextBox1.Select(findplace, richTextBox1.SelectedText.Length);
- }
- else
- {
- 查找ToolStripMenuItem1_Click(sender, e);
- }
- }
- private void 查找ToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- if (richTextBox1.Text.Length > 0)
- {
- 查找ToolStripMenuItem1.Enabled = true;
- 查找 show = new 查找();
- show.Owner = this;
- show.ShowDialog();
- }
- else
- 查找ToolStripMenuItem1.Enabled = false;
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- toolStripStatusLabel1.Text = DateTime.Now.ToString();
- int line = this.richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart);
- int col;
- int start=0;
- int cursor = richTextBox1.SelectionStart;
- while (start < cursor)
- {
- if (line == this.richTextBox1.GetLineFromCharIndex(start))
- {
- break;
- }
- else
- start++;
- }
- col = cursor - start;
- line++;
- col++;
- toolStripStatusLabel2.Text = "行" + line.ToString() + "列" + col.ToString();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- timer1.Start();
- }
- private void 替换ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (richTextBox1.Text.Length > 0)
- {
- 替换ToolStripMenuItem.Enabled = true;
- 替换 show = new 替换();
- show.Owner = this;
- show.ShowDialog();
- }
- }
- private void 转到ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- 转到ToolStripMenuItem.Enabled = true;
- 转到 show = new 转到();
- show.Owner = this;
- show.ShowDialog();
- }
- private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (richTextBox1.Text != null)
- richTextBox1.SelectAll();
- 全选ToolStripMenuItem.Enabled=false;
- }
- private void 时间日期ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- richTextBox1.Text += toolStripStatusLabel1.Text;
- }
- private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if(richTextBox1 .WordWrap==false)
- {
- richTextBox1.WordWrap = true;
- 自动换行ToolStripMenuItem.Text="√自动换行";
- }
- else
- {
- richTextBox1.WordWrap =false;
- 自动换行ToolStripMenuItem.Text ="自动换行";
- }
- }
- private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- fontDialog1.ShowColor = true;
- if (fontDialog1.ShowDialog() != DialogResult.Cancel)
- {
- richTextBox1.Font = fontDialog1.Font;
- richTextBox1.ForeColor = fontDialog1.Color;
- }
- }
- private void 状态栏ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (statusStrip1.Visible == false)
- {
- statusStrip1.Visible = true;
- 状态栏ToolStripMenuItem.Text = "√状态栏";
- }
- else
- {
- statusStrip1.Visible = false;
- 状态栏ToolStripMenuItem.Text = "状态栏";
- }
- }
- private void 关于记事本ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- 主题 show = new 主题();
- show.Show();
- }
- private void Form1_MouseDown(object sender, MouseEventArgs e)
- {
- }
- private void 撤消撤消ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- richTextBox1.Undo();
- }
- private void 剪切ToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- 剪切ToolStripMenuItem_Click(sender, e);
- }
- private void 复制ToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- 复制ToolStripMenuItem_Click(sender,e);
- }
- private void 粘贴ToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- 粘贴ToolStripMenuItem_Click( sender,e);
- }
- private void 删除ToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- 删除ToolStripMenuItem_Click(sender, e);
- }
- private void 全选ToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- 全选ToolStripMenuItem_Click(sender, e);
- }
- private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
- {
- if (MouseButtons == MouseButtons.Right)
- {
- int x = MousePosition.X;
- int y = MousePosition.Y;
- contextMenuStrip1.Show(x, y);
- }
- }
- private void toolStripButton1_Click(object sender, EventArgs e)
- {
- 新建ToolStripMenuItem_Click(sender,e);
- }
- private void toolStripButton2_Click(object sender, EventArgs e)
- {
- 打开ToolStripMenuItem_Click(sender, e);
- }
- private void toolStripButton3_Click(object sender, EventArgs e)
- {
- 保存ToolStripMenuItem_Click(sender, e);
- }
- private void toolStripButton7_Click(object sender, EventArgs e)
- {
- 打印ToolStripMenuItem1_Click(sender, e);
- }
- private void toolStripButton8_Click(object sender, EventArgs e)
- {
- 查找ToolStripMenuItem1_Click(sender, e);
- }
- private void toolStripButton4_Click(object sender, EventArgs e)
- {
- 复制ToolStripMenuItem_Click(sender, e);
- }
- private void toolStripButton5_Click(object sender, EventArgs e)
- {
- 剪切ToolStripMenuItem_Click(sender, e);
- }
- private void toolStripButton6_Click(object sender, EventArgs e)
- {
- 粘贴ToolStripMenuItem_Click(sender, e);
- }
- private void 关于记事本ToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- HELP show = new HELP();
- show.Show();
- }
- private void 图片ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
- if (openFileDialog2.ShowDialog ()== DialogResult.OK)
- {
- pictureBox1.Load(openFileDialog2.FileName);
- Point a = new Point();
- a.X = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart).X;
- a.Y= richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart).Y+49;
- pictureBox1.Location = a;
- pictureBox1.Visible = true;
- richTextBox1.Focus();
- richTextBox1.SelectionStart = richTextBox1.GetCharFromPosition(a);
- }
- }
- }
- }