Find replace near the the end of a text file

skywola

Member
Joined
Jan 5, 2012
Messages
12
Location
Mesa, AZ, USA
Programming Experience
10+
In a find-replace operation, when you are very close to the end of the text file, I have found that if you use:
FrmMain.RTBMain.SelectedText = findInstance.replaceString
it adds the text, but does not re-size the file as a whole if you are replacing a small string with a large string,
and then when you try to save the file you get an error. My solution was to add the replace text to the clipboard,
then paste the text from the clipboard over the smaller text, which does re-size the file. However, I would
be that there is a shorter and easier solution. Below is the code:

VB.NET:
EOF = FrmMain.RTBMain.TextLength
  If position + findInstance.findString.Length < EOF Then
      FrmMain.RTBMain.SelectedText = findInstance.replaceString 
  Else  ' prevent error when replacing a selected string with greater length than the selected text near EOF 
  Dim clipboardContents As String = ""
  If Clipboard.ContainsText Then
      clipboardContents = Clipboard.GetText
  End If
  Clipboard.SetText(findInstance.replaceString)
                FrmMain.RTBMain.Paste()
  If clipboardContents.Length > 0 Then
      Clipboard.SetText(clipboardContents)
  End If
End If
 
it adds the text, but does not re-size the file as a whole if you are replacing a small string with a large string,
and then when you try to save the file you get an error.
It must be something wrong with your saving the content to file then. RichTextBox.SaveFile methods will not have that problem.
 
Back
Top