C# 禁用在 TextBox 中选择文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13256720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:57:27  来源:igfitidea点击:

Disable selecting text in a TextBox

c#.netwinformstextbox

提问by Victor

I have a textbox with the following (important) properties:

我有一个具有以下(重要)属性的文本框:

this.license.Multiline = true;
this.license.ReadOnly = true;
this.license.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.license.ShortcutsEnabled = false;

It looks like this:

它看起来像这样:

Textbox with highlighted text in it

带有突出显示文本的文本框

How can I disable the user to highlight text in this textbox (I do not want to disable the textbox completely)?

如何禁用用户突出显示此文本框中的文本(我不想完全禁用文本框)?

回答by Mike Perrenoud

Attach to the SelectionChangedevent, and inside the event set e.Handled = true;and the SelectionLength = 0;and that will stop the selection from occuring. This is similar to what it takes to keep a key press from happening.

附加到SelectionChanged事件,并在事件集内e.Handled = true;SelectionLength = 0;将阻止选择发生。这类似于防止按键发生所需的操作。

回答by JG in SD

If you put the text into a label and then but the label into a System.Widnows.Forms.Panelcontrol that has AutoScrollturned on you can display the text w/o it being selectable.

如果您将文本放入标签中,然后将标签放入System.Widnows.Forms.PanelAutoScroll打开的控件中,则可以显示文本而无需选择。

回答by Eduardo Fernandes

In WinForms, the correct method is to assign the event MouseMove and set the SelectionLength to 0.

在 WinForms 中,正确的方法是分配事件 MouseMove 并将 SelectionLength 设置为 0。

I′ve tried here and works perfectly.

我在这里试过并且工作得很好。

回答by Dávid Farkas

private void textBox5_Click(object sender, EventArgs e)
{
    this.textBox5.SelectionStart = this.textBox5.Text.Length;
}

回答by sean.net

You can use a disabled RichTextBoxand reset the color to black afterwards.

您可以使用禁用RichTextBox并在之后将颜色重置为黑色。

RichTextBox rtb = new RichTextBox();
rtb.IsEnabled = false;
rtb.Text = "something";
rtb.SelectAll();
rtb.SelectionColor = Color.Black;
rtb.SelectedText = String.Empty;

回答by Reza Aghaei

To disable selection highlight in a TextBox, you can override WndProcand handle WM_SETFOCUSmessage and replace it with a WM_KILLFOCUS. Please be aware that it doesn't make the TextBoxcontrol read-only and if you need to make it read-only, you should also set ReadOnlyproperty to true. If you set ReadOnlyto true, you can set and its BackColorto Whiteor any other suitable color which you want.

要禁用 a 中的选择突出显示TextBox,您可以覆盖WndProc和处理WM_SETFOCUS消息并将其替换为WM_KILLFOCUS。请注意,它不会将TextBox控件设为只读,如果您需要将其设为只读,还应将ReadOnly属性设置为true. 如果您设置ReadOnly为 true,您可以将其设置BackColorWhite您想要的任何其他合适的颜色。

In below code, I added a SelectionHighlightEnabledproperty to MyTextBoxto make enabling or disabling the selection highlight easy:

在下面的代码中,我添加了一个SelectionHighlightEnabled属性MyTextBoxto 使启用或禁用选择突出显示变得容易:

  • SelectionHighlightEnabled: Gets or sets a value indicating selection highlight is enabled or not. The value is trueby default to act like a normal TextBox. If you set it to falsethen the selection highlight will not be rendered.
  • SelectionHighlightEnabled: 获取或设置一个值,指示选择突出显示是否启用。true默认情况下,该值的行为类似于正常TextBox. 如果您将其设置为,false则不会渲染选择突出显示。
using System.ComponentModel;
using System.Windows.Forms;
public class MyTextBox : TextBox
{
    public MyTextBox()
    {
        SelectionHighlightEnabled = true;
    }
    const int WM_SETFOCUS = 0x0007;
    const int WM_KILLFOCUS = 0x0008;
    [DefaultValue(true)]
    public bool SelectionHighlightEnabled { get; set; }
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_SETFOCUS && !SelectionHighlightEnabled)
            m.Msg = WM_KILLFOCUS;

        base.WndProc(ref m);
    }
}

回答by iato

If you are using XAML / WPF you should use a TextBlockinstead of a TextBox.

如果您使用 XAML/WPF,则应使用TextBlock而不是TextBox

ONLY IF YOU USE A TEXTBOX AS A DISPLAY AND NOT FOR INPUT- as TextBlock makes it seem as if the text is "engraved" onto the form itself, and not within a textbox. To get a Border around the TextBlock (if you wish), you can either do it :

仅当您使用 TEXTBOX 作为显示而非输入时- 因为 TextBlock 使文本看起来好像是“雕刻”在表单本身上,而不是在文本框中。要在 TextBlock 周围设置边框(如果您愿意),您可以这样做:

In XAMLsuch as :

XAML 中,例如:

<Border BorderThickness="1" BorderBrush="Gray">
    <TextBlock Background="White" Text="Your Own TextBlock"/>
</Border>

Or dynamically in C# Code:

或者在C# 代码中动态:

//Create a Border object
Border border = new Border();
border.BorderThickness = new Thickness(1);
border.BorderBrush = Brushes.Black;

//Create the TextBlock object           
TextBlock tb = new TextBlock();
tb.Background = Brushes.White;
tb.Text = "Your Own TextBlock";

//Make the text block a child to the border
border.Child = tb;

回答by clamchoda

Since the standard TextBox doesn't have the SelectionChanged event, here's what I came up with.

由于标准 TextBox 没有 SelectionChanged 事件,这就是我想出的。

private void TextBox1_MouseMove(object sender, MouseEventArgs e)
{
    TextBox1.SelectionLength = 0;
}

回答by Minaketan

I came across of this thread for my same issue I faced. Somehow I resolved it as below,

我遇到了这个线程,因为我遇到了同样的问题。我以某种方式解决了它,如下所示,

if (sender != null)
                {
                    e.Handled = true;
                    if((sender as TextBox).SelectionLength != 0)
                        (sender as TextBox).SelectionLength = 0;
                }

Verifying if the length changed other than 0, then only set it to 0, resolves the recursive loop.

验证长度是否更改为 0 以外的值,然后仅将其设置为 0,以解决递归循环。

回答by Jadam

Very Easy Solution

非常简单的解决方案

Find a Label and into the textbox go to mousedown event and set focus to the label

找到一个标签并在文本框中转到 mousedown 事件并将焦点设置为标签

This is in VB and can be easily converted into C#

这是在VB中,可以很容易地转换成C#

Private Sub RichTextBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseDown
        Label1.Focus()
    End Sub