473,805 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Lin es[lineNo]
does not change rtfTerminal.Lin es[lineNo]. As a matter of fact, I can assign
any string into rtfTerminal.Lin es[lineNo] and it acts as if the assignment
never occurred. What am I missing? Thanks, as always.

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

Jun 11 '07 #1
8 12369
On Mon, 11 Jun 2007 09:06:03 -0700, Ray Mitchell
<Ra************ *****@MeanOldTe acher.comwrote:
[...]
The substring itself looks fine but assigning it to
rtfTerminal.Lin es[lineNo]
does not change rtfTerminal.Lin es[lineNo]. As a matter of fact, I can
assign
any string into rtfTerminal.Lin es[lineNo] and it acts as if the
assignment
never occurred. What am I missing? Thanks, as always.

string s = rtfTerminal.Lin es[lineNo].Substring(0, colNo);
rtfTerminal.Lin es[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.Lin es[lineNo].Substring(0, colNo);
string[] lines = rtfTerminal.Lin es;

lines[lineNo] = s;
rtfTerminal.Lin es = 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************ *****@MeanOldTe acher.comwrote:
[...]
The substring itself looks fine but assigning it to
rtfTerminal.Lin es[lineNo]
does not change rtfTerminal.Lin es[lineNo]. As a matter of fact, I can
assign
any string into rtfTerminal.Lin es[lineNo] and it acts as if the
assignment
never occurred. What am I missing? Thanks, as always.

string s = rtfTerminal.Lin es[lineNo].Substring(0, colNo);
rtfTerminal.Lin es[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.Lin es[lineNo].Substring(0, colNo);
string[] lines = rtfTerminal.Lin es;

lines[lineNo] = s;
rtfTerminal.Lin es = 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.Sel ect(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************ *****@MeanOldTe acher.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.Sel ect(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 GetCharacterInd exFromLineIndex () method:

rtfTerminal.Sel ect(rtfTerminal .GetCharacterIn dexFromLineInde x(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************ *****@MeanOldTe acher.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.Sel ect(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 GetCharacterInd exFromLineIndex () method:

rtfTerminal.Sel ect(rtfTerminal .GetCharacterIn dexFromLineInde x(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************ *****@MeanOldTe acher.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.Sel ect(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 GetCharacterInd exFromLineIndex () method:

rtfTerminal.Sel ect(rtfTerminal .GetCharacterIn dexFromLineInde x(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 GetCharacterInd exFromLineIndex
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************ *****@MeanOldTe acher.comwrote:
There's apparently one slight problem. The
GetCharacterInd exFromLineIndex
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 GetCharacterInd exFromLineIndex (int lineNo, string str)
{
if (lineNo-- 0)
{
int ichNewLine = str.IndexOf(Env ironment.NewLin e);

while (ichNewLine != -1 && lineNo-- 0)
{
ichNewLine = str.IndexOf(Env ironment.NewLin e, ichNewLine
+ Environment.New Line.Length);

if (ichNewLine + Environment.New Line.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 GetCharacterInd exFromLineIndex (int lineNo, string[] rgstrLines)
{
int ichNewLine = 0;

for (int ilineCur = 0; ilineCur < rgstrLines.Leng th && lineNo0;
ilineCur++, lineNo--)
{
ichNewLine += rgstrLines[ilineCur].Length
+ Environment.New Line.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************ *****@MeanOldTe acher.comwrote:
There's apparently one slight problem. The
GetCharacterInd exFromLineIndex
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 GetCharacterInd exFromLineIndex (int lineNo, string str)
{
if (lineNo-- 0)
{
int ichNewLine = str.IndexOf(Env ironment.NewLin e);

while (ichNewLine != -1 && lineNo-- 0)
{
ichNewLine = str.IndexOf(Env ironment.NewLin e, ichNewLine
+ Environment.New Line.Length);

if (ichNewLine + Environment.New Line.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 GetCharacterInd exFromLineIndex (int lineNo, string[] rgstrLines)
{
int ichNewLine = 0;

for (int ilineCur = 0; ilineCur < rgstrLines.Leng th && lineNo 0;
ilineCur++, lineNo--)
{
ichNewLine += rgstrLines[ilineCur].Length
+ Environment.New Line.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************ *****@MeanOldTe acher.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
16898
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
11665
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 = true richTextBox->Text = S"First line /n" richTextBox->Text = S"Second line" Thanks
0
1172
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
2006
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 the white spaces also. I tried copying all file int list and then tried to manipulate that
2
2919
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
1974
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 removing each character from the string that represents that line, but how do I back up the caret? I understand .NET 3.0 has implemented a method for doing backspaces, but I'd like to know how to do it manually since not many people around here...
3
1934
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 still needs some work to make it work for all cases but my question is more basic so I think you can probably ignore the minor details (?). My main question concerns why all the text in the box flashes each time I do a backspace and what can be done...
1
2105
by: StanislavPetrov | last post by:
How to select line in a RichTextBox?i mean like item in ListBox control.
2
1367
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 NUMBER" rtf1.ScrollToCaret() However I need to know how to find the first line in a rich text box. Anyone have any ideas?
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10609
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10360
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10105
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.