Geeks With Blogs
Pete's Weblog The Blog Formerly Known as Fun with WinForms

For a while now I've been toying with the idea of writing a C# editor (almost an IDE, but with only the features that I need). Clearly the hardest part of this task would be getting some form of edit control working.. the obvious choice for which is the RichTextBox. Anyone who has tried using RichTextBox in this situation will know that it has a number of flaws, the most serious being that it's rather slow when trying to colourise the text.

I've long since given up on using RichTextBox for a code editor, but while I was investigating its use I came across a number of ways to improve it's functionality. I'll present these here in a series of posts on the subject.. first up is how to speed up the control's updating, as well as adding one little extra formatting option.

Faster Updating

There are articles on this subject on the internet. The standard method seems to be to send a WM_SETREDRAW message to prevent the control from being redrawn while updating. I had this implemented and running nicely when I came across another article showing that an extra speed increase can be gained by sending EM_SETEVENTMASK to prevent the control from handling events. Here's the code:

+ Code to help speed up RichTextBox updating.

Although I'm using HandleRef here, I have absolutely no idea if this is the correct usage (or even if it's needed). Full code will be at the end of this posting. I have not fully tested these methods so I have no idea what the performance gains might be (if there are any). If anyone wants to try benchmarking, please let me know the results.

Justification

It always bothered me that the SelectionAlignment property of RichTextBox does not allow fully justified text. Especially since I know it's possible with the richedit control that RichTextBox wraps. Here's some code that will replace SelectionAlignment with something a little nicer for those that are writing word processors in C#:

+ Code to allow justified text in the RichTextBox.

The TextAlign enumeration contains Left, Right, Center and Justify. Again, this is mostly untested -- it should work on any machine with the richedit 2.0 common control (I believe), but I offer no guarantees.

Code

Here's the full code for the AdvRichTextBox control (plus the TextAlign enumeration):

+ Full AdvRichTextBox code.

Conclusion

I'll add to this code in future posts and if anyone has any comments or suggestions (or even requests), feel free to ask them. Hope you found this useful.

Posted on Tuesday, October 14, 2003 5:02 PM Windows Forms | Back to top


Comments on this post: Extending RichTextBox

# re: Extending RichTextBox
Requesting Gravatar...
Hi,

Thanks Sooo Much! for your Extending RichTextBox articles -
very-helpful.

Do you know of anyone who put those extensions into a
standalone .Net WinForm control to bring into a VSNet
project?

Thanks
Gene

Left by Eugene Mortimore on Dec 03, 2003 5:25 PM

# re: Extending RichTextBox
Requesting Gravatar...
Hi,

Can you help meinadding Pagebreaks in a richtextbox.

I am in dire need of this functionality

Thanks in advance.
Asim
Left by Asim on May 05, 2004 4:10 AM

# re: Extending RichTextBox
Requesting Gravatar...
EXCELLENT! you are a genius
Left by Repstat on May 09, 2004 8:46 PM

# re: Extending RichTextBox
Requesting Gravatar...
This is good stuff.
I have a simple problem I can't get over with richTextBox. I am dragging text over and dropping it in the middle of other text. - Using the Rtf of the text, I can't find the insertion point SelectionStart is only for the Text property, not the Rtf.

How do I find Rtf insertion point without lots of code?
Thanks.
Left by jackz on Jul 14, 2004 2:49 PM

# re: Extending RichTextBox
Requesting Gravatar...
After hours and hours searching I finally found what I was looking for :)
Thank you, thank you, thank you

Left by gof on Jul 20, 2004 7:00 AM

# re: Extending RichTextBox
Requesting Gravatar...
Your RichTextBox extension partially overlaps with an article of mine on <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/wnf_RichTextBox.asp">MSDN</a>, so you could save yourself some time with the code published there (regarding printing, for example).

Oh, and btw. in EndUpdate() I think it should read
<pre>if ( updating < 0 )</pre>
instead of
<pre> if ( updating > 0 )</pre>

mav
Left by mav.northwind on Jul 30, 2004 12:51 PM

# re: Extending RichTextBox
Requesting Gravatar...
Hi,
can you help me figure out how i can set number bullets and keep them even when we enter 2 "enter key". When we press Shift + Ctrl + L it goes through the bullet styles but no public inerface or properties to set it.
thanks
Left by phil on Aug 09, 2004 10:52 PM

# re: Extending RichTextBox
Requesting Gravatar...
Wow, so many comments... sorry I haven't been back to this site in a while.

mav.northwind: Your link doesn't appear to work. Also, EndUpdate is correct as it should only reset the events to the richtextbox when updating is finished.

phil: Sorry, I have no idea -- it's been too long since I touched this stuff.
Left by Pete on Aug 22, 2004 12:15 PM

