C# 记事本查找替换 新手 请高人多多指教
来源:互联网 发布:网络信息发布制度 编辑:程序博客网 时间:2024/06/04 18:20
//主窗体部分
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;
using System.Drawing.Printing;
namespace notepad
{
public partial class Form1 : Form
{
public string path="";
public string wenben;
public bool saved;
System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
public Form1()
{
InitializeComponent();
// richTextBox1.Size.Width = this.Size.Width;
// richTextBox1.Size.Height = this.Size.Height;
saved = false ;
this.Paint+=new PaintEventHandler(Form1_Paint);
this.FormClosing+=new FormClosingEventHandler(Form1_FormClosing);
//richTextBox1.b;
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
wenben = richTextBox1.Text;
}
private void Form1_Load(object sender, EventArgs e)
{
this.toolStripStatusLabel1.Text = "当前时间为 "+DateTime.Now.ToShortTimeString();
this.toolStripStatusLabel2.Text= "20082121003 鲍东升制作";
this.toolStripStatusLabel3.Text = " 软件综合课程设计";
printPreviewControl1.Document = pd;
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*.txt(文本文档)|*.txt";
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
StreamReader SReader=new StreamReader(openFileDialog1.FileName,Encoding.Default);
richTextBox1.Text = SReader.ReadToEnd();
// richTextBox1.Font = SReader.;
SReader.Close();
path = openFileDialog1.FileName;
}
this.Text = openFileDialog1.SafeFileName;
// richTextBox1.LoadFile(path);
// MessageBox.Show(path);
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "*.txt(文本文档)|*.txt";
if(saveFileDialog1.ShowDialog()==DialogResult.OK)
{
StreamWriter SWriter = new StreamWriter(saveFileDialog1.FileName, true);
SWriter.Write(richTextBox1.Text);
SWriter.Close();
}
saved = true;
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.DefaultExt = "*.txt";
// RichTextBoxStreamType = "*.txt";
if (path.Length > 0)
{
richTextBox1.SaveFile(path, RichTextBoxStreamType.TextTextOleObjs);
// MessageBox.Show(path);
}
else
{
saveFileDialog1.Filter = "*.txt(文本文档)|*.txt";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamWriter SWriter = new StreamWriter(saveFileDialog1.FileName, true);
SWriter.Write(richTextBox1.Text);
SWriter.Close();
}
}
saved = true;
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
}
private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
pageSetupDialog1.Document = pd;
this.pageSetupDialog1.AllowMargins = true;
this.pageSetupDialog1.AllowOrientation = true;
this.pageSetupDialog1.AllowPaper = true;
this.pageSetupDialog1.AllowPrinter = true;
this.pageSetupDialog1.ShowDialog();
}
private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
{
printDialog1.Document = pd;
// e.Graphics.DrawString(path, Brushes.Black, 10, 10);
printDialog1.AllowPrintToFile = true;
printDialog1.AllowCurrentPage = true;
printDialog1.AllowSelection = true;
printDialog1.AllowSomePages = true;
printDialog1.ShowDialog();
}
private void 编辑_Click(object sender, EventArgs e)
{
}
private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
{
if(richTextBox1.CanUndo==true)
{
richTextBox1.Undo();
richTextBox1.ClearUndo();
}
}
private void 重复RToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Redo();
}
private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectionLength != 0)
richTextBox1.Cut();
}
private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
{
if(Clipboard.GetText().ToString()!="")
richTextBox1.Paste();
}
private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.SelectAll();
}
private void 自定义CToolStripMenuItem_Click(object sender, EventArgs e)
{
fontDialog1.AllowVectorFonts = true;
fontDialog1.AllowVerticalFonts = true;
fontDialog1.FixedPitchOnly = true;
fontDialog1.MaxSize = 72;
fontDialog1.MinSize = 5;
if(fontDialog1.ShowDialog()==DialogResult.OK)
{
if (richTextBox1.SelectedText == "")
richTextBox1.SelectAll();
richTextBox1.SelectionFont = fontDialog1.Font;
}
// richTextBox1.Text+=fontDialog1.Tag.ToString();
}
private void 选项OToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.AllowFullOpen = true;
colorDialog1.AnyColor = true;
colorDialog1.SolidColorOnly = true;
if(colorDialog1.ShowDialog()==DialogResult.OK)
{
if (richTextBox1.SelectedText== "")
richTextBox1.SelectAll();
richTextBox1.SelectionColor = colorDialog1.Color;
}
}
private void 状态栏ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 工具TToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
{
if(richTextBox1.SelectionLength!=0)
richTextBox1.Copy();
}
private void 查找ToolStripMenuItem1_Click(object sender, EventArgs e)
{
}
private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
{
Search s=new Search();
s.Owner = this;
s.Show();
}
private void 编辑EToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Modified == false)
{//有修改
撤消UToolStripMenuItem.Enabled = false;
重复RToolStripMenuItem.Enabled = false;
}
else
{
撤消UToolStripMenuItem.Enabled = true;
重复RToolStripMenuItem.Enabled = true;
}
if(richTextBox1.SelectedText=="")
{
剪切TToolStripMenuItem.Enabled = false;
复制CToolStripMenuItem.Enabled = false;
粘贴PToolStripMenuItem.Enabled = false;
}
else
{
剪切TToolStripMenuItem.Enabled = true;
复制CToolStripMenuItem.Enabled = true;
粘贴PToolStripMenuItem.Enabled = true;
}
if(richTextBox1.Text=="")
{
查找ToolStripMenuItem.Enabled = false;
}
else
{
查找ToolStripMenuItem.Enabled = true;
}
if(Clipboard.GetText().ToString()=="")
{
粘贴PToolStripMenuItem.Enabled = false;
}
else
粘贴PToolStripMenuItem.Enabled = true;
if (richTextBox1.Text != "")
{
全选AToolStripMenuItem.Enabled = true;
}
else
{
全选AToolStripMenuItem.Enabled = false;
}
}
private void 时间日期ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Text += DateTime.Now.ToString();
}
private void 替换ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, EventArgs e)
{
// richTextBox1. = this.Size.Width;
// richTextBox1.Size.Height = this.Size.Height;
}
private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.CanUndo == true)
{
richTextBox1.Undo();
richTextBox1.ClearUndo();
}
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
if (richTextBox1.Modified == false)
撤销ToolStripMenuItem.Enabled = false;
else
撤销ToolStripMenuItem.Enabled = true;
if (richTextBox1.SelectedText == "")
{
剪切ToolStripMenuItem.Enabled = false;
复制ToolStripMenuItem.Enabled = false;
粘贴ToolStripMenuItem.Enabled = false;
删除ToolStripMenuItem.Enabled = false;
}
else
{
剪切ToolStripMenuItem.Enabled = true;
复制ToolStripMenuItem.Enabled = true;
粘贴ToolStripMenuItem.Enabled = true;
删除ToolStripMenuItem.Enabled = true;
}
if(richTextBox1.Text!="")
{
全选ToolStripMenuItem.Enabled = true;
}
else
{
全选ToolStripMenuItem.Enabled = false;
}
}
private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectionLength != 0)
richTextBox1.Cut();
}
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectionLength != 0)
richTextBox1.Copy();
}
private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Clipboard.GetText().ToString() != "")
richTextBox1.Paste();
}
private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.SelectAll();
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.SelectedText = "";
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_FormClosing(object sender, EventArgs e)
{
if(saved==false)
{
DialogResult dr;
dr = MessageBox.Show("文件"+path+"的文字已改变。想保存文件吗?", "记事本", MessageBoxButtons.YesNoCancel);
if(dr==DialogResult.Yes)
{
// MessageBox.Show("S");
if(path=="")
{
另存为ToolStripMenuItem_Click(sender, e);
}
else
{
保存ToolStripMenuItem_Click(sender, e);
}
}
if(dr==DialogResult.Cancel)
{
((CancelEventArgs)e).Cancel = true;
}
}
}
private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
{
about info = new about();
info.Show();
}
private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = this.pd;
//pd.PrintPage += new PrintPageEventHandler();
printPreviewDialog1.ShowDialog();
}
private void toolStripStatusLabel3_Click(object sender, EventArgs e)
{
}
}
}
//查找替换 窗体
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 notepad;
namespace notepad
{
public partial class Search : Form
{
int weizhi;
public Search()
{
InitializeComponent();
weizhi = 0;
}
private void button1_Click(object sender, EventArgs e)
{
int index;
Form1 frm1 = this.Owner as Form1;
index=frm1.richTextBox1.Text.IndexOf(textBox1.Text, weizhi);
if(index!=-1)
{
frm1.richTextBox1.Select(index, textBox1.Text.Length);
frm1.richTextBox1.Focus();
weizhi = index+1;
// if (weizhi > frm1.richTextBox1.Text.Length)
// weizhi = 0;
}
else
{
MessageBox.Show("无法找到 " + textBox1.Text);
weizhi = 0;
}
}
private void button2_Click(object sender, EventArgs e)
{
int index;
Form1 frm1 = this.Owner as Form1;
index = frm1.richTextBox1.Text.IndexOf(textBox1.Text, weizhi);
if (index != -1)
{
// frm1.richTextBox1.Text.Replace("b", "a");
frm1.richTextBox1.Select(index, textBox1.Text.Length);
frm1.richTextBox1.SelectedText = textBox2.Text;
frm1.richTextBox1.Focus();
weizhi = index +textBox2.Text.Length;
}
else
{
MessageBox.Show("无法找到 " + textBox1.Text);
weizhi = 0;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
weizhi = 0;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
weizhi = 0;
}
private void Search_Load(object sender, EventArgs e)
{
}
}
}
- C# 记事本查找替换 新手 请高人多多指教
- 新手上路,请多多指教
- 我是新手,请多多指教!
- 新手上路,请多多指教
- 我是新手,请大家多多指教
- 我是新手请大家多多指教
- 本人新手,请大家多多指教
- 请高人指教
- 新手上路多多指教
- 我是新手,多多指教
- 新手来到 多多指教
- 新手上路,多多指教!
- 初来乍到,请多多指教!
- 请多多指教
- 请多多指教
- 请多多指教
- 请大家多多指教!
- 请多多指教啊!
- 基于Xlib(X库)的开发
- VS2005打包图解
- JDBC 插入时, 取得插入的主键的返回值
- VB.NET中退出确认的实现
- 帮HR招到好员工
- C# 记事本查找替换 新手 请高人多多指教
- Mapxtreme Java 地图缩放,平移操作的原理和实现
- 为什么我的积分是82分,可下载的时候只显示只有2分?谁告诉我是怎么回事啊?
- 初学者如何开发出高质量J2EE系统
- C/C++——字符串和字符数组
- PHP程序员的参考书籍
- 查看和修改系统最大文件描述符
- 获取本机IP和获取公网IP
- Android全屏设置代码