473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Replacing a line in a multiline RichTextBox

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;

Jun 11 '07 #1
8 12323
On Mon, 11 Jun 2007 09:06:03 -0700, Ray Mitchell
<Ra*****************@MeanOldTeacher.comwrote:
[...]
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
Jun 11 '07 #2


"Peter Duniho" wrote:
On Mon, 11 Jun 2007 09:06:03 -0700, Ray Mitchell
<Ra*****************@MeanOldTeacher.comwrote:
[...]
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
Jun 11 '07 #3
On Mon, 11 Jun 2007 12:26:02 -0700, Ray Mitchell
<Ra*****************@MeanOldTeacher.comwrote:
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.GetCharacterIndexFr omLineIndex(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
Jun 11 '07 #4


"Peter Duniho" wrote:
On Mon, 11 Jun 2007 12:26:02 -0700, Ray Mitchell
<Ra*****************@MeanOldTeacher.comwrote:
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.GetCharacterIndexFr omLineIndex(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!
Jun 11 '07 #5
"Peter Duniho" wrote:
On Mon, 11 Jun 2007 12:26:02 -0700, Ray Mitchell
<Ra*****************@MeanOldTeacher.comwrote:
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.GetCharacterIndexFr omLineIndex(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
Jun 11 '07 #6
On Mon, 11 Jun 2007 13:50:00 -0700, Ray Mitchell
<Ra*****************@MeanOldTeacher.comwrote:
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 && lineNo0;
ilineCur++, lineNo--)
{
ichNewLine += rgstrLines[ilineCur].Length
+ Environment.NewLine.Length;
}

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

Hope that helps.

Pete
Jun 12 '07 #7


"Peter Duniho" wrote:
On Mon, 11 Jun 2007 13:50:00 -0700, Ray Mitchell
<Ra*****************@MeanOldTeacher.comwrote:
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.
Jun 12 '07 #8
On Mon, 11 Jun 2007 17:24:01 -0700, Ray Mitchell
<Ra*****************@MeanOldTeacher.comwrote:
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. :)
Jun 12 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chad | last post by:
Hello, im struggleing,Im trying to replace a line in a file. 1|010000 1|010001 1|010002 0|010003 <-- replace this with 1|10003 0|010004 0|010005 .... all of the strings are the same length.
10
by: D Steward | last post by:
I can't seem to add a newline (/n) to get a richtextbox control to display text on successive lines. The text that I type is overwritten. How do I remedy this My example richTextBox->Multiline...
0
by: Buddhist[CHinA] | last post by:
I got a line number, and I wanted to display that line in the middle of a RichTextBox. Can someone give me a hint to do it? Thx
7
by: CSUIDL PROGRAMMEr | last post by:
Folks I am trying to read a file This file has a line containing string 'disable = yes' I want to change this line to 'disable = no' The concern here is that , i plan to take into account...
2
by: Don Gollahon | last post by:
..NET 1.1 How do I get the line # the cursor is currently in in a RichTextBox? Cursor x, y coordinates in the component would be nice. Thanks. -- Don Gollahon
2
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I'm trying to implement a backspace on the "bottom" line of a multiline text box. I want it to be ignored if it's already at the beginning of that line. I think I've got the part about...
3
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I've asked about this kind of thing before but I'm still having problems with it. Below is a stripped down version of my code to do a backspace in a multiline RichTextBox. I know it...
1
by: StanislavPetrov | last post by:
How to select line in a RichTextBox?i mean like item in ListBox control.
2
by: aethiolas | last post by:
I'm writing code to basically try to sync the line position of two richtextboxes and I would think that it would be possible to set it using something like: rtf1.Selection = rtf2."TOPLINE...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.