Drawing a Border for a textbox

M

Murali Valluri

I have tried the following, which draws the border but clears the Text
when the control does not have focus.
Please help.
----------------------------------------------------------
1. Set the ControlStyle to "UserPaint" in the constructor:

public MyTextBox()
{
// This call is required by the Windows.Forms Form Designer.
this.SetStyle(ControlStyles.UserPaint,true);
InitializeComponent();

// TODO: Add any initialization after the InitForm call
}

2. Add the following code to the overrided OnPaint method.

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
int borderWidth = 1;
Color borderColor = Color.Blue;

ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, borderColor,
borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth,

ButtonBorderStyle.Solid, borderColor, borderWidth,
ButtonBorderStyle.Solid,

borderColor, borderWidth, ButtonBorderStyle.Solid);
}
 
V

Vadim Chekan

Murali said:
I have tried the following, which draws the border but clears the Text
when the control does not have focus.
Please help.

Hmm, Interesting.
As soon as you set
this.SetStyle(ControlStyles.UserPaint,true)
you have to take over all aspects of drawing, including text output.
So you have to
e.Graphics.DrawString(Text,this.Font,br,0,0);
But when user edit text it looks incosistens, and when user leave it, no
repaint is called. I see that .net control is strange mix of wrappers of
windows native gui calls.

Vadim Chekan.
 

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