Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / November 2007

Tip: Looking for answers? Try searching our database.

caret position in custom TextBox inhereting from UserControl

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rachel@bluetang.co.uk - 07 Nov 2007 22:35 GMT
Hi everyone,

I'm writing a custom TextBox that inherits from UserControl. I can't
use a standard TextBox, or inherit from it or TextBoxBase, as it needs
to support proper transparency, meaning both the background needs to
be transparent, and the foreground back colour (I'm finding that if I
set the background to transparent, and the foreground to black, text
is being rendered as black on white, which rather does away with the
transparency)

I need to draw,( in the OnPaint method) a caret at the correct
position, but can't really get it to work. Have tried computing the
position using graphics.MeasureCharacterRanges, but without luck

any help or tips would be appreciated

thanks in advance

rachel
Bob Powell [MVP] - 10 Nov 2007 22:11 GMT
Win32 has a Caret API that can be used to "correctly" position the caret in
the control.

Here are the DLL imports you can use...

[DllImport("user32.dll")]

public static extern int CreateCaret(IntPtr hwnd, IntPtr hbm, int cx, int
cy);

[DllImport("user32.dll")]

public static extern int DestroyCaret();

[DllImport("user32.dll")]

public static extern int SetCaretPos(int x, int y);

[DllImport("user32.dll")]

public static extern int ShowCaret(IntPtr hwnd);

[DllImport("user32.dll")]

public static extern int HideCaret(IntPtr hwnd);

You still have to determine the text positions which you can do using
MeasureCharacterRanges.

Signature

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

> Hi everyone,
>
[quoted text clipped - 15 lines]
>
> rachel

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.