Replacing a line in a multiline RichTextBox

Replacing a line in a multiline RichTextBox

Post by UmF5IE1pdG » Wed, 13 Jun 2007 01:06:03


I have a RichTextBox (rtfTerminal below) in which I would like to replace the
last line with the last line minus its last character (i.e., do a backspace).
I tried the following code, wherein I first get a substring representing the
last line minus the last character, then rewrite it back over the last line.
The substring itself looks fine but assigning it to rtfTerminal.Lines[lineNo]
does not change rtfTerminal.Lines[lineNo]. As a matter of fact, I can assign
any string into rtfTerminal.Lines[lineNo] and it acts as if the assignment
never occurred. What am I missing? Thanks, as always.

string s = rtfTerminal.Lines[lineNo].Substring(0, colNo);
rtfTerminal.Lines[lineNo] = s;
 
 
 

Replacing a line in a multiline RichTextBox

Post by Peter Duni » Wed, 13 Jun 2007 02:54:26

On Mon, 11 Jun 2007 09:06:03 -0700, Ray Mitchell



When you call the Lines property of RichTextBox, you get an array that's a
copy of the lines in the textbox. You need something like this:

string s = rtfTerminal.Lines[lineNo].Substring(0, colNo);
string[] lines = rtfTerminal.Lines;

lines[lineNo] = s;
rtfTerminal.Lines = lines;

That will modify the copy and reassign it back to the textbox.

Pete

 
 
 

Replacing a line in a multiline RichTextBox

Post by UmF5IE1pdG » Wed, 13 Jun 2007 04:26:02


Thanks a lot, Pete. It worked! However, just one more question... Although
the modified line does display correctly, and I can move the caret to the
correct column (back one space) by doing a rtfTerminal.Select(colNo - 1, 0);,
the caret jumps up to the first line on the page. Do you know how to keep it
(or put it back) on the line it was on (the last line)? Thanks in advance.
Ray
 
 
 

Replacing a line in a multiline RichTextBox

Post by Peter Duni » Wed, 13 Jun 2007 04:39:05

On Mon, 11 Jun 2007 12:26:02 -0700, Ray Mitchell



Use the GetCharacterIndexFromLineIndex() method:

rtfTerminal.Select(rtfTerminal.GetCharacterIndexFromLineIndex(lineNo)
+ colNo - 1, 0);

You need to do this because while the text box controls offer a line-based
way to access the text in the control, the text really isn't line-based as
far as most of the control API is concerned; it's really just a single
string. In particular, the Select() method concerns itself only with this
string, so you need to convert from a "line and column" address to a
simple "character" address within the text.

Pete
 
 
 

Replacing a line in a multiline RichTextBox

Post by UmF5IE1pdG » Wed, 13 Jun 2007 04:58:01


Thanks Pete. It's so simple once it's explained!
 
 
 

Replacing a line in a multiline RichTextBox

Post by UmF5IE1pdG » Wed, 13 Jun 2007 05:50:00


There's apparently one slight problem. The GetCharacterIndexFromLineIndex
method appears to be new in .NET framework 3.0. I have installed that on my
machine but when I do an About on my Visual Studio 2005 Pro (academic), it
only shows the .NET framework version 2.0.50727. So, apparently I need to
tell it to somehow use my 3.0 installation. Do you have any ideas on this?
Thanks, Ray
 
 
 

Replacing a line in a multiline RichTextBox

Post by Peter Duni » Wed, 13 Jun 2007 09:12:48

On Mon, 11 Jun 2007 13:50:00 -0700, Ray Mitchell



True. Sorry, I didn't realize you couldn't use that without some effort..
I haven't actually upgraded a 2.0 to 3.0 installation, so while I believe
there should be a relatively simple way to allow .NET 3.0 development in
your existing VS 2005 installation, I don't know the specifics. I
_suspect_ that there's a .NET 3.0 SDK you can install that will do the
right things to enable that.

However, it's not difficult to implement yourself, if you don't want to
bother with .NET 3.0. You can simply write your own version of that
method, that takes the Text or Lines property of the text box and scans
through it for the number of line breaks corresponding to the "lineNo"
value. For example (warning, code not even compiled, never mind
tested...but this should give you the basic idea):

int GetCharacterIndexFromLineIndex(int lineNo, string str)
{
if (lineNo-- > 0)
{
int ichNewLine = str.IndexOf(Environment.NewLine);

while (ichNewLine != -1 && lineNo-- > 0)
{
ichNewLine = str.IndexOf(Environment.NewLine, ichNewLine
+ Environment.NewLine.Length);

if (ichNewLine + Environment.NewLine.Length >= str.Length)
{
ichNewLine = -1;
}
}

return ichNewLine;
}
else
{
return 0;
}
}

or alternatively (depending on whether you want to pass the Text property
or Lines property):

int GetCharacterIndexFromLineIndex(int lineNo, string[] rgstrLines)
{
int ichNewLine = 0;

for (int ilineCur = 0; ilineCur < rgstrLines.Length && lineNo > 0;
ilineCur++, lineNo--)
{
ichNewLine += rgstrLines[ilineCur].Length
+ Environment.NewLine.Length;
}

return (lineNo > 0) ? -1 : ichNewLine;
}

Hope that helps.

Pete
 
 
 

Replacing a line in a multiline RichTextBox

Post by UmF5IE1pdG » Wed, 13 Jun 2007 09:24:01


Thanks Pete. I actually came up with a soultion that looks at the length of
the entire string using .TextLength, then plays games with it. It seems to
work fairly well. However, I did another posting to this group regarding
text flashing problems I'm having, which may be related to how I did it. I
included some of my code, if you care to look at it. Thanks again for your
help, Ray.
 
 
 

Replacing a line in a multiline RichTextBox

Post by Peter Duni » Wed, 13 Jun 2007 09:50:28

On Mon, 11 Jun 2007 17:24:01 -0700, Ray Mitchell



I don't see how you can modify the last character in an arbitrary line
within the textbox that way, but okay. If you say so. :)