我有一个文本框,具有以下(重要)属性:
this.license.Multiline = true;
this.license.ReadOnly = true;
this.license.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.license.ShortcutsEnabled = false;
它看起来像这样:
我如何禁止用户在此文本框中突出显示文本(但我不想完全禁用文本框)?
附加到 SelectionChanged
事件,然后在事件内设置 e.Handled = true;
和 SelectionLength = 0;
,这将停止选择发生。这类似于防止按键操作发生的方法。
AutoScroll
的System.Windows.Forms.Panel
控件中,则可以显示文本而无需选择。TextBox
中的选择高亮,您可以重写WndProc
并处理WM_SETFOCUS
消息,将其替换为WM_KILLFOCUS
。请注意,这不会使TextBox
控件只读,如果您需要使其只读,还应将ReadOnly
属性设置为true
。如果您将ReadOnly
设置为true,则可以设置它的BackColor
为White
或任何其他适合您的颜色。SelectionHighlightEnabled
属性到MyTextBox
,使启用或禁用选择高亮变得容易:
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);
}
}
我遇到了与这个线程相同的问题。我按照以下步骤解决了它:
if (sender != null)
{
e.Handled = true;
if((sender as TextBox).SelectionLength != 0)
(sender as TextBox).SelectionLength = 0;
}
验证长度是否改变,如果不为0,则将其设置为0,解决递归循环。
非常简单的解决方案
找到一个标签并进入文本框的mousedown事件,将焦点设置为该标签
这是使用VB编写的,可以很容易地转换为C#
Private Sub RichTextBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseDown
Label1.Focus()
End Sub
由于标准TextBox没有SelectionChanged事件,因此我想出了以下解决方案。
private void TextBox1_MouseMove(object sender, MouseEventArgs e)
{
TextBox1.SelectionLength = 0;
}
MouseLeave
和 KeyPress
。MouseLeave
用于在鼠标不再位于文本框区域时取消选择,而 KeyPress
用于在用户按下 Shift + Key Up/Down 进行选择时取消选择。 - Death GOD 7如果您正在使用XAML / WPF,则应使用而不是。
仅当您将TextBox用作显示而非输入时 - 因为TextBlock使文本似乎“刻在”表单本身上,而不是在文本框内。如果您希望在TextBlock周围获得边框,则可以执行以下操作:
在XAML中,例如:
<Border BorderThickness="1" BorderBrush="Gray">
<TextBlock Background="White" Text="Your Own TextBlock"/>
</Border>
或者在 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;
private void textBox1_Enter(object sender, EventArgs e)
{
this.anotherControl.Focus(); // Preferably a readonly control in case the user starts typing
}
您可以使用禁用的 RichTextBox
,然后将颜色重置为黑色。
RichTextBox rtb = new RichTextBox();
rtb.IsEnabled = false;
rtb.Text = "something";
rtb.SelectAll();
rtb.SelectionColor = Color.Black;
rtb.SelectedText = String.Empty;
private void textBox5_Click(object sender, EventArgs e)
{
this.textBox5.SelectionStart = this.textBox5.Text.Length;
}
this.license.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
是针对winforms的。 - tukaef