1

I've created Windows forms and I'm using the textbox control for input, but I like to use it without border and other layout for textbox etc. I just want to use a underscore line and blinking cursor.

I played with the borderStyle (Fixed3D, None), backcolor=InactiveBorder etc. But I still do net get the underline... like this-> _____________ result like this: This is underline______________

I think Backcolor=InactiveBorder and BorderStyle=None is ok to use, but how to get the underline and blinking cursor?

Requirement:

  • blinking cursor and underline. (The doesn't blink by default, I just see a vertical line))
ethem
  • 2,888
  • 9
  • 42
  • 57

5 Answers5

1

To fake this, you could add a label below the text box with the content being _____________________. My preferred solution would be to create a simple custom control that just draws a line.

Doesn't the caret on your system blink by default? It does on my system if the focus is on the text box.

If the caret doesn't blink by default, go to the Windows Control Panel and check your Keyboard Settings there - this is the place where you can adjust the caret blink rate.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • no it's not blinking by default, I updated my question regarding this too. btw: I'm newbie with winforms, so I could be struggling with the code with drawing a line. – ethem Apr 04 '13 at 09:43
  • Well, then use a `Label` as I suggested - even though this is really bad fake :-) About the blinking cursor: Windows itself is responsible for the caret to blink. This is not something you need to "switch on" in your code. You can, however, change the shape of the caret as described in the answers to this question: http://stackoverflow.com/questions/13439717/how-to-change-windows-blink-cursor-shape-from-c. However, by default in Windows the caret is a vertical line that blinks. If it doesn't, check the keyboard settings in the Windows Control Panel. There you can adjust the cursor blink rate. – Thorsten Dittmar Apr 04 '13 at 10:54
  • The problem with using a Label control instead of a TextBox is that you have to write all of the text input code yourself, which is not going to be simple. And even once you get something written that appears to work, it'll be rather fragile and lacking a number of features (e.g., IME support). It's much simpler to use a TextBox with all of the complex logic built in and just skip drawing the borders, etc. that you don't want. – Cody Gray - on strike Apr 05 '13 at 06:08
  • No, that's not what I meant! I meant to put a borderless textbox on the form, and align a label with it below. The text is entered in the text box as usual, the label contains many `_` to simulate the underline. – Thorsten Dittmar Apr 05 '13 at 06:59
1

For creating a underline for your textbox you can do like this,

  • First add a panel which is in the height of text box's height + underline's height.
  • Now add your textbox inside of that panel and set its dock to TOP.
  • Then set the textbox's border to none.
  • Now set the backcolor of the panel, according to the color need of underline.

Update:

This is VB code, i hope that you can easily convert it into c#

[ Concept: You just need to set the border for all of your textboxes as none.then In forms paint event track those text boxes and draw a line under it. ]

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Me.Paint

    Using xPen As Pen = New Pen(Color.Blue)
        ' Here we are using LINQ to filter the controls.
        ' If you don't want it, you just check all the controls by using typeof
        ' inside the For Each loop.
        For Each xTxtBox In Me.Controls.OfType(Of TextBox)()
            e.Graphics.DrawLine(xPen,
                                xTxtBox.Location.X,
                                xTxtBox.Location.Y + xTxtBox.Height,
                                xTxtBox.Location.X + xTxtBox.Width,
                                xTxtBox.Location.Y + xTxtBox.Height)
        Next
    End Using

End Sub
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
  • sounds good, looks like easy to, but I have 3 forms and approx. 20 textboxes per form. so I need to create then per textbox a panel... that's a lot of maintenance for me... but it's simple and creative solution and much work :(... thanks for the answer – ethem Apr 04 '13 at 17:50
  • Oh.. its ok, cool. I just added an update, check that out, it'll definitely suits your requirement. – Rajaprabhu Aravindasamy Apr 05 '13 at 05:17
  • I fixed your code. You should *never* dispose of the automatically-provided `Graphics` object inside of a `Paint` event handler. And there's no reason in having an empty try/catch block just to use the finally block. It's much cleaner to use a `Using` statement to manage disposal of the `Pen` object and anything else you create. – Cody Gray - on strike Apr 05 '13 at 06:12
  • @CodyGray Thanks, i just learned a new concept `USING` Because of you. thanks again. – Rajaprabhu Aravindasamy Apr 05 '13 at 06:40
0

Use Masked TextBox and set Focus , e.g. maskedtextbox1.Focus(); <== this is for the blinking cursor and the masked textbox to the underline !

try :

To set logical focus to an input control

FocusManager.SetFocusedElement(this, textboxJack); 

To set keyboard focus to an input control

Keyboard.Focus(textboxJill);

and for the masked textbox you can set a mask that will not be changed when you delete the text from it not like the simple textbox :) Good luck

Obama
  • 2,586
  • 2
  • 30
  • 49
  • I've used maskedTextbox control and set the focus() on OnLoad, but it's still not blinking... and do not see the difference with Textbox. how to do the underline in Maskedtextbox? – ethem Apr 04 '13 at 09:52
  • @mesut see answer changes :) – Obama Apr 04 '13 at 10:05
  • I don't have problem with focus, it's the underline. so there is no underline. – ethem Apr 04 '13 at 10:49
  • thanks Obama. this is above my knowledge, I thought something like simple 2-3 lines of code or a property. – ethem Apr 04 '13 at 17:48
0

To do this, I would recommend creating a custom control (which is accomplished in the WinForms world by inheriting from one of the provided control classes). That custom control would then:

  1. Provide its own drawing logic (by overriding OnPaint) in order to draw the underline and skip drawing anything else you don't want to see (e.g., the borders of the control).

  2. Create its own caret when it receives the focus, and destroy that caret when it loses the focus. You'll find all the details on how to do this in my answer here.

You can also configure the blink rate of the caret by calling the SetCaretBlinkTime function. But note that this is not recommended, as it changes the global system setting and therefore affects other applications. It is best to do as Thorsten suggests and modify the setting on your machine if you wish to see something different. You should always respect a user's settings. There's a reason that they (or someone else) set up their system to not blink the caret.

Naturally, you will need to use P/Invoke to call these Win32 API functions related to caret management from a C# application. That shouldn't be too difficult if you know what you're doing. If you need a complete solution, consider setting a bounty on this question to persuade me to write one up for you.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
0

I faced the same issue and built something that works fine:

public class TextBox : System.Windows.Forms.TextBox
{
    public TextBox()
    {
        BorderStyle = BorderStyle.None;
        Text = "__________"; //Sometime this doesn't work while creating the control in design mode ; don't know why
    }

    //protected override void OnFontChanged(EventArgs e)
    //{
    //    base.OnFontChanged(e);
    //    RefreshHeight();
    //}

    bool loaded = false;
    protected override void OnCreateControl()
    {
        if(!loaded)
            RefreshHeight();
        base.OnCreateControl();
    }

    private void RefreshHeight()
    {
        loaded = true;
        Multiline = true;
        Size s = TextRenderer.MeasureText(Text, Font, Size.Empty, TextFormatFlags.TextBoxControl);
        MinimumSize = new Size(0, s.Height + 1);
        Multiline = false;
    }
}

I used bool loaded = false to avoid the app to crash in a loop because of OnCreateControl. TextBox control doesn't have OnLoad event (I'm open to another approach).

OnFontChanged can be uncommented if your app change the font size in run time

MinimumSize = new Size(0, s.Height + 1); I added 1 to avoid any error of MeasureText

Fabrice T
  • 648
  • 9
  • 23