# re: Extending RichTextBox
Requesting Gravatar...
On closer examination the link does work, it's just the browser picking up extra junk (the ">MSDN</a>").
Left by Pete on Aug 22, 2004 12:17 PM

# re: Extending RichTextBox
Requesting Gravatar...
Hi,

I want some of the text in RichTextBox to be right indented. Can some point to how it can be done?

--Nitin
Left by Nitin Rastogi on Oct 13, 2004 5:37 AM

# re: Extending RichTextBox
Requesting Gravatar...
thank you very much
Left by Tran Thien Nhan on Oct 29, 2004 3:40 AM

# re: Extending RichTextBox
Requesting Gravatar...
Thank you for your job. It's great.
I have one question.
During a selection, I have some bold caracters and some italic caracters for example.
When I use the FontDialog class, I must enter the style and so the style selection become always the same even if I just want to change the size of font.
Do you know how I can doing?
Left by matthieu on Nov 16, 2004 9:47 AM

# re: Extending RichTextBox
Requesting Gravatar...
I need to show editable form fields. Can I ??
Left by Pet on Nov 26, 2004 12:10 AM

# re: Extending RichTextBox
Requesting Gravatar...
any comment for number bulleting
my email is garconalapipe@hotmail.com
Left by greeky creek on Jan 26, 2005 8:55 AM

# re: Extending RichTextBox
Requesting Gravatar...
I need help for making Numbering (Bullets and Numbering) List so any body know about this ?????????
Left by Jaan Ji on Feb 01, 2005 9:37 AM

# re: Extending RichTextBox
Requesting Gravatar...
I'm having a problem with RichTextEdit.AppendText pounding the GC. At first it causes a gen 0 collection pretty much every call, and then more later.

Are there any ways to get around this?

Thanks in advance.
Left by reinux on Feb 24, 2005 1:52 AM

# re: Extending RichTextBox
Requesting Gravatar...
If I insert data towards the bottom of the AdvRichTextBox when scrollbars are present the control scrolls so that the cursor is now at the top of the control. I don't want this functionality i just wan the view to remain unchanged. Any thoughts?
Left by Al on Feb 25, 2005 12:24 AM

# re: Extending RichTextBox
Requesting Gravatar...

I'm having the same problem as 'reinux'. Has anyone figured out how to prevent AppendText causing scrolling to be incredibly slow? I'm simulating an overnight load on a richtextbox log window and loading in, line by line, 13M of a previously recorded log. Slowly but surely scrolling becomes *incredibly* slow.

I see the same behavior when I set SelectedText to supposedly add the new line of text without creating copying the existing contents

private void DoAppend(string m)
{
// richTextBox1.AppendText(m + Environment.NewLine);
richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.SelectedText = m + Environment.NewLine;
}

Craig
Left by Craig on Mar 25, 2005 6:27 PM

# need code for changing selected rtb text format
Requesting Gravatar...
i need a code to change format the selected text of rich text box in vb.net. vb.net tells that

RTF1.SelectionFont.Bold is readonly property.

other wise, how can i make it possible.

tellme.
dhana.
Left by dhana on Mar 28, 2005 10:49 AM

# RichTextBox
Requesting Gravatar...

i use your adv rtf control ,fine.

i have a problem with rtf control.

to disable scroll , while scan entire text for color changing,
or to restore old position after scanning.

i tried lot not working

and another
in rtf Additional new line control charater is found in rich text box. Is there any way to disable that.
Left by raj on May 17, 2005 7:25 AM

# RichTextBox
Requesting Gravatar...

i use your adv rtf control ,fine.

i have a problem with rtf control.

to disable scroll , while scan entire text for color changing,
or to restore old position after scanning.

i tried lot not working

and another
in rtf Additional new line control charater is found in rich text box. Is there any way to disable that.
Left by raj on May 17, 2005 7:25 AM

# re: Extending RichTextBox
Requesting Gravatar...
To toggle the font to bold in VB .Net

Dim tb As RichTextBox

tb.SelectionFont = New Font(tb.SelectionFont, FontStyle.Bold)

Left by JM on Jul 06, 2005 5:23 AM

# re: Extending RichTextBox
Requesting Gravatar...
Its really gr8 , thanks for writing such a nice article.It helped me a lot.Keep writing the articles of pratical usage.Once again I thank for writing this kind of article.
Left by Manohar on Sep 23, 2005 9:31 AM

# re: Extending RichTextBox
Requesting Gravatar...
This works excellent as far as speeding up the SelectionColor process, but I have a different problem now.

It colors all the right selected text to the color of blue, but then the very next time I populate the RichTextBox all of the text turns blue.

I've tried to reset the forecolor and the selectedColor to black each time before using it again, but it insists on coloring all of the text blue!
Left by David Douglass on Nov 20, 2005 8:39 PM

# re: Extending RichTextBox
Requesting Gravatar...
I have a problem with a RichTextBox control.
It does not properly support Punjabi font.
The same file with the same program it works on Windos 2000 but does not on XP it adds spaces inside the word.

