Geeks With Blogs
Saqib Ullah BootStrapper Know How
Today I had a situation in my project were I need a textbox control with custom border property. Microsoft .Net framework doesn’t allow us to render a textbox with custom border color; you can only change the appearance of the textbox control from Fixed3D to flat and from flat to none. Actually I want a control with flat border style but with custom color not in black. I do the following thing to achieve this control. Create a user control -> Add textbox control on it and move into the code.
My user control Paint Event is transform into the following code
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            ControlPaint.DrawBorder(e.Graphics, base.ClientRectangle, this.BackColor, ButtonBorderStyle.Solid);
        }
My user control size changed event is transform into the following code
        private void UserControl1_SizeChanged(object sender, EventArgs e)
        {
            if (!txtBox.Multiline)
            {
                txtBox.Width = this.Width - 2;
                this.Height = txtBox.Height + 2;
                return;
            }
            else
            {
                txtBox.Width = this.Width - 2;
                txtBox.Height = this.Height - 2;
            }
 
My textbox size changed event is transform into the following code
        private void txtBox_SizeChanged(object sender, EventArgs e)
        {
            this.Height = txtBox.Height + 2;
            this.Width = txtBox.Width + 2;
        }
My user control font changed event is transform into the following code
        private void UserControl1_FontChanged(object sender, EventArgs e)
        {
            txtBox.Font = this.Font;
        }
My textbox font changed event is transform into the following code
                  private void txtBox_FontChanged(object sender, EventArgs e)
        {
            this.Height = txtBox.Height + 2;
            this.Width = txtBox.Width + 2;
        }
Add the following codes in your control class
        private bool multiline = false;
 
        public bool Multiline
        {
            get { return multiline; }
            set
            {
                multiline = value;
                txtBox.Multiline = multiline;
            }
        }
Set the X and Y location of the textbox to 1,1. Build the solution file and use your textbox control with custom color border feature.
Posted on Monday, January 14, 2008 7:47 AM .Net | Back to top


Comments on this post: Custom Border Color Textbox Control

# re: Custom Border Color Textbox Control
Requesting Gravatar...
hallo, this small code helped me a lot, since the is no other way to solve the problem.
regards
raimund
Left by raimund popp on Mar 15, 2010 4:59 AM

Your comment:
 (will show your gravatar)


Copyright © Saqib Ullah | Powered by: GeeksWithBlogs.net