none
How to change TextBox border color in C# ?

    Question

  •  Hi,

    In C#

    TextBox tb;
    tb.BackColor = Color.Black
    tb.ForeColor = Color.White.

    how to change border color of text box ? I tried Using BackColor and ForeColor but I could not change border color.

    Thank
    Tuesday, September 30, 2008 9:37 AM

Answers

  • This will get you started:

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Drawing;

    using System.Data;

    using System.Text;

    using System.Windows.Forms;

     

    namespace BorderColorTextBox

    {

        public partial class UserControl1 : UserControl

        {

            private string text;

            public string Text

            {

                get { return textBox.Text; }

                set { textBox.Text = value; }

            }

     

            TextBox textBox = new TextBox();

            public UserControl1()

            {

                InitializeComponent();

                this.Paint += new PaintEventHandler(UserControl1_Paint);

                this.Resize += new EventHandler(UserControl1_Resize);

                textBox.Multiline = true;

                textBox.BorderStyle = BorderStyle.None;

                this.Controls.Add(textBox);

            }

     

            private void UserControl1_Resize(object sender, EventArgs e)

            {

                textBox.Size = new Size(this.Width - 3, this.Height - 2);

                textBox.Location = new Point(2, 1);

     

            }

     

            private void UserControl1_Paint(object sender, PaintEventArgs e)

            {

                ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Red, ButtonBorderStyle.Solid);

     

            }

        }

    }


    I need a job!
    • Marked as answer by Ghasem Karimi Tuesday, September 30, 2008 6:07 PM
    • Unmarked as answer by Ghasem Karimi Tuesday, September 30, 2008 6:35 PM
    • Marked as answer by jack 321 Thursday, October 2, 2008 8:40 AM
    Tuesday, September 30, 2008 5:48 PM
  • Directly from MSDN:

    Some Windows Forms controls, such as TextBox, are painted directly by Windows. In these instances, the OnPaint method is never called.

    So as the other answerers told you, you have to tweak around the border colored text box by creating your own custom control, and not deriving from TextBox class.


    Best regards,
    Timo Salomäki


    Clouddev.blogspot.com
    • Marked as answer by jack 321 Thursday, October 2, 2008 8:40 AM
    Tuesday, September 30, 2008 6:30 PM
  • Just place it on a panel that's slightly bigger than the textbox, and set the background color of the textbox.
    David Morton - http://blog.davemorton.net/
    • Marked as answer by jack 321 Thursday, October 2, 2008 8:41 AM
    Tuesday, September 30, 2008 7:25 PM
    Moderator

All replies

  • This is simply not possible. You will have to wrap a borderless textbox in your own control and draw a border yourself.
    Ole - Andre Johansen | Hire Me !
    Tuesday, September 30, 2008 11:02 AM
  • Hi, tuxbear 

    thx for reply

    I would really appreciate it if  you could show me an example of such code,could  you do that?
    Tuesday, September 30, 2008 12:24 PM
  • Hello,

    I seems that the textbox does not fire any paint events. I guess this is a result of the control not being 100% managed, and that the native painting is used. Someone please correct or confirm :)

    What you then must do is to make your own usercontrol that you stick a borderless textbox inside, and then paint the border on the panel. I'm sorry I don't have the time to show you right now. Google .net usercontrol and handling the Paint event.

    Ole - Andre Johansen | Hire Me !
    Tuesday, September 30, 2008 5:15 PM
  • This will get you started:

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Drawing;

    using System.Data;

    using System.Text;

    using System.Windows.Forms;

     

    namespace BorderColorTextBox

    {

        public partial class UserControl1 : UserControl

        {

            private string text;

            public string Text

            {

                get { return textBox.Text; }

                set { textBox.Text = value; }

            }

     

            TextBox textBox = new TextBox();

            public UserControl1()

            {

                InitializeComponent();

                this.Paint += new PaintEventHandler(UserControl1_Paint);

                this.Resize += new EventHandler(UserControl1_Resize);

                textBox.Multiline = true;

                textBox.BorderStyle = BorderStyle.None;

                this.Controls.Add(textBox);

            }

     

            private void UserControl1_Resize(object sender, EventArgs e)

            {

                textBox.Size = new Size(this.Width - 3, this.Height - 2);

                textBox.Location = new Point(2, 1);

     

            }

     

            private void UserControl1_Paint(object sender, PaintEventArgs e)

            {

                ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Red, ButtonBorderStyle.Solid);

     

            }

        }

    }


    I need a job!
    • Marked as answer by Ghasem Karimi Tuesday, September 30, 2008 6:07 PM
    • Unmarked as answer by Ghasem Karimi Tuesday, September 30, 2008 6:35 PM
    • Marked as answer by jack 321 Thursday, October 2, 2008 8:40 AM
    Tuesday, September 30, 2008 5:48 PM
  • thx cablehead :)
    Tuesday, September 30, 2008 6:08 PM
  • Directly from MSDN:

    Some Windows Forms controls, such as TextBox, are painted directly by Windows. In these instances, the OnPaint method is never called.

    So as the other answerers told you, you have to tweak around the border colored text box by creating your own custom control, and not deriving from TextBox class.


    Best regards,
    Timo Salomäki


    Clouddev.blogspot.com
    • Marked as answer by jack 321 Thursday, October 2, 2008 8:40 AM
    Tuesday, September 30, 2008 6:30 PM
  • Just place it on a panel that's slightly bigger than the textbox, and set the background color of the textbox.
    David Morton - http://blog.davemorton.net/
    • Marked as answer by jack 321 Thursday, October 2, 2008 8:41 AM
    Tuesday, September 30, 2008 7:25 PM
    Moderator
  • Just place it on a panel that's slightly bigger than the textbox, and set the background color of the textbox.
    David Morton - http://blog.davemorton.net/

    I know this is old, but just helped me solve this very problem...Thanks David.  Elegant, simple solution.
    Wednesday, February 23, 2011 4:51 PM
  • One more option (for those who don't want the bloat of an extra wrapper control for a single textbox):

    • Handle the Paint event of the form (or the panel/control that the textbox resides on) 
    • Make a rectangle that is inflated around the clientRectangle of the textbox
    • ControlPaint.DrawBorder on that rectangle using the graphics from the PaintEventArgs

     

    private void form1_Paint(object sender, System.Windows.Forms.PaintEventArgs)
    {
    	System.Drawing.Rectangle rect = new Rectangle(textBox1.Location.X, textBox1.Location.Y, textBox.ClientSize.Width, textBox1.ClientSize.Height);
    	//adding 1 pixel to either side, then adding 3 pixels up top, plus 3 down below. This will add the necessary padding to mimic the border of a standard textbox
    rect.Inflate(1, 3); System.Windows.Forms.ControlPaint.DrawBorder(e.Graphics, rect, Color.FromArgb(180, 180, 180), ButtonBorderStyle.Solid); }


    Hope that helps anyone else who comes across this thread with a similar question :)

    Happy Coding,
    Nate

    -------

    http://SeriusSoft.com 

     


    • Edited by dudeserius Friday, October 7, 2011 11:08 PM formatting
    Friday, October 7, 2011 11:06 PM
  • A very useful easy to understand step by step guide to create custom textbox in visual studio having border color feature, with snaps 
    http://csharpdemos.blogspot.in/2012/09/creating-custom-control-textbox-in.html
    Saturday, September 29, 2012 12:29 PM