Replacing a line in a multiline RichTextBox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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;
 
[...]
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;

When you call the Lines property of RichTextBox, you get an array that'sa
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
 
Peter Duniho said:
[...]
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;

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

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

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
 
Peter Duniho said:
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
Thanks Pete. It's so simple once it's explained!
 
Peter Duniho said:
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

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
 
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?

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
 
Peter Duniho said:
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?

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

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

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. :)
 
Back
Top