Replace text in RichTextBox
I am creating a word pad style text editor & I am having problems trying to replace text.
Rather than having a separate form for this I have created a toolstrip with a find textbox (tstxtSearchText), a find button (tsbtnFind), a replace text box (tstxtReplaceText) & a replace button (tsbtnReplace). My find function works fine but I cannot figure out replace.
Here is my find code:
private void tsbtnFind_Click(object sender, EventArgs e)
{
int length = txtMain.TextLength;
int index = 0;
int lastIndex = txtMain.Text.LastIndexOf(this.tstxtSearchText.Text);
while (index < lastIndex)
{
txtMain.Find(tstxtSearchText.Text, index, length, RichTextBoxFinds.None);
txtMain.SelectionBackColor = Color.Yellow;
index = txtMain.Text.IndexOf(tstxtSearchText.Text, index) + 1;
}
}
I have tried the following code for the Replace click event:
private void tsbuttonReplace_Click(object sender, EventArgs e)
{
if (tstxtReplaceText.SelectionLength > 0)
{
int start = tstxtReplaceText.SelectionStart;
int len = tstxtReplaceText.SelectionLength;
tstxtReplaceText.Text = tstxtSearchText.Text.Remove(start, len);
txtMain.Text = tstxtReplaceText.Text.Insert(start, tstxtReplaceText.Text);
txtMain.Focus();
}
}
* txtMain is the text editor of the form.
This appears to do nothing. If anyone can see where I am going any help would be greatly appreciated. Thanks in advance.