473,386 Members | 1,823 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,386 software developers and data experts.

RichTextBox -- I am sooo Confused

Tom
Inserting text into a RichTextBox is a snap! >>

textBox1.Text += "Enter string 0000\n";
textBox1.Text += "Enter string 0001\n";
textBox1.Text += "Enter string 0002\n";
textBox1.Text += "Enter string 0003\n";

How do I then go back and programmatically remove let's say line #2?

As amazingly simple as this sounds ... I am equally dumbfounded by not
being able to find the solution. :(

Please shed some light on this for me.

Thanks!

-- Tom
Dec 18 '07 #1
8 2220
From what you've put in it looks more like you need a listbox rather
than a richtextbox. As far as deleting specific lines you would be
removing text from a gigantic string.

Dec 18 '07 #2
Tom,

You have to take the entire text string, parse it to identify the
elements that you don't want to include in the text, reconstruct the string,
and then set the Text property back to that string.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tom" <Th********@earthlink.netwrote in message
news:5e********************************@4ax.com...
Inserting text into a RichTextBox is a snap! >>

textBox1.Text += "Enter string 0000\n";
textBox1.Text += "Enter string 0001\n";
textBox1.Text += "Enter string 0002\n";
textBox1.Text += "Enter string 0003\n";

How do I then go back and programmatically remove let's say line #2?

As amazingly simple as this sounds ... I am equally dumbfounded by not
being able to find the solution. :(

Please shed some light on this for me.

Thanks!

-- Tom
Dec 18 '07 #3
Dom
There's a few ways.

1. Remember that the "\n" can be found in the Text property. So you
can split the Text back into the original lines by using r.Text.Split
("\n", ...);
2. If possible, just rewrite the entire property without the string.

Dom

On Dec 18, 10:06 am, Tom <Thomas-...@earthlink.netwrote:
Inserting text into a RichTextBox is a snap! >>

textBox1.Text += "Enter string 0000\n";
textBox1.Text += "Enter string 0001\n";
textBox1.Text += "Enter string 0002\n";
textBox1.Text += "Enter string 0003\n";

How do I then go back and programmatically remove let's say line #2?

As amazingly simple as this sounds ... I am equally dumbfounded by not
being able to find the solution. :(

Please shed some light on this for me.

Thanks!

-- Tom
Dec 18 '07 #4
On Tue, 18 Dec 2007 07:06:16 -0800, Tom <Th********@earthlink.netwrote:
Inserting text into a RichTextBox is a snap! >>

textBox1.Text += "Enter string 0000\n";
textBox1.Text += "Enter string 0001\n";
textBox1.Text += "Enter string 0002\n";
textBox1.Text += "Enter string 0003\n";

How do I then go back and programmatically remove let's say line #2?
While the suggestions to process the Text property directly will work,
given what you're trying to do I think that accessing the text via the
Lines property would be easier. It basically does the String.Split() for
you, returning an array of strings, one per line. So:

void RemoveLine(TextBox box, int line)
{
string[] lines = box.Lines;

Array.Copy(lines, line + 1, lines, line, lines.Length - line - 1);
lines[lines.Length - 1] = null;
box.Lines = lines;
}

or something like that...my apologies if there's an off-by-one error. I
didn't actually compile or run the code. :)

Pete
Dec 18 '07 #5
Tom
Thanks Everybody !!

I had searched a few hours before posting and then soon stumbled into
the "TextBoxBase" category of methods. My search skills are pathetic!

The RTB must have some powerful controls buried deep within ... but
not enough examples on programmatically maneuvering within its buffer.

I long for a simplistic tool >>

textBox1.RemoveRange(int lineStart, int lineCount);

Just like in ArrayList ... but then things would be just too easy?

I've tried searching for the line feed with no success as of yet.

string findMe = "\n";
int indexToText = textBox1.Find(findMe, 0, RichTextBoxFinds.None);

The above does not work. I know those line feeds are not stripped out
because you can allow wrapping and then shrink the control to force
wrapping ... followed by expanding the control that returns the lines
to unwrapped. Being able to search for a >chr(10), Char(10), '\n',
or however you properly specify a non printing character would be nice
considering "\n" does not work.

This work is part of a large data file viewer with small resource
usage project. I am trying to create this project for both future
usage and as a learning aid. I initiate the read at an arbitrary
location within the data file. Typically near the end of what are
often 50+ MB files.

My disk input is simple using this method >>

FileStream.Read(byte[] array, int offset, int count);

Then I trim the partial sentences away using a logic loop that
searches for byte(10). The front trimming is simply >>

int trim0 = 0;
while(byteArray[trim0] != 10) ++trim0;
long Position_0 = ++trim0;

Then I split the trimmed byteArray into lines and store them in an
ArrayList. This ArrayList is my buffer; however, it is a waste of
memory space!! There is no reason why the RTB could not be used as the
buffer storage space if adequate control was achieved.

As it is now ... I can clear the RTB and reconstruct it from the
ArrayList whenever I jump to another area of the data file. Good RTB
control would allow local scrolling to be much more efficient. I'd
really like the RTB caret to trigger buffer events. But learning about
that caret is another issue in itself.

This learning project of mine is certainly kickin me around. Someday I
will post it in its entirety and hopefully some other newbs can
benefit from it. Getting some guru level critique will be a very
humbling and educational event I look forward too.

Thanks again for all the comments!! Every suggestion and nudge gets me
that much closer to success. :)

-- Tom
Dec 18 '07 #6
Tom
Pete --

Your advice arrived while I was responding to earlier comments. I have
a major guilt issue here in that I over simplified my task in the hope
of learning more about the inner workings of the RichTextBox. (Not too
deep, I'm just learning!) In doing so I have cheated myself out of
receiving help at my project's structural level. I am essentially
splitting a byte array into lines and storing these lines in an
ArrayList. I then populate the RTB from the ArrayList.

I had to stress test this ArrayList - buffering logic repeatedly to
remove the off-by-one errors. I had a few in there ... but that part
is confirmed to be working! :)

And the bigger picture is I am building a viewer of large data files
that loads only the area of interest and does not hog memory
resources. Often my data files exceed 50 MB.

The ArrayList is a working buffer ... but it feeds the RTB which is
its own buffer too. This double buffering is inefficient of resources.
It works, it's relatively fast, but it also reflects my sloppy design.
Additionally, I Clear() the RTB each time I append or insert data into
the ArrayList buffer and then reload the RTB text.

I never learned MFC nor windows programming until now. Although I have
done a ton of numerical analysis type work. I do recall from Petzold's
Programming Windows 95 text that he moved a screen up a line and then
painted the new line when scrolling. For me to now Clear() the RTB and
rebuild it each time makes me feel like I am sloppy! I'd like to work
directly with the RTB buffer and eliminate the ArrayList if possible.
If I could insert and append into the RTB buffer beyond the viewing
area ... my thoughts are that I'd be taking advantage of what I
perceive to be very efficient RTB scrolling control.

Thanks for you input. It is always valuable!! I had not used the Lines
property as you illustrated. It's in my crosshairs now and I will
learn as I experiment with it. :)

-- Tom

>While the suggestions to process the Text property directly will work,
given what you're trying to do I think that accessing the text via the
Lines property would be easier. It basically does the String.Split() for
you, returning an array of strings, one per line. So:

void RemoveLine(TextBox box, int line)
{
string[] lines = box.Lines;

Array.Copy(lines, line + 1, lines, line, lines.Length - line - 1);
lines[lines.Length - 1] = null;
box.Lines = lines;
}

or something like that...my apologies if there's an off-by-one error. I
didn't actually compile or run the code. :)

Pete
Dec 18 '07 #7
On Tue, 18 Dec 2007 10:56:03 -0800, Tom <Th********@earthlink.netwrote:
[...]
The ArrayList is a working buffer ... but it feeds the RTB which is
its own buffer too. This double buffering is inefficient of resources.
It works, it's relatively fast, but it also reflects my sloppy design.
Additionally, I Clear() the RTB each time I append or insert data into
the ArrayList buffer and then reload the RTB text.
It's only "inefficient" in a relative sense. Maintaining two copies of
some very small subset of some large set of data is still more efficient
than maintaining two copies of that large set of data.

Could it be _more_ efficient? Sure. But assuming this mechanism is
working for you and the performance is adequate for your needs, it may in
fact be a fine solution. Simple, correct code that works is always better
than complicated, incorrect code that doesn't. Even if it's slower or
uses more resources (like memory). :)