I tried on many computers and it's the same.
Please help me....
Left by Alex Malyakin on Nov 24, 2005 2:16 PM

# re: Extending RichTextBox
Requesting Gravatar...
I had the same problem as Al, adding text when scrollbars are present makes the cursor scroll up to the top of the control.
This can be prevented by adding the following code to store then restore the scroll position at the end of the update.



//used for saving and restoring the scroll position to avoid flickering...
[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public long X;
public long Y;
}

private const int EM_GETSCROLLPOS = 0x0400 + 221;
private const int EM_SETSCROLLPOS = 0x0400 + 222;

[DllImport( "user32", CharSet = CharSet.Auto )]
private static extern int SendMessage( HandleRef hWnd, int msg,
int wParam, ref POINT lp );


//used to store the scroll position at the start of updating
POINT _scrollpos = new POINT();


//--------Add this code to BeginUpdate() after "if ( updating > 1 ) return;"----------

//store the current scrollposition
SendMessage(new HandleRef(this, Handle), EM_GETSCROLLPOS, 0, ref _scrollpos);
//------------------------------------------------------------------------------------


//--------Add this code to EndUpdate() after "if ( updating > 1 ) return;"------------

//restore the scrollposition
SendMessage(new HandleRef(this, Handle), EM_SETSCROLLPOS, 0, ref _scrollpos);
//------------------------------------------------------------------------------------
Left by jim on Nov 25, 2005 1:38 PM

# re: Extending RichTextBox
Requesting Gravatar...
I hate to tell you this, but number bullets isn't supported in the .NET Framework. Even 2.0 Framework doesn't have it.

To do bullets you need to do some unsafe calls so you can send a message to the richtextbox and tell it to use number/letter/normal bulleting.
Left by Scott Campbell on Feb 11, 2006 6:19 AM

# re: Extending RichTextBox
Requesting Gravatar...
OK. Here is some sample code for adding numbered bulleting to your richtextbox.

You need to add a few usings.
using System.Runtime.InteropServices;

Then a structure for paragraph formatting. .NET Framework only has PARAFORMAT (not the extended version)
[StructLayout(LayoutKind.Sequential)]
public class PARAFORMAT2
{
public int cbSize;
public int dwMask;
public short wNumbering;
public short wReserved;
public int dxStartIndent;
public int dxRightIndent;
public int dxOffset;
public short wAlignment;
public short cTabCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x20)]
public int[] rgxTabs;

public int dySpaceBefore; // Vertical spacing before para
public int dySpaceAfter; // Vertical spacing after para
public int dyLineSpacing; // Line spacing depending on Rule
public short sStyle; // Style handle
public byte bLineSpacingRule; // Rule for line spacing (see tom.doc)
public byte bOutlineLevel; // Outline Level
public short wShadingWeight; // Shading in hundredths of a per cent
public short wShadingStyle; // Byte 0: style, nib 2: cfpat, 3: cbpat
public short wNumberingStart; // Starting value for numbering
public short wNumberingStyle; // Alignment, Roman/Arabic, (), ), ., etc.
public short wNumberingTab; // Space bet 1st indent and 1st-line text
public short wBorderSpace; // Border-text spaces (nbl/bdr in pts)
public short wBorderWidth; // Pen widths (nbl/bdr in half twips)
public short wBorders; // Border styles (nibble/border)

public PARAFORMAT2()
{
this.cbSize = Marshal.SizeOf(typeof(PARAFORMAT2));
}
}


Then some code to do numbering.

#region PARAFORMAT MASK VALUES
// PARAFORMAT mask values
private const uint PFM_STARTINDENT = 0x00000001;
private const uint PFM_RIGHTINDENT = 0x00000002;
private const uint PFM_OFFSET = 0x00000004;
private const uint PFM_ALIGNMENT = 0x00000008;
private const uint PFM_TABSTOPS = 0x00000010;
private const uint PFM_NUMBERING = 0x00000020;
private const uint PFM_OFFSETINDENT = 0x80000000;

// PARAFORMAT 2.0 masks and effects
private const uint PFM_SPACEBEFORE = 0x00000040;
private const uint PFM_SPACEAFTER = 0x00000080;
private const uint PFM_LINESPACING = 0x00000100;
private const uint PFM_STYLE = 0x00000400;
private const uint PFM_BORDER = 0x00000800; // (*)
private const uint PFM_SHADING = 0x00001000; // (*)
private const uint PFM_NUMBERINGSTYLE = 0x00002000; // RE 3.0
private const uint PFM_NUMBERINGTAB = 0x00004000; // RE 3.0
private const uint PFM_NUMBERINGSTART = 0x00008000; // RE 3.0

