I have a textbox with the following (important) properties:
this.license.Multiline = true;
this.license.ReadOnly = true;
this.license.ScrollBars = System.Win
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.
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;
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.
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
private void textBox5_Click(object sender, EventArgs e)
{
this.textBox5.SelectionStart = this.textBox5.Text.Length;
}
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.