Click here to Skip to main content
15,889,116 members
Articles / Desktop Programming / WPF
Tip/Trick

Caret for WPF User Controls

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
30 Jul 2012CPOL 19.4K   5   8
A simple Caret for WPF User Controls

Introduction  

In this article I present a simple Caret which I have successfully used in a couple of my WPF projects. I hope that it can be useful for others too. 

Background 

WPF does not provide a Caret control for User Controls. There is a CaretElement, but it is internal to the framework and undocumented. The presented code can be easily adapted for WinForms as well.

Using the code  

Using the caret is pretty straightforward. You only have to create an instance of the Caret and add it as a child to your user control. In order to move the Caret around, you will have to change its Top and Left properties. You can change Caret's height using the CaretHeight property.

C#
public class MyControl : UserControl 
{ 
    Caret caret = new Caret(); 
    public MyControl() 
    {
        AddChild(caret);
        caret.Top = 100;
        caret.Left = 100;
    }
}

And here is the Caret class:

C#
public class Caret : FrameworkElement
{
    System.Threading.Timer timer;
    Point location;
    public double CaretHeight { get; set; }
    int blinkPeriod = 500;
    Pen pen = new Pen(Brushes.Black, 1);

    public static readonly DependencyProperty VisibleProperty = 
      DependencyProperty.Register("Visible", typeof(bool), 
      typeof(Caret), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));

    public Caret()
    {
        pen.Freeze();
        CaretHeight = 18; 
        Visible = true;
        timer = new System.Threading.Timer(blinkCaret, null, 0, blinkPeriod);
    }

    protected override void OnRender(DrawingContext dc)
    {
        if (Visible)
        {
            dc.DrawLine(pen, location, new Point(Left, location.Y + CaretHeight));
        }
    }

    bool Visible
    {
        get
        {
            return (bool)GetValue(VisibleProperty);
        }
        set
        {
            SetValue(VisibleProperty, value);
        }
    }

    void blinkCaret(Object state)
    {
        Dispatcher.Invoke(new Action(delegate { Visible = !Visible; }));
    }
            
    public double Left
    {
        get { return location.X; }
        set
        {
            if (location.X != value)
            {
                location.X = Math.Floor(value) + .5; //to avoid WPF antialiasing
                if (Visible)
                {
                    Visible = false;
                }
            }
        }
    }

    public double Top
    {
        get { return location.Y; }
        set
        {
            if (location.Y != value)
            {
                location.Y = Math.Floor(value) + .5; //to avoid WPF antialiasing
                if (Visible)
                {
                    Visible = false;
                }
            }
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead https://mathiversity.com
Unknown
I am a full-stack developer. My skills include JavaScript, C#/.Net, MS Azure cloud etc. I love to work on complex programming tasks requiring deep analysis, planning and use of efficient algorithms and data structures.

Comments and Discussions

 
QuestionCaret of a JavaApp? Pin
Member 1053655315-Mar-18 6:56
Member 1053655315-Mar-18 6:56 
QuestionCaret implementation Pin
DerekTremblay1-Oct-17 9:04
DerekTremblay1-Oct-17 9:04 
AnswerRe: Caret implementation Pin
Kashif_Imran10-Oct-17 5:45
Kashif_Imran10-Oct-17 5:45 
GeneralRe: Caret implementation Pin
DerekTremblay28-May-19 17:27
DerekTremblay28-May-19 17:27 
GeneralRe: Caret implementation Pin
Kashif_Imran11-Jun-19 0:40
Kashif_Imran11-Jun-19 0:40 
Questionhow to move the caret Pin
ts8915-Aug-12 1:15
ts8915-Aug-12 1:15 
AnswerRe: how to move the caret Pin
Kashif_Imran15-Aug-12 4:20
Kashif_Imran15-Aug-12 4:20 
GeneralMy vote of 4 Pin
Christian Amado30-Jul-12 2:26
professionalChristian Amado30-Jul-12 2:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.