How to draw selected text

I am writing a custom replacement for Textbox / Label controls. It should provide similar functionality, but it will be optimized and (hopefully) much faster in the context of my application. It must support multi-line text. I am wondering if there is an easy way to make the selected text? Right now, it seems the only way is to figure out where the selection rectangle should be drawn, and draw it manually. I was hoping to get recommendations on the easiest way to do this or any alternative methods. Thanks.

+4
source share
2 answers

Use System.Windows.Forms.TextRenderer .

Just override the methods from System.Windows.Forms.Control :

 protected override void OnPaint(PaintEventArgs e) { TextRenderer.DrawText(e.Graphics, Text, Font, new Rectangle(0, 0, Width, Height), ForeColor); } public override Size GetPreferredSize(Size proposedSize) { return TextRenderer.MeasureText(Text, Font); } 

Of course, you will need to handle a couple more events, such as OnFontChanged or OnSizeChanged , if you need to perform individual behavior.

If what you are looking for is for specific text (selected text) in these controls, you can take a look at this article . The source code for the spelling check library (SharpSpell) is also associated with this article.

+2
source

Rewriting the basic controls for an application is an important task and should not be performed unless it is an absolute last resort (IMO). If the included controls do not meet your exceptions, I would recommend purchasing a third-party control library in which their business is built around designing and supporting these controls. I have had a lot of success using ASP.NET Telerik Controls. I have seen many positive statements about my other control lines.

There are also many other sources of third-party controls that you could learn.

0
source

All Articles