Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
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 21-Jul-10 4:15am
Edited 22-Jul-10 0:19am
Dalek Dave431.6K
v2
Comments
Dalek Dave at 22-Jul-10 6:20am
   
Edited for readability.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
    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:
    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:
    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 += ...
 
Smile | :)
  Permalink  
v3
Comments
Dalek Dave at 22-Jul-10 6:20am
   
Good Call
Pawan Kiran at 22-Jul-10 7:27am
   
Reason for my vote of 5
Great job. Keep it up
Nuri Ismail at 22-Jul-10 8:46am
   
Thanks! :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2
Comments
Pawan Kiran at 22-Jul-10 4:35am
   
I have followed the 2 option. it works fine but events are not working(usercontrol events).
Nuri Ismail at 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. :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
}
  Permalink  
Comments
Nuri Ismail at 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 at 21-Jul-10 11:12am
   
:) Yes . Sorry I couldnt realize that .
Toli Cuturicu at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 399
1 Nirav Prabtani 338
2 Abhinav S 170
3 PIEBALDconsult 160
4 Dave Kreskowiak 155
0 OriginalGriff 7,580
1 Sergey Alexandrovich Kryukov 6,806
2 Maciej Los 3,919
3 Peter Leow 3,693
4 CHill60 2,742


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 29 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100