Share via


Change border color of custom inherit textbox

Question

Thursday, July 28, 2011 4:59 AM

Dear all,

I have below code, how to change the border color of textbox custom controls??

public class UC_MaskedTextBox : System.Windows.Forms.TextBox
    {
        public UC_MaskedTextBox()
        {
            this.BorderStyle = System.Windows.Forms.BorderStyle.None;
          
        }

All replies (5)

Tuesday, August 2, 2011 10:55 PM âś…Answered

Hi,

Please check the codes following:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class myTextbox : System.Windows.Forms.TextBox
{

private System.Drawing.Color m_BorderColor = System.Drawing.Color.Red;
public System.Drawing.Color GetBorderColor()
{

return this.m_BorderColor;

}

public bool SetBorderColor(System.Drawing.Color clrBorder)
{

this.m_BorderColor = clrBorder;

this.Invalidate();

}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

Pen penBorder = new Pen(this.GetBorderColor(), 1);

Rectangle rectBorder = new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1);

e.Graphics.DrawRectangle(penBorder, rectBorder);

}

}

Best Regards,

Damon


Thursday, July 28, 2011 7:52 AM

 this.BorderColor = System.Drawing.Color.Red;


Thursday, July 28, 2011 8:03 AM

First,

if you want no border then why do you want to change the border color?

If you want to change color then you need to set border style from none to some other value. may be Solid.

Then you can change the border color.


Thursday, July 28, 2011 9:15 PM

It does not have BorderColor properties. I can only set as below: How to do then??

 this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;


Wednesday, August 3, 2011 12:50 AM

txtbox.BorderStyle = BorderStyle.Solid;

txtbox.BorderColor=System.Drawing.ColorTranslator.FromHtml("#0000A0");

OR

txtbox.BorderColor = System.Drawing.Color.Blue;