private const uint PFM_RTLPARA = 0x00010000;
private const uint PFM_KEEP = 0x00020000; // (*)
private const uint PFM_KEEPNEXT = 0x00040000; // (*)
private const uint PFM_PAGEBREAKBEFORE = 0x00080000; // (*)
private const uint PFM_NOLINENUMBER = 0x00100000; // (*)
private const uint PFM_NOWIDOWCONTROL = 0x00200000; // (*)
private const uint PFM_DONOTHYPHEN = 0x00400000; // (*)
private const uint PFM_SIDEBYSIDE = 0x00800000; // (*)
private const uint PFM_TABLE = 0x40000000; // RE 3.0
private const uint PFM_TEXTWRAPPINGBREAK = 0x20000000; // RE 3.0
private const uint PFM_TABLEROWDELIMITER = 0x10000000; // RE 4.0

// The following three properties are read only
private const uint PFM_COLLAPSED = 0x01000000; // RE 3.0
private const uint PFM_OUTLINELEVEL = 0x02000000; // RE 3.0
private const uint PFM_BOX = 0x04000000; // RE 3.0
private const uint PFM_RESERVED2 = 0x08000000; // RE 4.0

public enum AdvRichTextBulletType
{
Normal = 1,
Number = 2,
LowerCaseLetter = 3,
UpperCaseLetter = 4,
LowerCaseRoman = 5,
UpperCaseRoman = 6
}

public enum AdvRichTextBulletStyle
{
RightParenthesis = 0x000,
DoubleParenthesis = 0x100,
Period = 0x200,
Plain = 0x300,
NoNumber = 0x400
}
#endregion

[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern IntPtr SendMessage(System.Runtime.InteropServices.HandleRef hWnd, int msg, int wParam, [In, Out, MarshalAs(UnmanagedType.LPStruct)] PARAFORMAT2 lParam);

private AdvRichTextBulletType _BulletType = AdvRichTextBulletType.Normal;
private AdvRichTextBulletStyle _BulletStyle = AdvRichTextBulletStyle.Plain;
private short _BulletNumberStart = 1;

public AdvRichTextBulletType BulletType
{
get
{
return _BulletType;
}
set
{
_BulletType = value;
}
}
public AdvRichTextBulletStyle BulletStyle
{
get
{
return _BulletStyle;
}
set
{
_BulletStyle = value;
}
}
public short BulletNumberStart
{
get
{
return _BulletNumberStart;
}
set
{
_BulletNumberStart = value;
}
}
private void NumberedBullet(bool TurnOn)
{
PARAFORMAT2 paraformat1 = new PARAFORMAT2();
paraformat1.dwMask = (int)(PFM_NUMBERING | PFM_OFFSET | PFM_NUMBERINGSTYLE | PFM_NUMBERINGSTART | PFM_NUMBERINGTAB);
if (!TurnOn)
{
paraformat1.wNumbering = 0;
paraformat1.dxOffset = 0;
}
else
{
paraformat1.wNumbering = (short)_BulletType;
paraformat1.dxOffset = rtbData.BulletIndent;
paraformat1.wNumberingStyle = (short)_BulletStyle;
paraformat1.wNumberingStart = _BulletNumberStart;
}
SendMessage(new System.Runtime.InteropServices.HandleRef(rtbData, rtbData.Handle), 0x447, 0, paraformat1);
}



I am sorry if this code doesn't work straight away. It was a cut and paste from a AdvRichTextBox that I am working on for a project. Hopefully it will set you on the right track though.
Left by Scott Campbell on Feb 11, 2006 6:26 AM

# re: Extending RichTextBox
Requesting Gravatar...
Jim, i'm not sure if your code will work properly after the Richedit box has been resized by user?
Left by Max on Feb 15, 2006 11:58 AM

# re: Extending RichTextBox
Requesting Gravatar...
I'm trying to add annotations to a rtb with limited success. I'm simply embedding panel in the rtb. It scrolls H & V just fine, but when zoom is changed I can't get the panel to scale appropriately. Could you provide some insight on how to do this? I could send a sample app to try an show what I'm doing if necessary...
Left by J. K. Johnson on Feb 23, 2006 3:28 PM

# re: Extending RichTextBox
Requesting Gravatar...
Hi, could you tell me how did you include annotations in a RichTextBox? When I paste some RTF code with annotations in a RTB, the annotations are lost. I mean, the .rtf property of the control does not have those annotations anymore.
Left by M. Drzazga on Mar 15, 2006 4:38 PM

# re: Extending RichTextBox
Requesting Gravatar...
this is very useful. i'm trying the linespacing for richtectbox using this coding. but no changes. here You are used Unmanged code. i want managed code.
Left by jeya on Jun 13, 2006 4:59 AM

# re: Extending RichTextBox
Requesting Gravatar...
i want to format the richtextbox content: it's very uregent. Pls..
Text Orientation->top-bottom,right-left...like as content alignment
bullets and numbering
oulinelevel-body,level1,level2..
line spacing-single,double,1.5
para -after ,before
Left by Naga on Jun 14, 2006 8:22 AM

# re: Extending RichTextBox
Requesting Gravatar...
I have been experimenting with linespacing, using the flags, etc outlined on the MS web page and I can get linespacing to work using SETPARAFORMAT and I can see the linespacing after using GETPARAFORMAT but I get zero linespacing USING GETPARAFORMAT without first using SETPARAFORMAT.
That doesn't make sense to me because it would seem logical to GET the linespacing before changing it with SET.
I would appreciate some advice on this.
Brian.
Left by Brian Reynolds on Oct 28, 2006 12:04 AM

# re: Extending RichTextBox
Requesting Gravatar...
We are working on a .NET (C# WinInforms) windows application where our customer has a requirement to have Word Style Editor. Therefore we are using RichTextBox . But we have the following issues with the RichTextBox.

1) Unable to implement “Justify” text like in Microsoft Word Application.
2) Programmatic Page breaking between two pages.
3) Bulleting and Numbering inside RichTextBox