That said, using a text box (rich or otherwise) there will be no way
around this problem. The text box maintains its buffer internally and you
don't have access to it. You can modify portions of it by creating a
selection and replacing that selection, using the SelectedText property.
And of course you can replace the entire text wholesale as illustrated in
this thread. But you can't fiddle with the text box buffers directly.

If you want a more efficient mechanism, you might be better off
implementing your own control, one that provides you direct access to
whatever data it's supposed to be displaying.

Pete
Dec 18 '07 #8
Tom
Hey all -- Here's an update on my progress. Again thanks for the
assistance to all. This update is perhaps of benefit to other newbs
who are working with the RichTextBox for the first time.

The key for gaining control of that RTB buffer is positioning the
caret, specifying the selection length, and using the SelectText
method. To insert without replacing any existing text ... simply set
the SelectonLength = 0. Simple, eh? But I did not realize this
amazingly simplistic technique and based on a review of other's
questions ... I'm not alone. Another simple technique is to set a
range of text equal to >"". (Just a pair of double quotes with no
space.) The simplest null literal there is!! This is great for
deleting rows.

For example >>

textBox1.SelectionStart = textBox1.SelectionLength = 0;
textBox1.SelectedText = "This line is inserted at the start.\n";

Once I gained this simple understanding ... I was able to eliminate a
layer of buffering on my little project. Now I use the RTB's buffer as
the storage buffer. Scrolling up >delete the bottom rows and insert
at the top. Scrolling down >delete the top rows and append at the
bottom. Seems to be working very nicely. :) Now I have to implement a
ton of controls to make it dance the jig with some pizzazz.

