Drawing a Border for a textbox

  • Thread starter Thread starter Murali Valluri
  • Start date Start date
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);
}
 
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.
 
Back
Top