Remove Method

Topics: Developer Forum, User Forum
Jun 29, 2011 at 7:55 PM

I am trying to remove some characters from the text.  The text.remove method is not removing any text, however.  I used it previously to clear the text but that is also no longer working.  I can't think of anything that might have changed to affect this.  Has someone seen this before or is there a better way of clearing?  It is not throwing any exceptions but nothing is happening, the program simply continues.

Aug 24, 2011 at 4:16 AM

I am having the same problem.

Aug 24, 2011 at 6:45 PM

I finally gave up on the remove method.  I just used scintilla.text = "" to clear contents and I changed what I was doing so the remove method wasn't necessary.  Sorry I don't have a better answer.

Aug 24, 2011 at 10:38 PM

Same.

I was creating a way to batch add/remove line comments, but the remove method wasn't working.  Ended up with something like what you described.

Apr 16, 2013 at 1:41 AM
Sorry about being late. I found this post and was sad to see nothing good (not your fault) so I kept looking around and found this:

((INativeScintilla)scintilla).DeleteBack()

This will simulate a backspace key press (remove 1 character on the left of the cursor). It isnt pretty but it could get the job done for you. Worked for me :)
Developer
Apr 16, 2013 at 1:42 PM
The Text.Remove method doesn't remove any text because you have to assign the return value of that method back to the Text property. The reason for this is that strings in .Net are immutable, meaning a reference to a string will always refer to the same value, and that value will never change. When you change a string you are actually creating a new one, so the Text property has to be updated to the newly created string, rather than the original.
May 20, 2013 at 12:00 AM
The problem is that if you only want to remove some text as soon as you assign the text back the control scrolls to the top of the page.
Developer
May 20, 2013 at 1:41 PM
I didn't think about that... Hmmm... There is a SCI_DELETERANGE method in native scintilla which would do what you want, but I don't think it's wrapped currently by ScintillaNET, meaning you'd have to send the message directly.
Nov 24, 2013 at 12:07 PM
Edited Nov 24, 2013 at 12:08 PM
I tried using SCI_DELETERANGE but nothing happened, I did someting like:
int startPos = ...;
int length = ...;
const uint SCI_DELETERANGE = 2645;
scintilla.NativeInterface.SendMessageDirect(SCI_DELETERANGE, startPos, length);
Coordinator
Feb 18, 2014 at 3:43 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Feb 18, 2014 at 4:13 PM
1). As was pointed out by @blah38621, doing
scintilla.Text.Remove(1, 5);
will have no affect on the text in the Scintilla control and it has nothing to do with Scintilla. The Scintilla.Text property is a string and strings in .NET are immutable. i.e. can't be changed. What the string.Remove method does is return a NEW string with the range specified removed. Thus you could do:
scintilla.Text = scintilla.Text.Remove(1, 5);
but that would be horribly inefficient.

2). The recommended way to delete a range of text in Scintilla is to use a Range object. For example:
scintilla.GetRange(1, 5).Text = "";
will get the range between indexes 1 and 5 and replace it with an empty string. i.e. remove the text in that range.

3). I agree that we should implement the SCI_DELETERANGE message and make this easier to do. As noted in the post above I created work item 34984 to do just that.


Cheers,
Jacob