473,883 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Formatting text in a RichTextBox in WindowsForm


The documentation for the RichTextBox is sketchy at best. I want to do a
very simple task but I cannot find information on this. I am using a
RichTextBox as an output window. Some text I want to just add plain (e.g..
rtf.Text += "new stuff..."; ) but some text, when added, I want to be in a
different color or font size. Find() will not work because the text will
vary. I have delimiters on it so I could recognize it with a regular
expression (e.g. "\[.*?\]") but Find only takes constant strings. The
issue, then, is being able to select by regular expression, after which I
could set the SelectionFont or SelectionColor. Also, I observe that whenI
add new text, previous formatting I have done disappears. So I have to
reapply all formatting each time I add something; is that the way it is
supposed to work?
Nov 9 '06 #1
3 7200
If you are adding to the end of the text, I found it easier to have a second
non visibel RTB and set the text of that. Then select the text and apply all
the formatting. Then you take the RTF text from it and append it to the RTF
text of the main RTB.
If you set the Text property then it will lose all formatting as you are
giving it a new piece of plain text to show. It isnt able to determin if the
the new text is the old text plus some as they are difference string
instances.

HTH

Ciaran O'Donnell

"michael sorens" wrote:
>
The documentation for the RichTextBox is sketchy at best. I want to do a
very simple task but I cannot find information on this. I am using a
RichTextBox as an output window. Some text I want to just add plain (e.g..
rtf.Text += "new stuff..."; ) but some text, when added, I want to be in a
different color or font size. Find() will not work because the text will
vary. I have delimiters on it so I could recognize it with a regular
expression (e.g. "\[.*?\]") but Find only takes constant strings. The
issue, then, is being able to select by regular expression, after which I
could set the SelectionFont or SelectionColor. Also, I observe that when I
add new text, previous formatting I have done disappears. So I have to
reapply all formatting each time I add something; is that the way it is
supposed to work?
Nov 10 '06 #2
Hi Michael,

The introduction of RichTextBox class in MSDN may be a little sketchy. For
more information on how to use RichTextBox control in Windows Forms, you
could visit the following link.

http://msdn2.microsoft.com/en-us/library/aa467125.aspx

As for your first question, I am sorry that RichTextBox doesn't support
such a function at present. The Find method of the RichtTextBox can only
take a concrete string as its parameter. If you do need this function, I
recommend you to write a method to encapsulate the code for this function
(you could search the Internet for the arithmetic) and then call this
method whenever you want.

You have mentioned that you want to format some text when added. When a
text is added to a RichTextBox control, the SelectionStart property of the
RichTextBox is set to the end position of the added text. We could make use
of this feature to select the added text and then set the SelectionFont or
SelectionColor property of the RichTextBox.

The following is a sample.

this.richTextBo xPrintCtrl1.App endText("abcde" );
// set the SelectionStart property to the start position of the added text
this.richTextBo xPrintCtrl1.Sel ectionStart -= 5;
// set the SelectionLength property to the length of the added text
this.richTextBo xPrintCtrl1.Sel ectionLength = 5;
this.richTextBo xPrintCtrl1.Sel ectionFont = new Font("Times New Roman", 20);
this.richTextBo xPrintCtrl1.Sel ectionColor = Color.Blue;

As for your second question, could you tell me how you add a text in the
RichTextBox control? If we use the following code to add a text, the
previous format will be lost, because the Text property of the RichTextBox
control is re-assigned as as whole.

this.richTextBo xPrintCtrl1.Tex t += "abcde";

Instead, we could use the AppendText method of the RichTextBox control to
add a text.

Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 10 '06 #3
Thank you for both the documentation pointer and the code fragment. That
was just what I needed. (And yes, I had tried a simple rtb.Text += ...
because I had missed the AppendText method.)


Nov 10 '06 #4

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

Similar topics

2
5841
by: Neil | last post by:
Is there an editable RTF textbox control which allows the user to apply bold, italic, etc.? I tried the Microsoft Rich Textbox Control, but there doesn't seem to be a way to allow the user to change the formatting while typing text. Is there such a beast? Thanks, Neil
0
1708
by: Hariharan S | last post by:
Hi Guys, I have a string, say "Hello World" and would like to format the way I want to (say, wanna have the Hello to be bold italic and the World to be of font size 14 or so) and put it in the RichTextBox, I created. Just want to paste or write this string to the RichTextBox; what command should I use? I used the following command and it did not help ... richTextBox.SelectionFont = <some font selected>
2
17292
by: Hariharan S | last post by:
Hi Guys, I have a string, say "Hello World" and would like to format the way I want to (say, wanna have the Hello to be bold italic and the World to be of font size 14 or so) and put it in the RichTextBox, I created. Just want to paste or write this string to the RichTextBox; what command should I use? I used the following command and it did not help ... richTextBox.SelectionFont = <some font selected>
0
1398
by: Radu.Micu | last post by:
I have a RichTextBox on my form and I want to save the text that a user writes into it in a AccessDB but I want to save the formatted text not only plain txt I have seen in some programs that the db table has 2 columns one for the plain txt and one for some enconding I do not know but I know for certain that in that column all formatting are hold, is there a way to do this? Because to insert the rtf format to the DB is not realy efficient...
1
7963
by: Kevin | last post by:
Hello, Does anyone know how to implement find and replace in a RichTextBox in a way that does not lose the text formatting? As a first attempt I tried rtb.Rtf.Replace(sFindText, sReplaceText) , but that's no good since .Rtf includes all the formatting codes, and .Replace will change richtext codes that shouldn't be changed.
2
5495
by: Chris Rollock | last post by:
on msdn: "you can initialize the Rtf property to a string that contains the text to display, including the RTF codes that determine how the text should be formatted." Nowhere can I find an example on how to do this. I'm taking data from an xml file as a string, and I want to display it in a richtextbox with formatting depending on what it is I'm looking at. All I need is an example of how to directly append (rather than loading a file) a...
0
1259
by: ronchese | last post by:
Hello All. I'm using the RichTextBox control to show text to user that can do the primary font formatting: fontname, size, color, bold, italic and underline. Unlike the RichTextBox used in VB 6, this control in .NET do not apply the expected formatting when there are multiple selection of different font formats. For example, if I select a text that have portions in Bold and other portion
2
3712
by: Mystique | last post by:
Hello Is there any way to format text with tags (something like html tags) in a RichTextBox? Something similar to this: string aStr; aStr = "<b>this is bold</bthis is normal"; richTextBox1.Text = aStr;
2
2593
by: drfreaky | last post by:
Hello, I want to use a richtextbox for logging status messages. I have three kind of message types and want to use different formatting for them. My problem is, I change the color, add the text and the added text is shown in another color. I scroll to the end and want to add the next status line using appendtext. But now the previous line lost its formatting...... I want to keep the formatting… Do you have a solution? BTW I am using...
0
9943
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9793
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
11151
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
10750
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
10858
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
10419
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
9577
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
4619
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
3
3237
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.