locked
Input field with underline in WinForm RRS feed

  • Question

  • Mostly, the input field is made by TextBox which has rectangle frame but I want to make input field which has only underline.

    How could I make underline input field?


    • Edited by Jeff0803 Monday, April 4, 2016 10:42 PM
    • Moved by CoolDadTx Tuesday, April 5, 2016 2:49 PM Winforms related
    Monday, April 4, 2016 10:40 PM

Answers

  • Use a textbox having BorderStyle=None. Place a picture box bellow the textbox, set Border=FixedSingle, then set its Size Height = 1 (manually in Properties window). This should look like a horizontal line. Use keyboard to place it exactly. If you need a larger white area, then use a panel control with BackColor=Window, and place the textbox and picture inside the panel.

    Tuesday, April 5, 2016 5:35 AM

All replies

  • Hello,

     All depends on the source code your are using.

    Here is a WPF link;

    http://www.codeproject.com/Articles/633935/Customizing-the-Caret-of-a-WPF-TextBox

    And a WinForm link;

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/ca62549a-4c35-4156-8d2f-5a37268f8c4d/changing-the-blinking-cursor-in-a-textbox?forum=csharpgeneral

     Please read the articles that are provided. Thanks :)

    Tuesday, April 5, 2016 12:00 AM
  • Use a textbox having BorderStyle=None. Place a picture box bellow the textbox, set Border=FixedSingle, then set its Size Height = 1 (manually in Properties window). This should look like a horizontal line. Use keyboard to place it exactly. If you need a larger white area, then use a panel control with BackColor=Window, and place the textbox and picture inside the panel.

    Tuesday, April 5, 2016 5:35 AM
  • Hi Jeff,

    I would suggest you to use WPF for development you can do this easily there. Hope this helps you.

    Thanks,

    Sabah Shariq

    • Marked as answer by Jeff0803 Saturday, April 9, 2016 12:40 AM
    • Unmarked as answer by Jeff0803 Saturday, April 9, 2016 12:40 AM
    Tuesday, April 5, 2016 9:38 AM
  • Hi Jeff,

    You could try to refer to the following code(WndProc + GDI):

     public class CustomTextBox : TextBox
        {
            [System.Runtime.InteropServices.DllImport("user32.dll")]
            static extern IntPtr GetWindowDC(IntPtr hWnd);
            [System.Runtime.InteropServices.DllImport("user32.dll")]
            static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
    
            private Color _BorderColor = Color.Black;
            /// <summary>
            /// system message
            /// </summary>
            /// <param name="m"></param>
            protected override void WndProc(ref Message m)
            {
    
                base.WndProc(ref m);
                if (m.Msg == 0xf || m.Msg == 0x133)
                {
                    IntPtr hDC = GetWindowDC(m.HWnd);
                    if (hDC.ToInt32() == 0)
                    {
                        return;
                    }
    
                    Pen pen = new Pen(Color.Black, 2.0f);
                    //draw border
                    System.Drawing.Graphics g = Graphics.FromHdc(hDC);
                    g.DrawLine(pen, 0, this.Height, this.Width, this.Height);
    
                    pen.Dispose();
    
    
                    //result
                    m.Result = IntPtr.Zero;
                    //release
                    ReleaseDC(m.HWnd, hDC);
    
                    this.BackColor = SystemColors.Control;
                    this.BorderStyle = BorderStyle.None;
    
                }
            }
    
        }

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Friday, April 8, 2016 2:53 AM
  • I made input field with textbox as the way Viorel suggested because it's straight forward.

    Next I'm trying with MaskedTextBox for date input field of which format is "  /  /    ".

    However I can't remove underbar in the mm, dd, yyyy like following screen capture.

    Can I remove this under bar?


    • Edited by Jeff0803 Tuesday, April 12, 2016 5:32 PM
    Tuesday, April 12, 2016 5:30 PM