Custom TextBox

G

Guest

Hi,

I would like to paint the text and the background of the .NET textbox
myself. I figured out that Win32 does all the painting there, so it seems
there's not so much I can do about this. It seems that the only solution is
to make my own custom control.

Can anyone point me to custom textbox controls, or did anyone find a trick
to paint the textbox themselves?
 
S

Simon Tamman {Uchiha Jax}

You want something along these lines I guess....

public class PaintedTextBox : TextBox
{

public PaintedTextBox()
{
SetStyle(ControlStyles.UserPaint, true);
}

protected override void OnPaint(PaintEventArgs e)
{
//// just a test to show we are actually drawing the textbox
e.Graphics.FillRectangle(Brushes.Blue, e.ClipRectangle);
/// commented out
//base.OnPaint (e);
}

protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged (e);
/// make sure it repaints when text is changed
this.Invalidate();
}
}

You might want to also run
SetStyle(ControlStyles.DoubleBuffer, true);
to prevent flicker.

HTH

Jax
 
G

Guest

I've tried to do this before, but I found a couple of problems there:
It seems that the caret is still painted. It is also painted on the wrong
position. I am trying to draw the text antialiased, and the caret seems to be
running ahead. Also: it seems that the original text is still printed, it is
often printed under or over my text, for instance, when I try to select text
with my mouse.

I've been browsing the internet for quite some time now and everyone
suggests me to use the userpaint style, but I have found noone who actually
succeeded in building their own, completely userpainted textbox. Or am I
missing something here?

It will also be a pain to support all the standard textbox features, btw.
But I am willing to do so.

(I am using the 2.0 framework from RC1)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top