How to use a text box but with an underline and a blinking cursor?

I created windows forms and I use the textbox control for input, but I like to use it without a frame and another layout for a text box, etc. I just want to use the underline and the blinking cursor.

I played with borderStyle (Fixed3D, None), backcolor = InactiveBorder, etc. But I'm still doing net, getting the underscore ... like this-> _____________ , like this: This is underline_ _____________

I think Backcolor = InactiveBorder and BorderStyle = No, it's ok to use, but how to get underline and blinking cursor?

Demand:

  • flickering cursor and underline. (Does not blink by default, I just see a vertical line))
+1
source share
5 answers

To fake this, you can add a shortcut under the text box with the contents of _____________________ . My preferred solution would be to create a simple user control that simply draws a string.

Does carriage affect your system by default? This is done on my system if the focus is in the text box.

If the carriage does not blink by default, go to the Windows control panel and check the keyboard settings - this is the place where you can configure the carriage blink speed.

+1
source

To create an underline for your textbox you can do this,

  • First add a panel that is at the height of the height of the text box + underline height .
  • Now add a textbox inside this panel and set its dock to TOP .
  • Then set the border text box to none .
  • Now set the backcolor in the panel according to the need for color in underlining.

Update:

This is VB code, I hope you can easily convert it to C #

[ Concept: You just need to set the border for all your text fields as none.then. In the process of drawing forms, track these text fields 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 
+1
source

Use Masked TextBox and set Focus, for example. maskedtextbox1.Focus(); <== this is for a blinking cursor and a masked text field for underlining!

try:

To set the logical focus to the input control

 FocusManager.SetFocusedElement(this, textboxJack); 

To adjust keyboard focus to input control

 Keyboard.Focus(textboxJill); 

and for a hidden text field, you can set a mask that will not be changed when deleting text from it, not like a simple text field :) Good luck.

0
source

To do this, I would recommend creating a custom control (which runs in the WinForms world by inheriting from one of the provided control classes). Then this user control:

  • Provide your own drawing logic (overriding OnPaint ) to draw the underline and skip the picture you don't want to see (for example, the borders of the control).

  • Create your own carriages when he gets focus, and destroy him when he loses focus. You will find all the details on how to do this in my answer here .

You can also adjust the carriage flashing speed by calling the SetCaretBlinkTime function. But note that this is not recommended, as it changes the global settings of the system and, therefore, affects other applications. It is best to make Thorsten and change the settings on your computer if you want to see something else. You should always respect user preferences. There is a reason that they (or someone else) are setting up their system so as not to blink the carriage.

Naturally, you will need to use P / Invoke to call these Win32 carriage control API functions from a C # application. It should not be too difficult if you know what you are doing. If you need a complete solution, consider setting up generosity on this subject to convince me to write it for you.

0
source

I ran into the same problem and created something that works great:

 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 application crashing in the loop due to OnCreateControl . The TextBox control does not have an OnLoad (I'm open to a different approach).

OnFontChanged may be uncommented if your application changes font size at runtime

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

0
source

Source: https://habr.com/ru/post/1385983/


All Articles