Click here to Skip to main content
15,886,060 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi all,

I need to change the Textbox Border Color at the time of Textbox Click.
If any body knows how to do this then please let me know.

I tried it in form_load event but it applies for all textboxes in the form.

I want it in one particular Textbox click.


Regards,
Pawan.
Posted
Updated 18-Mar-23 1:14am
v2
Comments
Dalek Dave 22-Jul-10 6:20am    
Edited for readability.

In order to work with the events of the inner TextBox, directly from your UserControl, you can expose the events that you need. Here is an example implementation of this idea:
C#
public partial class TextBoxEx : UserControl
{
    // The TextBox
    private TextBox textBox = new TextBox();

    // Border color of the textbox
    private Color borderColor = Color.Gray;

    // Ctor
    public TextBoxEx()
    {
        InitializeComponent();
        this.Paint += new PaintEventHandler(TextBoxEx_Paint);
        this.Resize += new EventHandler(TextBoxEx_Resize);
        textBox.Multiline = true;
        textBox.BorderStyle = BorderStyle.None;
        this.Controls.Add(textBox);

        InvalidateSize();
    }

    // Exposed properties of the textbox
    public override string Text
    {
        get { return textBox.Text; }
        set { textBox.Text = value; }
    }
    // ... Expose other properties you need...

    // The border color property
    public Color BorderColor
    {
        get { return borderColor; }
        set { borderColor = value; Invalidate(); }
    }

    // Expose the Click event for the texbox
    public event EventHandler TextBoxClick
    {
        add { textBox.Click += value; }
        remove { textBox.Click -= value; }
    }
    // ... Expose other events you need...

    private void TextBoxEx_Resize(object sender, EventArgs e)
    {
        InvalidateSize();
    }
    private void TextBoxEx_Paint(object sender, PaintEventArgs e)
    {
        ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, borderColor, ButtonBorderStyle.Solid);
    }
    private void InvalidateSize()
    {
        textBox.Size = new Size(this.Width - 2, this.Height - 2);
        textBox.Location = new Point(1, 1);
    }
}


Now you can use this event even through the VS designer. Here is an example event handler for the exposed event:
C#
private void textBoxEx1_TextBoxClick(object sender, EventArgs e)
{
    // Change the border color on click
    textBoxEx1.BorderColor = Color.Red;
}



An alternative is to expose the entire TextBox as a public property in user control and subscribe to it's events the way you desire.
Example:
C#
public partial class TextBoxEx : UserControl
{
    // The TextBox
    private TextBox textBox = new TextBox();

    // Expose the entire TextBox
    public TextBox InnerTextBox { get {return textBox ; } }
}


After doing this you can subscribe on any of its events like (pseudo-code):
textBoxEx.InnerTextBox.EventOfYourChoice += ...

:)
 
Share this answer
 
v3
Comments
Dalek Dave 22-Jul-10 6:20am    
Good Call
Pawan Kiran 22-Jul-10 7:27am    
Reason for my vote of 5
Great job. Keep it up
Nuri Ismail 22-Jul-10 8:46am    
Thanks! :)
Parazival 3-Jan-15 3:17am    
How to change Textbox BorderColor in C# please tell me the code


thanx
Member 13707618 19-Mar-18 4:10am    
textbox1.BorderColor = Color.Green;
You have some options:

1) A quick and dirty approach: put a Label behind the TextBox and make the label a little bigger than the TextBox. Use the BackColor property of the Label to give the TextBox a different border color.

2) Create a user control that contains a border-less TextBox and draw the border rectangle (with desired color) using ControlPaint.DrawBorder[^]. See this[^] thread for a simple example implementation of this idea.
 
Share this answer
 
v2
Comments
Pawan Kiran 22-Jul-10 4:35am    
I have followed the 2 option. it works fine but events are not working(usercontrol events).
Nuri Ismail 22-Jul-10 6:10am    
Basically you have to write code to expose any property, method, or event that you need. See my answer which contains code example code snippets on how to do this. :)
aazam rafiee zade 13-May-16 13:08pm    
Thank you
You Can Extend the TextBox Class , Add BorderColor proberty and override the WndProc method as it shown in that code,
Note that you must set the BorderStyle to FixedSingle.
public class ExTextBox : System.Windows.Forms.TextBox
   {
       private const int WM_PAINT = 0xF;
       Color borderColor = Color.Blue;
       public Color BorderColor
       {
           get { return borderColor; }
           set { borderColor = value; Invalidate(); }
       }

       protected override void WndProc(ref Message m)
       {
           base.WndProc(ref m);
           if (m.Msg == WM_PAINT)
           {
               using (var g = Graphics.FromHwnd(Handle))
               {
                   using (var p = new Pen(BorderColor, 1))
                   {
                       g.DrawRectangle(p, new Rectangle(0, 0, this.Width - 1, this.Height - 1));
                   }
               }
           }
       }
   }
 
Share this answer
 
v3
you can use this on click event or any other.
Textbox1.Style.Add("Border", "1px Solid");


also you can use css

input[type=text]:focus {
border: 1px Solid
}
 
Share this answer
 
Comments
Nuri Ismail 21-Jul-10 11:06am    
I think that the OP asks about the TextBox control for WinForms application, because he talks about Form and Form_Load. :)
Ozgur salginci 21-Jul-10 11:12am    
:) Yes . Sorry I couldnt realize that .
Toli Cuturicu 21-Jul-10 18:18pm    
Reason for my vote of 1
not windows forms

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900