简体   繁体   English

如何在文本框中禁用光标?

[英]How to disable cursor in textbox?

Is there any way to disable cursor in textbox without setting property Enable to false?有没有办法在不将属性 Enable 设置为 false 的情况下禁用文本框中的光标? I was trying to use ReadOnly property but despite the fact that I can't write in textbox, the cursor appears if I click the textbox.我试图使用 ReadOnly 属性,但尽管我无法在文本框中写入,但如果我单击文本框,则会出现光标。 So is there any way to get rid of this cursor permamently?那么有没有办法永久摆脱这个光标?

In C#, you can use the following read-only textbox:在 C# 中,您可以使用以下只读文本框:

public class ReadOnlyTextBox : TextBox
{
    [DllImport("user32.dll")]
    static extern bool HideCaret(IntPtr hWnd);

    public ReadOnlyTextBox()
    {
        this.ReadOnly = true;
        this.BackColor = Color.White;
        this.GotFocus += TextBoxGotFocus;
        this.Cursor = Cursors.Arrow; // mouse cursor like in other controls
    }

    private void TextBoxGotFocus(object sender, EventArgs args)
    {
        HideCaret(this.Handle);
    }
}

In C# you can disable the cursor in a textbox by temporarily disabling and then re-enabling the text box whenever it receives the focus.在 C# 中,您可以通过暂时禁用文本框中的光标,然后在收到焦点时重新启用文本框。 Note there is no need to make the textbox read only if using this method.请注意,如果使用此方法,则无需将文本框设为只读。 For example:例如:

private void TextBox_Enter(object sender, EventArgs e)
{
  TextBox.Enabled = false;
  TextBox.Enabled = true;
}

You could use a Label instead.您可以改用Label When in the designer, you set BorderStyle = Fixed3D , BackColor = Window and AutoSize = False , it looks a lot like a TextBox.当在设计器中设置BorderStyle = Fixed3DBackColor = WindowAutoSize = False ,它看起来很像一个 TextBox。

However, the cursor in a TextBox is provided so that the user can scroll through the text when it is longer than the box.但是,提供了 TextBox 中的光标,以便用户可以在文本比文本框长时滚动浏览。 You'll lose that functionality with a Label, unless you are sure that it will always fit.使用 Label 将失去该功能,除非您确定它始终适合。 Other than that, it is not possible to remove the cursor from a TextBox.除此之外,无法从 TextBox 中删除光标。

Putting the hideCaret function inside the TextChanged event will solve the problem:将 hideCaret 函数放在 TextChanged 事件中将解决问题:

[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);

private void textBox1_TextChanged(object sender, EventArgs e)
{
    HideCaret(textBox1.Handle);
}

Easiest solution for me was to just override the on focus event and focus back to the parent.对我来说最简单的解决方案是覆盖 on focus 事件并将焦点返回给父级。 This prevents the cursor and any editing of the textbox by the user and basically disables the text box with out having to set the Enabled = false property.这可以防止光标和用户对文本框的任何编辑,并且基本上禁用文本框而不必设置 Enabled = false 属性。

private void Form1_load(object sender, EventArgs e) {
    textBox1.ReadOnly = true;
    textBox1.Cursor = Cursors.Arrow;
    textBox1.GotFocus += textBox1_GotFocus;
}


private void textBox1_GotFocus(object sender, EventArgs e) {
    ((TextBox)sender).Parent.Focus();
}

Like @Mikhail Semenov 's solution, you can also use lambda express to quickly disable the cursor if you do not have many textboxes should do that:像@Mikhail Semenov 的解决方案一样,如果您没有很多文本框应该这样做,您也可以使用 lambda express 来快速禁用光标:

[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);

textBox1.ReadOnly = true;
textBox1.BackColor = Color.White;
textBox1.GotFocus += (s1, e1) => { HideCaret(textBox1.Handle); };
textBox1.Cursor = Cursors.Arrow;

您可以以编程方式设置它。

textBox1.Cursor = Cursors.Arrow;

This is not strictly an answer to the question, but perhaps it can solve some similar problem(s).这不是严格意义上的问题的答案,但也许它可以解决一些类似的问题。 I use a textbox control which can look like a label for a control which displays a scale, but can be edited, when clicked.我使用了一个文本框控件,它看起来像一个显示比例的控件的标签,但可以在单击时进行编辑。 Start enabled = false and make an activation (enabled = true) in a mousehandler of the parent of the textbox control (which, when disabled, border None and backcolor = parent backcolor, looks like a label).启动 enabled = false 并在文本框控件的父级的鼠标处理程序中激活(启用 = true)(禁用时,边框无和背景色 = 父背景色,看起来像一个标签)。 Eg when enter hit or other event, disable again in KeyDown handler.例如,当输入命中或其他事件时,在 KeyDown 处理程序中再次禁用。 (Of course the parent mouse click routine can check whether the mouseclick really occured in the label/textbox control). (当然,父鼠标点击例程可以检查鼠标点击是否真的发生在标签/文本框控件中)。 If you need the textbox control to activate by tabbing, some more work is required (than I have done).如果您需要通过 Tab 键激活文本框控件,则需要做更多的工作(比我所做的还要多)。 I use the form constructor to find the textbox parent at runtime and to apply the delegate mouse control.我使用表单构造函数在运行时查找文本框父级并应用委托鼠标控件。 Perhaps you can do this as wel in compile time (Form header), but that seemed a little error-prone to me.也许您可以在编译时(Form header)也这样做,但这对我来说似乎有点容易出错。

One way of doing it is using View + TabIndex, you can do indexing of some other controls on the dialog as first, let say for buttons if there any.一种方法是使用 View + TabIndex,您可以首先对对话框上的其他一些控件进行索引,如果有按钮的话。 Then as if the control tabIndex is not the first ie 0, cursor won't get appear there.然后就好像控件 tabIndex 不是第一个即 0,光标不会出现在那里。

Just try this,试试这个,

private void SetText(string text)
{
     textBox1.ReadOnly = false;
     textBox1.Text = text;
     textBox1.ReadOnly = true;
}

you can use RightToLeft Property of Text Box, set it to true, you will not get rid of the Cursor, but it will get fixed at right corner and it will not appear automatically after every text you type in your text Box.您可以使用文本框的RightToLeft属性,将其设置为 true,您不会摆脱光标,但它会固定在右角,并且不会在您在文本框中输入每个文本后自动出现。 I have used this to develop an application like Windows Calculator.我已经用它开发了一个像 Windows Calculator 这样的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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