Could you please help me on the above.

Thanks
Isaac
Email - baby.isaac@wipro.com
Left by Isaac on Mar 08, 2007 3:44 PM

# re: Extending RichTextBox
Requesting Gravatar...
Thank you Sooo Much! for your excellent Extending RichTextBox articles -
very-helpful.
Can you help me out please,how to add numbers,bullets in a richtextbox.

Thanks in advance.
Mahesh.Naragund
Left by Mahesh.N on Mar 09, 2007 3:19 AM

# How to keep the richtextbox size constant(not extentable)
Requesting Gravatar...
I am perplexed with a scenario in richtext box. Hope this is the right forum to post it!!


I have a richtextbox in a form and through crystal reports i am taking the text/picture entered in the richtextbox for exporting the report in pdf. The form has the options to change font, size etc of the entered text.

My requirement is that the richtextbox in the form should not be extendable vertically(word wrap is already set to True) beyond a prefixed limit.

i.e. The User should be capable of entering text only in that
particular area of the richtextbox, afterwhich he should not be able to
enter any more data/ image.
(The concept of line number doesnot work because, the fonts may be of different type and sizes in the richtextbox)

Thanks in advance
Ranjith
Left by K P Ranjith on May 17, 2007 6:58 AM

# re: Extending RichTextBox
Requesting Gravatar...
Why not just implement the Text Object Model interfaces. It 's just like using MS Word ;-). Export them from the RichEdit20 dll and then import the COM types as source code into Visual Studio using .Net Reflector. Trust me it's worth it.
Left by Neal on Nov 21, 2007 1:15 AM

# re: Extending RichTextBox
Requesting Gravatar...
i m using vb.net, can u plz write the justification code in vb.net
Left by sada on Dec 23, 2007 1:22 AM

# re: Extending RichTextBox
Requesting Gravatar...
Hi, i have a question concerning the richtextbox control.
i will like the richtextbox to detect in a Text some text with a given pattern (for instance FF_Text1, FF_Text34)
as a hyperlink, so that i can click the link to open with the Process class a new document on
my hard drive.
i will be so kind if anyone knows how to solve this problem.
Thanks
Gervais
Left by Gervais on Jan 22, 2008 9:25 AM

# re: Extending RichTextBox
Requesting Gravatar...
Nice code. I want to show copy paste dialog on richtextbox. Any idea how I can do that?

Also if my form has one accept button richtextbox does not handle enter click, really appreciate any help on handling this...
Left by Pritesh Ostwal on Feb 02, 2008 10:16 AM

# re: Extending RichTextBox
Requesting Gravatar...
Thanks you so much for your helpful extended RichTextBox.
It works nicely.
It's exactly what I was looking for ! :-)
Left by DagVador on Feb 19, 2008 3:16 AM

# re: Extending RichTextBox
Requesting Gravatar...

the advanced rich text works properly for justification, thanks a lot
but when the last line is not completed, it is viewed aligned left
what should i do to make that last line aligned right not left

also can i add a line at the begining that is aligned center ?
how could i do this ??

thanks in advance
Left by High Mountain on Feb 23, 2008 2:40 AM

# re: Extending RichTextBox
Requesting Gravatar...
Hi
Thats nice job, but a i wanted Right justify ricj text box , in your project when set justfy only show in left align justfy and could not show right align , how do i set right justify ? please help me too do this
thanks so much
Left by Ehsan on Mar 11, 2008 3:22 AM

# re: Extending RichTextBox
Requesting Gravatar...
I use your adv rtf control , it works veryfine.

And I have a problem with scrolling. My requirement is when mousescroll the form scorll should work. For that I used the following code.

private const UInt32 WM_MOUSEWHEEL = 0x020a;


