Disable selecting text in a TextBox

前端 未结 10 1825
再見小時候
再見小時候 2020-12-06 12:42

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

this.license.Multiline = true;
this.license.ReadOnly = true;
this.license.ScrollBars = System.Win         


        
相关标签:
10条回答
  • 2020-12-06 13:16

    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)
  • 2020-12-06 13:17

    If you are using XAML / WPF you should use a TextBlock instead of a 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 :

    In XAML such as :

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

    Or dynamically in C# Code:

    //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;
    
    0 讨论(0)
  • 2020-12-06 13:18

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

    0 讨论(0)
  • 2020-12-06 13:21

    Very Easy Solution

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

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

    Private Sub RichTextBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseDown
            Label1.Focus()
        End Sub
    
    0 讨论(0)
  • 2020-12-06 13:25
    private void textBox5_Click(object sender, EventArgs e)
    {
        this.textBox5.SelectionStart = this.textBox5.Text.Length;
    }
    
    0 讨论(0)
  • 2020-12-06 13:26

    Attach to the SelectionChanged event, 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.

    0 讨论(0)
提交回复
热议问题