Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,837 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,555 people online right now. Registration is fast and FREE... Join Now!




How to change TextBox border color in C# ?

 
Reply to this topicStart new topic

How to change TextBox border color in C# ?

king of loop
30 Sep, 2008 - 01:41 AM
Post #1

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 16



Thanked: 1 times
My Contributions
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

User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: How To Change TextBox Border Color In C# ?
30 Sep, 2008 - 05:00 AM
Post #2

D.I.C Regular
Group Icon

Joined: 21 Mar, 2008
Posts: 375



Thanked: 19 times
Dream Kudos: 25
My Contributions
technically, there is no "border color" for a textbox. what you can do is this.....create a label that is just a few pixels bigger than the textbox. put it behind the textbox and just change it's background color.
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: How To Change TextBox Border Color In C# ?
30 Sep, 2008 - 07:10 AM
Post #3

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,551



Thanked: 98 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
Couldn't you override the Paint method of the control, and simply draw a rectangle around it, in a specific colour?

I haven't tried it, but I think it would work~
User is offlineProfile CardPM
+Quote Post

king of loop
RE: How To Change TextBox Border Color In C# ?
1 Oct, 2008 - 03:39 AM
Post #4

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 16



Thanked: 1 times
My Contributions
CODE

private static int WM_NCPAINT = 0x0085;
private static int WM_ERASEBKGND = 0x0014;
private static int WM_PAINT = 0x000F;

[DllImport("user32.dll")]
static extern IntPtr GetDCEx(IntPtr hwnd, IntPtr hrgnclip, uint fdwOptions);

[DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hwnd, IntPtr hDC);

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);
    if (m.Msg == WM_NCPAINT || m.Msg == WM_ERASEBKGND || m.Msg == WM_PAINT)
    {
        IntPtr hdc = GetDCEx(m.HWnd, (IntPtr)1, 1 | 0x0020);

        if (hdc != IntPtr.Zero)
        {
            Graphics graphics = Graphics.FromHdc(hdc);
            Color borderColor = Color.Blue;
            Rectangle rectangle = new Rectangle(0, 0, this.Width, this.Height);
            ControlPaint.DrawBorder(graphics, rectangle, borderColor, ButtonBorderStyle.Solid);
            m.Result = (IntPtr)1;
            ReleaseDC(m.HWnd, hdc);
        }
    }
}

User is offlineProfile CardPM
+Quote Post

king of loop
RE: How To Change TextBox Border Color In C# ?
1 Oct, 2008 - 03:47 AM
Post #5

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 16



Thanked: 1 times
My Contributions
,.......

This post has been edited by king of loop: 1 Oct, 2008 - 03:48 AM
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: How To Change TextBox Border Color In C# ?
1 Oct, 2008 - 07:20 AM
Post #6

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,551



Thanked: 98 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
Please don't double post~

What problems are you having with that code? State the errors, and you're more likely to get a response. smile.gif
User is offlineProfile CardPM
+Quote Post

king of loop
RE: How To Change TextBox Border Color In C# ?
1 Oct, 2008 - 07:49 AM
Post #7

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 16



Thanked: 1 times
My Contributions
QUOTE(gabehabe @ 1 Oct, 2008 - 08:20 AM) *

Please don't double post~

What problems are you having with that code? State the errors, and you're more likely to get a response. smile.gif



Hi, gabehabe

Firt of all very sorry for the double post I was editing and pressed publish by mistake.

This code had no problem and is exactly the way I want it to work I only posted it so that others may have use of it as well.
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: How To Change TextBox Border Color In C# ?
1 Oct, 2008 - 08:50 AM
Post #8

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,551



Thanked: 98 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
OK, thanks for the resource. smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 04:43PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month