Some helpful links I found along the journey include >>

http://www.personalmicrocosms.com/Pa...px?c=4&t=6#tip

http://msdn2.microsoft.com/en-us/lib...ffice.10).aspx

http://geekswithblogs.net/pvidler/ar...10/15/182.aspx

http://www.syncfusion.com/FAQ/Window.../FAQ_c89c.aspx

Just the RTF specification is over 250 pages! This topic can become as
in depth as you care to explore it. I just wanted the most basic of
skills and seemed to have difficulty getting my feet on the ground. If
you are in the same boat ... the above info might save you hours of
frustration.

Happy holidays everyone.

-- Tom

On Tue, 18 Dec 2007 09:06:16 -0600, Tom <Th********@earthlink.net>
wrote:
>Inserting text into a RichTextBox is a snap! >>

textBox1.Text += "Enter string 0000\n";
textBox1.Text += "Enter string 0001\n";
textBox1.Text += "Enter string 0002\n";
textBox1.Text += "Enter string 0003\n";

How do I then go back and programmatically remove let's say line #2?

As amazingly simple as this sounds ... I am equally dumbfounded by not
being able to find the solution. :(

Please shed some light on this for me.

Thanks!

-- Tom
Dec 19 '07 #9

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

Similar topics

0
by: nouno | last post by:
I am trying to spell check a richtextbox. Through code (shown below) I save the contents of the richtextbox to a rtf file, open the rtf file in Word, spell check it, save it, and then load the ftf...
0
by: Sethuraman V | last post by:
Hello Everybody, I have a rich text box in which i have to drop a custom object (user defined object). When i drop a string into the richtextbox control, everything goes fine but if i drop a...
11
by: Mad Joe | last post by:
I'm using a richTextBox for editing a source code (with Syntax Highlighting) of my own programming language... How come that myRichTextBox doesn't respond to Undo/Redo functions by using default...
1
by: Jesper | last post by:
Hi, Im fairly new to C# but have som experience in the C++/MFC world and I am a bit confused as I cant find some information that I am used to having. I am trying to use the RichTextBox control...
0
by: nuwankkumara | last post by:
hai all, i am using RichTextBox as my editor , there i can't perform undo and redo when i have changed the color and font of the text as below: richBox.SelectionStart = index;...
0
by: tcloud | last post by:
I have a RichTextBox that: - 1st would intermittently show the scroll bars but they wouldn't work - 2nd now doesn't allow any interaction even though it looks normal The application processes...
9
by: James Wong | last post by:
Hi, I use the RichTextBox in my program. It will use different language in this RichTextBox (chinese and english characters), and it set the "DualFont" and use different fonts. By the way, how...
2
by: lizii | last post by:
At the moment i am trying to write into a richtextbox using the Rtf call from another richtextbox. However realising that richtextbox do not have multithreading - i know i need to invoke this...
0
by: Vimalathithan | last post by:
I just developing a editor. I have provide the options like Bold, Italic, underlin, font change, font size change. These font options are keep in with one toolstripbutton. the toolstripbar keep...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.