简体   繁体   中英

How to Disable Flashing Text Line?

I have a textbox control and I've simulated it to be like a label by turning the readonly and border property to true and none respectively so it can highlighted. Everything works fine but when I click on the textbox, it shows the flashing text symbol (it's a flashing l that indicating that you can type here, don't know what's it called).

If I turn enabled property to false, it disappears but it can't be highlighted and the text is grayed out.

Is there a way to fix this? Like another type of control?

Text symbol will flash i don't know weather you can disable flashing better you can use label i think that is good check the visibility property of text box may be it will be false that's why you cant able to see.

through code also you can enable visibility

       textboxname.visible= true;

tweak this css properity for your project.

input[disabled] {
        background-color: #b5f0fa;
        border: 1px solid;
        color: Black;
        font-weight: bold;
    }

The little "flashing text line" is called a caret . It is like a cursor (which is the thing you move with the mouse), except that it appears in text.

And what you describe in the question is normal behavior. You see the exact same thing in the Windows shell. Right-click on any icon in any location (eg, the desktop), and open its "Properties" window. Then click on any one of the property labels (eg, the path under "Location"). You'll see the same thing.

There's a caret there, flashing just beyond the letter "i", and you can see even see that the selection is highlighted. This is by design, and the selection highlighting is an important clue.

The reason why you would use a read-only textbox instead of a label is to allow the user to select text out of the control. If you don't want to allow this, then just use a label.


If you absolutely must use a read-only textbox and still want to hide the caret, then you can p/invoke the HideCaret function . By calling this function as soon as your textbox control gets the focus, it won't ever show the caret. However, every call to HideCaret must be paired with a call to ShowCaret , so you'll need to call ShowCaret when your textbox control loses the focus.

You could wire up handlers to the GotFocus and LostFocus events for your particular textbox control and place the code in there. But it is cleaner to do this in a derived control class, especially if you want to have multiple textboxes that behave the same way. Something like this (code untested):

internal class NativeMethods
{
    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool ShowCaret(IntPtr hwnd);

    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool HideCaret(IntPtr hwnd);
}

public class ReadOnlyTextBox : System.Windows.Forms.TextBox
{
    public ReadOnlyTextBox()
    {
        this.ReadOnly = true;
    }

    protected override void OnGotFocus(EventArgs e)
    {
        NativeMethods.HideCaret(this.Handle);
    }

    protected override void OnLostFocus(EventArgs e)
    {
        NativeMethods.ShowCaret(this.Handle);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM