none
Rounded corner Textbox

    Question

  • Hi

     

    I want to make a textbox of my own by inheriting textbox. I want 3 properties in it

    one is - rounded corner

    and the second is border width 

    and the last is border color

    any help?

    Note : Im using vb.net. So vb code pls.

     

    Regards

     

    Wednesday, February 01, 2012 6:03 AM

Answers

  • Hi ackid32,

    This is a forum for people asking for help coding in .NET, not by asking someone to do the complete job for you, you need to try to do it yourself and then post specific questions.

    If you're looking for an example, check out codeproject.com or codeplex.com. Those both provide open-source solutions. If this is homework, do it yourself, you'll learn more that way.

     

    • Back to your question, here is an article with complete code that create a TextBox control with rounded corners http://www.codeproject.com/Articles/17453/Textbox-with-rounded-corners,
    • To change the border color of the TextBox, use the following code:
               private void Form1_Paint(object sender, PaintEventArgs e)
               {           
                  Pen p = new Pen(Color.Red);
                   g.DrawRectangle(p, new Rectangle(UC_Masked1.Location,textBox1.Size));   //UC_Masked1 is an instance of the UC_MaskedTextBox class 
              }

      Or override the onPaint for the custom textBox (because normally textbox doesn't react onPaint, you should do it like this)

      public   class   MyTextBox   :   System.Windows.Forms.TextBox   
      { 
           public   MyTextBox() 
           { 
                SetStyle(ControlStyles.UserPaint,   true); 
           } 
      
           protected   override   void   OnPaint(PaintEventArgs   e) 
           { 
                base.OnPaint(e); 
                e.Graphics.Clear(System.Drawing.Color.Red); 
           } 
      }
      • To change the border width, check the following demo
          class TextB : TextBox
           {
               public TextB()
               {
                   this.BorderStyle = BorderStyle.None;
                   // you can se BackColor to your faviored here.
                   this.BackColor = SystemColors.Control;
               }
               private int WM_PAINT = 0x000F;
               protected override void WndProc(ref Message m)
               {
                   base.WndProc(ref m);
      
                   if (m.Msg == WM_PAINT)
                   {
                       Pen pen = new Pen(Brushes.Black, 1.5f);
                       using (Graphics g = this.CreateGraphics())
                       {
                           g.DrawLine(pen, new Point(0, this.Size.Height - 1), new Point(this.Size.Width, this.Size.Height - 1));
                       }
                   }
               }
      

      this demo makes the customed textbox only has bottom border, you may modify that to change the border with, the logic is the same. Good luck.]

      Regards,


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us

    • Edited by Helen Zhou Thursday, February 02, 2012 3:43 AM
    • Marked as answer by ackid32 Friday, February 03, 2012 8:15 AM
    Thursday, February 02, 2012 3:42 AM

All replies

  • Hi ackid32,

    This is a forum for people asking for help coding in .NET, not by asking someone to do the complete job for you, you need to try to do it yourself and then post specific questions.

    If you're looking for an example, check out codeproject.com or codeplex.com. Those both provide open-source solutions. If this is homework, do it yourself, you'll learn more that way.

     

    • Back to your question, here is an article with complete code that create a TextBox control with rounded corners http://www.codeproject.com/Articles/17453/Textbox-with-rounded-corners,
    • To change the border color of the TextBox, use the following code:
               private void Form1_Paint(object sender, PaintEventArgs e)
               {           
                  Pen p = new Pen(Color.Red);
                   g.DrawRectangle(p, new Rectangle(UC_Masked1.Location,textBox1.Size));   //UC_Masked1 is an instance of the UC_MaskedTextBox class 
              }

      Or override the onPaint for the custom textBox (because normally textbox doesn't react onPaint, you should do it like this)

      public   class   MyTextBox   :   System.Windows.Forms.TextBox   
      { 
           public   MyTextBox() 
           { 
                SetStyle(ControlStyles.UserPaint,   true); 
           } 
      
           protected   override   void   OnPaint(PaintEventArgs   e) 
           { 
                base.OnPaint(e); 
                e.Graphics.Clear(System.Drawing.Color.Red); 
           } 
      }
      • To change the border width, check the following demo
          class TextB : TextBox
           {
               public TextB()
               {
                   this.BorderStyle = BorderStyle.None;
                   // you can se BackColor to your faviored here.
                   this.BackColor = SystemColors.Control;
               }
               private int WM_PAINT = 0x000F;
               protected override void WndProc(ref Message m)
               {
                   base.WndProc(ref m);
      
                   if (m.Msg == WM_PAINT)
                   {
                       Pen pen = new Pen(Brushes.Black, 1.5f);
                       using (Graphics g = this.CreateGraphics())
                       {
                           g.DrawLine(pen, new Point(0, this.Size.Height - 1), new Point(this.Size.Width, this.Size.Height - 1));
                       }
                   }
               }
      

      this demo makes the customed textbox only has bottom border, you may modify that to change the border with, the logic is the same. Good luck.]

      Regards,


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us

    • Edited by Helen Zhou Thursday, February 02, 2012 3:43 AM
    • Marked as answer by ackid32 Friday, February 03, 2012 8:15 AM
    Thursday, February 02, 2012 3:42 AM
  • In addition to the proposed Helen, you can read about "CreateRoundRectRgn", "WM_NCCALCSIZE" and "WM_NCPAINT" to make a control with corners rounded.

    Rounded corners in GroupBox Control

    Best regards.


    Marvin E. Pineda

      ComboBoxMultiColumns

     NetBarControl

      TextEditor

    Thursday, February 02, 2012 2:53 PM