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.
Loading

Guest UserRank #4,868 · 321 reputationPosted Apr 17, 2013, 11:42 AM EDT
private void tsbtnReplace_Click(object sender, EventArgs e)
{
if (tstxtSearchText.Text.Length > 0)
{
txtMain.Text = txtMain.Text.Replace(tstxtSearchText.Text,tstxtReplaceText.Text);
}
}
Guest UserRank #4,868 · 321 reputationPosted Apr 17, 2013, 7:08 AM EDT
Looking at the code...
Assuming the tstxtReplaceText.SelectionLength; property is definitely holding a value greater than 0.
The tstxtReplaceText.SelectionStart property needs to have a value set to say 0 as this is the integer specifying where the point of entry into the textbox will begin.
0 being the first, 1 being the second char etc as it works on a zero based index.
Then moving down to the Remove(); method, once the SelectionStart prop has been assigned a value of say 0 thats the entry point at which it will begin to remove the chars.
Then on the Insert(); method it will pass in the variable start which is holding the value 0 assigned to the SelectionStart property and then replace it with the new text that has been entered into the textbox...
Hope this helps?
Regards
Steven
Stephen Walsh15 reputationPosted Apr 17, 2013, 7:47 AM EDT
private void tsbuttonReplace_Click(object sender, EventArgs e)
{
if (tstxtReplaceText.SelectionLength > 0)
{
tstxtReplaceText.SelectionStart = 0;
int len = tstxtReplaceText.SelectionLength;
tstxtReplaceText.Text = tstxtSearchText.Text.Remove(0, len);
txtMain.Text = tstxtReplaceText.Text.Insert(0, tstxtReplaceText.Text);
txtMain.Focus();
}
}
Guest UserRank #4,868 · 321 reputationPosted Apr 17, 2013, 7:58 AM EDT
According to MSDN the Focus(); Method needs to be implemented before the SelectionStart and SelectionLength Properties like so:-
textBox1.Focus();
textBox1.SelectionStart = textBox1.SelectionStart + 1;
textBox1.SelectionLength = 1;
Id try moving the Method call above the 2 Properties and give that a go...
Regards,
Stephen Walsh15 reputationPosted Apr 17, 2013, 8:37 AM EDT
private void tsbuttonReplace_Click(object sender, EventArgs e)
{
if (tstxtReplaceText.SelectionLength > 0)
{
txtMain.Focus();
txtMain.SelectionStart = txtMain.SelectionStart + 1;
txtMain.SelectionLength = 1;
tstxtReplaceText.SelectionStart = 0;
int len = tstxtReplaceText.SelectionLength;
tstxtReplaceText.Text = tstxtSearchText.Text.Remove(0, len);
txtMain.Text = tstxtReplaceText.Text.Insert(0, tstxtReplaceText.Text);
}
}
This is not replacing anything but does set the focus to txtMain. Does my code look in the right order? Again my apologies if this is obvious.
Guest UserRank #4,868 · 321 reputationPosted Apr 17, 2013, 8:40 AM EDT
Replace your code with what you had before and just move the Focus(); Method before the 2 Properties ie:-
private void tsbuttonReplace_Click(object sender, EventArgs e)
{
if (tstxtReplaceText.SelectionLength > 0)
{
txtMain.Focus();
tstxtReplaceText.SelectionStart = 0;
int len = tstxtReplaceText.SelectionLength;
tstxtReplaceText.Text = tstxtSearchText.Text.Remove(0, len);
txtMain.Text = tstxtReplaceText.Text.Insert(0, tstxtReplaceText.Text);
}
}
Guest UserRank #4,868 · 321 reputationPosted Apr 17, 2013, 9:12 AM EDT
Documentation can be found here:-
http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx
Stephen Walsh15 reputationPosted Apr 17, 2013, 9:37 AM EDT
if (tstxtReplaceText.SelectionLength >= 0)
{
txtMain.Focus();
txtMain.SelectionStart = txtMain.SelectionStart + 1;
txtMain.SelectionLength = 1;
tstxtReplaceText.SelectionStart = 0;
tstxtSearchText.Text = "";
txtMain.Text = txtMain.SelectedText.Insert(0, tstxtReplaceText.Text);
}
This displays whatever is in the replace box in the main box but deletes everything else. I have no idea how to detect where the original word was & just replace the word. Any help would be apreciated. Thanks in advance.
Stephen Walsh15 reputationPosted Apr 17, 2013, 12:35 PM EDT
Guest UserRank #4,868 · 321 reputationPosted Apr 17, 2013, 1:17 PM EDT
Indeed like anything experience goes along way.
That's the trouble with programming, one can easily make it seem more complicated than it actually is! either by over analysing or adding unnecessary code. I should of spotted it earlier but after seeking advice from another member on here all became clear and the solution was finally given.
Cheers,