C#中文本框内容的查找替换功能
3个文本框一个按钮TextBox1是输入要查找的内容TextBox2是输入要替换的内容TextBox3是可以自己输入的文本问题:能够分别实现查找和替换功能求高手帮忙给个代...
3个文本框 一个按钮
TextBox1是输入要查找的内容
TextBox2是输入要替换的内容
TextBox3是可以自己输入的文本
问题:能够分别实现查找和替换功能
求高手帮忙给个代码 展开
TextBox1是输入要查找的内容
TextBox2是输入要替换的内容
TextBox3是可以自己输入的文本
问题:能够分别实现查找和替换功能
求高手帮忙给个代码 展开
4个回答
展开全部
不可能给你整个源码了
思路是这样的
定义两个字符串
然后把2中内容给了1
思路是这样的
定义两个字符串
然后把2中内容给了1
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//查找
private void button1_Click(object sender, EventArgs e)
{
int index;
if (textBox3.SelectionLength!=0)
index = textBox3.SelectionStart + textBox3.SelectionLength - 1;
else
index = 0;
index = textBox3.Text.IndexOf(textBox1.Text, index);
if (index != -1)
{
textBox3.Focus();
textBox3.SelectionStart = index;
textBox3.SelectionLength = textBox1.Text.Length;
index = textBox3.SelectionStart + textBox3.SelectionLength - 1;
}
else
{
textBox3.SelectionStart = 0;
textBox3.SelectionLength = 0;
MessageBox.Show("查找完成!");
}
}
//替换
private void button1_Click(object sender, EventArgs e)
{
int index;
if (textBox3.SelectionLength!=0)
index = textBox3.SelectionStart + textBox3.SelectionLength - 1;
else
index = 0;
index = textBox3.Text.IndexOf(textBox1.Text, index);
if (index != -1)
{
textBox3.Focus();
textBox3.Text = String.Concat(textBox3.Text.Substring(0, index), textBox2.Text, textBox3.Text.Substring(index + textBox1.Text.Length, textBox3.Text.Length - index - textBox1.Text.Length));
textBox3.SelectionStart = index;
textBox3.SelectionLength = textBox2.Text.Length;
index = textBox3.SelectionStart + textBox3.SelectionLength - 1;
}
else
{
textBox3.SelectionStart = 0;
textBox3.SelectionLength = 0;
MessageBox.Show("替换完成!");
}
}
我是依次替换相同的内容的,希望符合你的要求~~~
替换与查找相比只是多了
textBox3.Text = String.Concat(textBox3.Text.Substring(0, index), textBox2.Text, textBox3.Text.Substring(index + textBox1.Text.Length, textBox3.Text.Length - index - textBox1.Text.Length));
这一句,以及将查找中的textBox3.SelectionLength = textBox1.Text.Length替换为了textBox3.SelectionLength = textBox2.Text.Length
private void button1_Click(object sender, EventArgs e)
{
int index;
if (textBox3.SelectionLength!=0)
index = textBox3.SelectionStart + textBox3.SelectionLength - 1;
else
index = 0;
index = textBox3.Text.IndexOf(textBox1.Text, index);
if (index != -1)
{
textBox3.Focus();
textBox3.SelectionStart = index;
textBox3.SelectionLength = textBox1.Text.Length;
index = textBox3.SelectionStart + textBox3.SelectionLength - 1;
}
else
{
textBox3.SelectionStart = 0;
textBox3.SelectionLength = 0;
MessageBox.Show("查找完成!");
}
}
//替换
private void button1_Click(object sender, EventArgs e)
{
int index;
if (textBox3.SelectionLength!=0)
index = textBox3.SelectionStart + textBox3.SelectionLength - 1;
else
index = 0;
index = textBox3.Text.IndexOf(textBox1.Text, index);
if (index != -1)
{
textBox3.Focus();
textBox3.Text = String.Concat(textBox3.Text.Substring(0, index), textBox2.Text, textBox3.Text.Substring(index + textBox1.Text.Length, textBox3.Text.Length - index - textBox1.Text.Length));
textBox3.SelectionStart = index;
textBox3.SelectionLength = textBox2.Text.Length;
index = textBox3.SelectionStart + textBox3.SelectionLength - 1;
}
else
{
textBox3.SelectionStart = 0;
textBox3.SelectionLength = 0;
MessageBox.Show("替换完成!");
}
}
我是依次替换相同的内容的,希望符合你的要求~~~
替换与查找相比只是多了
textBox3.Text = String.Concat(textBox3.Text.Substring(0, index), textBox2.Text, textBox3.Text.Substring(index + textBox1.Text.Length, textBox3.Text.Length - index - textBox1.Text.Length));
这一句,以及将查找中的textBox3.SelectionLength = textBox1.Text.Length替换为了textBox3.SelectionLength = textBox2.Text.Length
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication15
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//private void btnFind_Click(object sender, EventArgs e)
//{
// int i = textBox3.Text.IndexOf(textBox1.Text);
// if (i == -1)
// MessageBox.Show("不存在");
// else
// MessageBox.Show("在第" + i.ToString() + "位");
//}
private void btnChange_Click(object sender, EventArgs e)
{
textBox3.Text= textBox3.Text.Replace(textBox1.Text, textBox2.Text);
}
}
}
楼上的 我一句话搞定的事你写那么多 真是很有意思啊
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication15
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//private void btnFind_Click(object sender, EventArgs e)
//{
// int i = textBox3.Text.IndexOf(textBox1.Text);
// if (i == -1)
// MessageBox.Show("不存在");
// else
// MessageBox.Show("在第" + i.ToString() + "位");
//}
private void btnChange_Click(object sender, EventArgs e)
{
textBox3.Text= textBox3.Text.Replace(textBox1.Text, textBox2.Text);
}
}
}
楼上的 我一句话搞定的事你写那么多 真是很有意思啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询