protected override void WndProc(ref Message m)
{
//Console.WriteLine(m.ToString());
// Pass WM_MOUSEWHEEL to parent
//source http://www.msdner.com/dev-archive/205/2-8-2056365.shtm
if (m.Msg == WM_MOUSEWHEEL)
{
SendMessage(this.parent.Handle, m.Msg, m.WParam, m.LParam);
m.Result = (IntPtr)0;
}
else
{
base.WndProc(ref m);
}
}

It is working fine when there is no code for Bulleting or Justification.

With Bulleting or Justification this responds strange.

When we scroll first form scrolls and then the scroll goes upward to the richtextbox control.

How can i get out of this?
I would appreciate some advice on this.

Thanks in advance

regards
Jaison Thomas
Left by Jaison Thomas on Apr 18, 2008 1:12 AM

# re: Extending RichTextBox
Requesting Gravatar...
Has someone put all these suggestions and code together ?

I have an other question: How can I set (Add) a Font style to text which alreade has other styles?
e.g.: I have a selcted text wich contains lets say Bold and Underline Text.
Now I would like to add the 'italic' style to the text so that the bold written text now has 'bold and italic' and the undelined text is now 'underline and italic'

My first try was
.selectedText.font = new font(selectedText.font, selectedText.font.style OR Italic)
This works well as long as there are no mixed font-styles is it.

Any Ideas?

Left by Thorsten on Apr 18, 2008 3:17 AM

# re: Extending RichTextBox
Requesting Gravatar...
Hi,

I'm working on C#.Net.We are constructing IDE where i required transparent text control which i inherited from RichTextBox but I'm facing one problem.I want to incorporate TextAlign property which RichTextBox control don't have.
How can I achieve this? Please help me in this regard.

Thanks.
Left by KnowledgeMatters on Apr 18, 2008 7:31 AM

# re: Extending RichTextBox
Requesting Gravatar...
This might help some people like it helped me:

private const int EM_SETEVENTMASK = 1073;

may not work for all situations...

private const int EM_SETEVENTMASK = 1093;

seems to cover more bases.

The event is defined in many places as WM_USER + 69 which is 1024 + 69 = 1093.

This prevented my databound text box from firing 'OnTextChanged' messages to the underlying data object. This then called validation code which was non-trivial. Changing the event value prevented this.

Other than that, this article was a godsend, thanks!
Left by Gary Hall on May 07, 2008 6:14 AM

# re: Extending RichTextBox
Requesting Gravatar...
How to view the print preview
Left by Worried on Oct 16, 2008 5:43 AM

# re: Extending RichTextBox
Requesting Gravatar...
Thank you for justify.But please help me how i can set last line alignment to right.
Left by Bonyadi on Nov 01, 2008 5:17 AM

# re: Extending RichTextBox
Requesting Gravatar...
To add mixed styles do somthing like this
In C#:
FontStyle rtfStyle = rtfDoc.selectedText.font.style | FontStyle.Italic;

rtfDoc.selectedText.font = new Font(fontName,fontSize,rtfStyle);


selectedText.font = new font(selectedText.font, selectedText.font.style OR Italic)
Left by adnan el-shaer on Dec 12, 2008 5:11 AM

# re: Extending RichTextBox
Requesting Gravatar...
Thank you!
Left by Jadan Bliss on Dec 16, 2008 11:34 PM

# re: Extending RichTextBox
Requesting Gravatar...
Hi,
I have used most of the code here from Pete and Scott with great sucess and I would like to thank them both. It has saved me a lot of time.

I have a few issues I'm working on that someone might like to help me with if they have already found a solution:

Dropping (as in dragdrop) images onto the textbox though seems to be a problem. I can detect the drop in OnDragDrop() convert the image to a bitmap, add it to the clipboard then paste it rtb.Paste() or insert it as as a rtf string with metafile, but whatever I always end up with the last file in the filedrop inserted as a blank image with the image type as text, ie. the standard rtb image drop. Anyone know how to drop an image sucessfully?

An image when selected has nice resize handles and the image resizes as expected, but the cursor remains as the Ibeam and not the various resize cursors that the user would expect. Anyone solved this problem?

And of course it would be wonderful to find a way to wrap the text around an embedded image and not simply have it aligned with the text, but I suspect this is beyond RTF, unless anyone can tell me otherwise.

Thanks again
Brian
Left by Brian Akehurst on Jan 04, 2009 6:13 AM

# re: Extending RichTextBox
Requesting Gravatar...
Hi for inserting a page preak use the following

Dim CurrentRTF As String

'inserts a pagebreak at current position
CurrentRTF = Mid(txtPrintLetter.SelectedRtf, 1, Len(txtPrintLetter.SelectedRtf) - 3)
txtPrintLetter.SelectedRtf = CurrentRTF & " \page\par }"
Left by Mark on Jan 23, 2009 7:06 AM

