Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i have a Rich_textbox and two text_box and a button, i want when i write any thing in rich_textbox and write a word in text_box1 and click button, find the word in rich_textbox.
and when find it replace whit the text in text_box2
plz help.
sorry for my english
Posted

for Find text

Quote:
private void button1_Click(object sender, EventArgs e)
{
string str = richTextBox1.Text;
if (str.Contains(textBox1.Text))
{
label1.Text = "True";
}
else
{
label1.Text = "False";
}
}


for replace text

str.Replace(Textbox1.text,textbox2.text)



I hope This code really help u

Regard
Sham
:)
 
Share this answer
 
You should use this code for the event handler of the button:
C#
private void button1_Click(object sender, EventArgs e)
{
   string strTextToFind = TextBox1.Text;
   string strTextToReplace = textBox2.Text;
   richTextBox1.Text.Replace(strTextToFind, strTextToReplace);
}

Basically the String.Replace method searches for the string (strTextToFind) and when it finds it, it replaces it with another string (strTextToReplace).
 
Share this answer
 
Comments
JANARDHAN GURRAM 4-Oct-12 8:29am    
5ed,good answer in 3 lines :-)
Hi Amir,

Please add below code in your button click event method.


string str1= RichTextbox.Text;

int loc = str1.IndexOf(TextBox1.Text);

if(loc == -1)
{
return;
}
else
{
str1.replace(Textbox1.Text, Textbox2.Text)
}
RichTextBox.Text = str1;
 
Share this answer
 
v3
Comments
amirmohamad 4-Oct-12 6:36am    
tank's for help
and how i can find it?
Mohd. Mukhtar 4-Oct-12 6:40am    
int loc = str1.IndexOf(TextBox1.Text);

in this line we are checking that any occurance of that Textbox1 is avail into the rich text or not.

if loc will give positive result then we replace those text in else block.
Use string's Replace method.
C#
txtRichTextBox.Text.Replace(oldWord,newWord);
 
Share this answer
 
Comments
amirmohamad 4-Oct-12 6:18am    
How i can Find it?
and after find replace it
Oshtri Deka 4-Oct-12 11:01am    
Why don't you read about Replace method? I've supplied you a link.

In short Replace method finds ALL references of particular string and replaces it with with another. It's simple as that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900