Disable text selection in the text box

I have a text box 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 a user to select text in this text box (I don't want to completely disable the text box)?

+5
source share
9 answers

Join the SelectionChanged event and inside the event set e.Handled = true; and SelectionLength = 0; and it will stop the choice. This is similar to what is required to prevent keystrokes.

+8
source

If you put the text in the label, and then, but the shortcut in the System.Widnows.Forms.Panel control in which AutoScroll is AutoScroll , you can display the text without being able to select it.

+4
source

To turn off selection highlighting in a TextBox , you can override WndProc and process the WM_SETFOCUS message and replace it with WM_KILLFOCUS . Remember that the TextBox read control, and if you need to make it read-only, you must also set the ReadOnly property to true . If you set ReadOnly to true, you can set it and BackColor to White or any other suitable color that you want.

In the code below, I added the SelectionHighlightEnabled property of MyTextBox to enable or disable just the selection selection:

  • SelectionHighlightEnabled : Gets or sets a value indicating whether the selection is selected or not. The default value is true to act like a regular TextBox . If you set it to false highlight selection will not be displayed.
 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); } } 
+4
source

After that, you can turn off the RichTextBox and reset the color to black.

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

If you are using XAML / WPF, you should use TextBlock instead of TextBox .

ONLY IF YOU USE THE TEXT AS A DISPLAY AND NOT FOR ENTRANCE - since TextBlock pretends that the text is "engraved" on the form itself, and not inside the text field. To get the border around the TextBlock (if you want), you can do this:

In XAML, for example:

 <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
source

Since the standard TextBox does not have a SelectionChanged event, this is what I came up with.

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

I came across this topic for my own problem, which I encountered. Somehow I solved it as shown below

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

Checking if a length other than 0 has changed and then setting only 0 allows a recursive loop.

0
source

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

I tried here and it works great.

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

Source: https://habr.com/ru/post/1444421/


All Articles