Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to change Caret in TextBox?

277 views
Skip to first unread message

Dave Leach

unread,
Oct 13, 2003, 4:32:42 PM10/13/03
to
Is it possible to change the TextBox insertion point
marker (caret) from a blinking vertical line to a blinking
box?

Is it possible to change the caret to some other shape
that I define?

Thanks for any help or pointers.

Dave

Herfried K. Wagner [MVP]

unread,
Oct 13, 2003, 5:23:39 PM10/13/03
to
* "Dave Leach" <ba...@agilent.com> scripsit:

> Is it possible to change the TextBox insertion point
> marker (caret) from a blinking vertical line to a blinking
> box?
>
> Is it possible to change the caret to some other shape
> that I define?

You will find a VB6 sample here:

<http://www.activevb.de/tipps/vb6tipps/tipp0192.html>

You will have to translate it to .NET (untested).

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Dave Leach

unread,
Oct 13, 2003, 7:42:57 PM10/13/03
to
Herfried,

Thanks for the pointer. Although it is in German and VB6,
I think I get the idea. Basically I will need to invoke
functions from the user32 lib when the control gets focus.

Anybody else have suggestions or pointers to a C# example
or explanation in English?

Thanks,
Dave

>.
>

Ying-Shen Yu[MSFT]

unread,
Oct 14, 2003, 2:18:43 AM10/14/03
to
Hi Dave,
I wrote a small sample in C# for you, you can use the Interop declaration
in the sample.
After you click the ShowCaret Button, you can see a caret in the textbox.
But it is just a sample which shows how to use Caret-related API in .NET
if you want to show your own caret in TextBox, you must derive a new
control and process the get focus and lost focus event on your own, since
the DefWndProc of TextBox will always Create the default caret.

If you still have problems on this issue, please let me know!
Thanks for using MSDN Newsgroup.


<code>
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace WindowsForm_OwnerCaret
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Drawing.Bitmap bm;
private IntPtr hBitmap;
private int blinkTime;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button showCaret;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox2 = new System.Windows.Forms.TextBox();
this.showCaret = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(112, 24);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 2;
this.textBox2.Text = "textBox2";
//
// showCaret
//
this.showCaret.Location = new System.Drawing.Point(16, 24);
this.showCaret.Name = "showCaret";
this.showCaret.TabIndex = 4;
this.showCaret.Text = "ShowCaret";
this.showCaret.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(256, 69);
this.Controls.Add(this.showCaret);
this.Controls.Add(this.textBox2);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
bm = new Bitmap("app.ico");
hBitmap = bm.GetHbitmap();
}

private void button1_Click(object sender, System.EventArgs e)
{
NativeMethod.CreateCaret(this.textBox2.Handle,hBitmap,16,16);
NativeMethod.ShowCaret(this.textBox2.Handle);
}
}
class NativeMethod
{
[DllImport("user32.dll")]
public extern static int GetCaretBlinkTime();
[DllImport("user32.dll")]
public extern static int SetCaretBlinkTime(int wMSeconds);
[DllImport("user32.Dll")]
public extern static int GetCaretPos(ref POINT pt);
[DllImport("user32.dll")]
public extern static int SetCaretPos(int x, int y);
[DllImport("user32.Dll")]
public extern static int DestroyCaret();
[DllImport("user32.dll")]
public extern static int CreateCaret(IntPtr hwnd, IntPtr hBitmap, int
nWidth, int nHeight);
[DllImport("user32.dll")]
public extern static int ShowCaret(IntPtr hwnd);
[DllImport("user32.dll")]
public extern static int HideCaret(IntPtr hwnd);

[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
}
}
}
</code>

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| Content-Class: urn:content-classes:message
| From: "Dave Leach" <ba...@agilent.com>
| Sender: "Dave Leach" <ba...@agilent.com>
| References: <067501c391c9$26fd14c0$a401...@phx.gbl>
<bmf59a$m9bj5$3...@ID-208219.news.uni-berlin.de>
| Subject: Re: How to change Caret in TextBox?
| Date: Mon, 13 Oct 2003 16:42:57 -0700
| Lines: 34
| Message-ID: <0a5701c391e3$bae5ec60$a301...@phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: quoted-printable
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOR47rldO2CHK3sRBuhKWD/q0akxQ==
| Newsgroups: microsoft.public.dotnet.framework.windowsforms.controls
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms.controls:11462
| NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms.controls

Dave Leach

unread,
Oct 15, 2003, 7:45:38 PM10/15/03
to
Ying-Shen,

Thanks for the example code below. It provided enough
information for me to get the desired result.

Thanks again,
Dave

>.
>

0 new messages