# re: Extending RichTextBox
Requesting Gravatar...
Hi All,
I am developing a control which extends from RichTextBox.
Does anyone knows how to restrict bulleting which occurs after Ctrl + Shift + L in RichTextBox.

Thanks in advance....
Left by makdev on Feb 27, 2009 7:34 AM

# re: Extending RichTextBox
Requesting Gravatar...
Sorry! Can you give an example of how to call this TextAlign.

New to C#, and would like an example.

Thanks.
Left by Robert on Mar 04, 2009 11:23 PM

# re: Extending RichTextBox
Requesting Gravatar...

AdvRichTextBox text;

text.SelectionAlignment = TextAlign.Left;
Left by Carlos on Mar 11, 2009 8:42 AM

# re: Extending RichTextBox
Requesting Gravatar...
Just copied your code and translated it to VB.Net 2008

Unable to make it work.

Any chance you could email me a small sample on how this is to function. I like the part about Bullets, and the Justify of the text. Just cannot see how to make it work.

Thanks in advance.
Left by Robert on Mar 22, 2009 5:43 PM

# re: Extending RichTextBox
Requesting Gravatar...
As did Robert, I ported this code to VB.NET 2008. I did not get any errors compiling, but when I use the BeginUpdate and EndUpdate, the text i type does not display in the text box.
Left by Peter on Mar 24, 2009 4:18 PM

# re: Extending RichTextBox
Requesting Gravatar...
Using BeginUpdate/EndUpdate, I was having all sorts of odd display problems, such as duplicate cursors and the extra text being underlined.

I was able to solve the problem by adding "Invalidate(true);" at the very end of EndUpdate(). I got the idea from the article you linked to, which invalidates the control in its equivalent C++ Win32 code.
Left by Daniel Stutzbach on May 19, 2009 2:44 PM

# re: Extending RichTextBox
Requesting Gravatar...
Does anyone know how to display text that contains more than 4096 characters as one line in RichTextBox?
Left by Ed on Aug 04, 2009 7:37 PM

# re: Extending RichTextBox
Requesting Gravatar...
how to add new line rtf format data in richtext box.
my database is sql server2000, data type is ntext
i m using this code but it is not working
richtextbox1.text+=ds.tables("qb").rows(0).item("Question").tostring & vbnewline
it add only text format data. i wanted text format data as well as rtf format data also
plz help
Left by Ratnesh on Sep 23, 2009 5:33 AM

# re: Extending RichTextBox
Requesting Gravatar...
I also converted the code justification in (Vb.net) VS 2008 and it does not seems to work. Can u kindly post me the code for VS 2005. Thanks in advance.

Left by Kamal on Nov 16, 2009 3:18 AM

# re: Extending RichTextBox
Requesting Gravatar...
Hola, muchas gracias, esta excelente este aporte...
Left by Oscar Quintero on Nov 21, 2009 10:10 AM

# re: Extending RichTextBox(Adding Functions)
Requesting Gravatar...
I used .NET refactor to pull the printing code from the microsoft .Net RichTextBoxPrintControl, and added it to this code to make an a entirely new RTB control. If anyone wants it, i can email it to them when i finish it. Thanks for thisd wonderful code.
Left by Fullmetal on Mar 12, 2010 12:39 PM

# re: Extending RichTextBox @ Scott Campbell
Requesting Gravatar...
I tried to use the numbered bullets code, however it says that rtbData doesn't exist, and i don't know where to find it/reference it.
Left by Fullmetal on Apr 09, 2010 7:20 PM

# re: Extending RichTextBox
Requesting Gravatar...
Extending RichTextBox shows paragraph breaks?
Left by Desbaratizador on Apr 21, 2010 12:47 PM

# re: Extending RichTextBox
Requesting Gravatar...
how can i add the justification code to my program? I do not know where will I put that code..
Left by louie on May 23, 2010 11:13 AM

# re: Extending RichTextBox
Requesting Gravatar...
Thank's a lot buddy for this code.
Can I get ur email id?
Left by Tony on Jul 11, 2010 12:29 PM

# re: Extending RichTextBox
Requesting Gravatar...
hi i am new to C#.net,i have a small problem i want to apply numbering for selected text in rich text box ,like the property richtextBox.selectionbullet=true.

thanks in advance if any one helps me
Left by srinivas on Jul 12, 2010 6:03 AM

# re: Extending RichTextBox
Requesting Gravatar...
Thanks for the article, works like a charm.
However, when using RightToLeft fonts it doesn't work any more.
Does anyone have any insights on how to solve this issue?
Left by Ovi on Aug 03, 2010 8:45 AM

# re: Extending RichTextBox
Requesting Gravatar...
Hello
It is possible to have the code in VB.net?
thanks in advance
Left by Angelo on Aug 08, 2010 12:52 PM

# re: Extending RichTextBox
Requesting Gravatar...
How can i implement it for righttoleft languages?
Left by Fedman Dov on Aug 29, 2010 12:46 AM

# re: Extending RichTextBox
Requesting Gravatar...
I happened across this looking for "RichTextBox justify." I decided I didn't need justification, but the code for speeding up RTB highlighting was of interest. In an earlier project I gave up on coloring the RTB directly, and resorted to grabbing the rtf as a string and inserting the codes into the string and then loading the resulting string back into the RTB. No events involved there, and it's still slow on large files. But, for the benefit of any other soul who should happen this way, here is code for faster updating which works:
(using System.Runtime.InteropServices;)

private const int WM_SETREDRAW = 0x000B;
private const int WM_USER = 0x400;
private const int EM_GETEVENTMASK = (WM_USER + 59);
private const int EM_SETEVENTMASK = (WM_USER + 69);

[DllImport("user32", CharSet = CharSet.Auto)]
private extern static IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);

IntPtr oldEventMask = IntPtr.Zero;
int updating = 0;

public void BeginUpdate()
{
// Deal with nested calls.
++updating;

if (updating > 1)
return;

// Prevent the control from raising any events.
oldEventMask = SendMessage(rtb.Handle,
EM_SETEVENTMASK, 0, IntPtr.Zero);

// Prevent the control from redrawing itself.
SendMessage(rtb.Handle,
WM_SETREDRAW, 0, IntPtr.Zero);
}

public void EndUpdate()
{
// Deal with nested calls.
--updating;

if (updating > 0)
return;

// Allow the control to redraw itself.
SendMessage(rtb.Handle,
WM_SETREDRAW, 1, IntPtr.Zero);

// Allow the control to raise event messages.
SendMessage(rtb.Handle,
EM_SETEVENTMASK, 0, oldEventMask);
}

Note that rtb is my RichTextBox, if you were extending one you would use this.Handle.
Left by Jim on Apr 27, 2011 10:42 AM

# re: Extending RichTextBox
Requesting Gravatar...
The Justify functionality is not working ...

and all working fine

thank you ...
Left by sanjay jangid on Jul 06, 2011 1:49 AM

# re: Extending RichTextBox
Requesting Gravatar...
@Scott Campbell

Hi, in your comment "re: Extending RichTextBox 2/11/2006 6:26 AM Scott Campbell" for adding numbered bulleting to your richtextbox! don't work there is an error in

"paraformat1.dxOffset = rtbData.BulletIndent; "

"rtbData" dont' exist!!!!

please help me and sorry for my basic english!

Angelo
Left by Angelo on Nov 01, 2011 1:10 PM

# re: Extending RichTextBox
Requesting Gravatar...
The code works well with Windows 7 OS on my desktop; but it does not behave as expected on my laptop (the full justification code does not work on other laptops running Windows 7 professional version either).

I compiled the code of my app on the desktop then copied the .EXE to a USB drive. Next I ran the program directly from the USB on the desktop; the full justification feature worked. However, this feature on the USB failed on all Windows 7 laptops that I tested.
Left by LThai on Nov 21, 2011 10:52 PM

# re: Extending RichTextBox
Requesting Gravatar...
This is a wonderfull article, justify property is right, I believe that it dont working, but if you set extended RichTextBox property RightToLeft = yes and set property SelectionAligment = Justify, its text is justify.

Thanks
Alejandro
Left by Alejandro on Jan 06, 2012 11:00 AM

# re: Extending RichTextBox
Requesting Gravatar...
Well played, my friend. Well played. Thanks for the solution.
Left by J. Appleseed on Jun 26, 2012 2:01 PM

# re: Extending RichTextBox
Requesting Gravatar...
Hello,

I need to be able to rotate the AdvRichTextBox. The actual box rotates but the text inside does not. Any ideas of why? thanks!
Left by Rodrigo Jimenez on Jul 16, 2012 9:34 AM

# re: Extending RichTextBox
Requesting Gravatar...
I love you. It works perfectly 100/100. You are superb man.
Left by Pankaj Rajput on Feb 20, 2013 11:45 PM

# re: Extending RichTextBox
Requesting Gravatar...
I copied the code, but my text inside the box is still not intended, I have tried manually setting values to the:
dxRightIndent,
dxStartIndent,
dySpaceAfter
dySpaceBefore
of the paraformat object, but still no avail.

Left by Big Fan on Mar 03, 2013 11:10 PM

# re: Extending RichTextBox
Requesting Gravatar...
Thank you.......

I have some Problem in vb.I hope that you Should Clear my Problem.I want to Extend my Richtextbox as we extend the form in run-time.And also help me how we select a control at run time as it appear in design mode?

Sudhir Roy
+919563505904
Left by Sudhir Roy on Mar 25, 2013 4:46 PM

Your comment:
 (will show your gravatar)


Copyright © Pete Vidler | Powered by: GeeksWithBlogs.net | Join free