每当我将RichTextBox.Enabled属性设置为false时,其背景颜色将自动设置为灰色,因为它设置为控制面板中设置的系统颜色.即使将其设置为禁用,如何将其颜色更改为黑色?
我正在创建一个带有RichTextBox的函数,并且可以访问关键字列表和'badwords'.我需要突出显示我在用户输入时在RichTextBox中找到的任何关键字和坏词,这意味着每次发布编辑键时都会调用该函数.
我写过这个函数,但是框中的文字和光标闪烁得太过舒适.
我发现了一个解决方案 - 在我编辑和格式化文本时禁用RichTextBox重绘自身的能力.但是,我知道这样做的唯一方法是覆盖"WndProc"函数并拦截(我将要收集的内容)重绘消息如下:
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == 0x00f) {
if (paint)
base.WndProc(ref m);
else
m.Result = IntPtr.Zero;
}
else
base.WndProc(ref m);
}
Run Code Online (Sandbox Code Playgroud)
布尔突出'paint'在我开始突出显示之前设置为false,在我完成时设置为true.但正如我所说,我制作的函数必须采用RichTextBox; 我不能使用子类.
那么,有没有办法禁止从外部自动重新绘制RichTextBox?
我正在尝试使用RichTextBox和我的第一感觉:"使用它有多复杂!"......太棒了......
所以我试图突出显示我的RichTextBox中包含的文本.
我目前有以下代码:
TextRange range = new TextRange(MyTextInput.Document.ContentStart, MyTextInput.Document.ContentEnd);
range.Text = @"TOP a multiline text or file END";
Regex reg = new Regex("(top|file|end)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
foreach (Match match in reg.Matches(range.Text))
{
TextPointer start = range.Start.GetPositionAtOffset(match.Index, LogicalDirection.Forward);
TextPointer end = range.Start.GetPositionAtOffset(match.Index + match.Length, LogicalDirection.Backward);
// text contains the exact match I want
string text = range.Text.Substring(match.Index, match.Length);
// here the highlighted text isn't the text I searched...
TextRange textrange = new TextRange(start, end);
textrange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
textrange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
}
Run Code Online (Sandbox Code Playgroud)
TOP …
如何RichTextBox在没有边距,边框,填充等的情况下制作?换句话说,以同样的方式显示内容TextBlock?我试过这个:
<RichTextBox Margin="0" Padding="0" Grid.Row="0" BorderThickness="0" >
<FlowDocument >
<Paragraph>LLL</Paragraph>
</FlowDocument>
</RichTextBox>
<TextBlock>LLL</TextBlock>
Run Code Online (Sandbox Code Playgroud)
但结果产生的仍然不是我想要的:

在文档内容之前还有一些空间(也可能在文档的顶部或底部之后......).我该如何删除它?
如果你有兴趣我为什么需要这个:我试图让HB回答我的问题在WPF中创建吉他和弦编辑器来处理字距并且我不希望字符之间有不自然的空间.
所以ControlTemplate至少不仅如此,因为下面的代码会产生完全相同的结果(如上图所示):
<RichTextBox Margin="0" Padding="0" Grid.Row="0" BorderThickness="0">
<RichTextBox.Template>
<ControlTemplate>
<ScrollViewer Padding="0" Margin="0" x:Name="PART_ContentHost"/>
</ControlTemplate>
</RichTextBox.Template>
<FlowDocument PagePadding="0">
<Paragraph Padding="0" Margin="0" >LLL</Paragraph>
</FlowDocument>
</RichTextBox>
Run Code Online (Sandbox Code Playgroud)
而且我认为这将是问题容易回答...... 有趣的现象:当我有模板设置,我设置PagePadding="0"的FlowDocument 是显示我要布局在VisualStudio的设计师- 直到我运行演示.在演示中再次出错......当我关闭演示时,设计师再次出错了.这是VS的一个小错误,或者它实际上设置为正确的布局一段时间但是然后某些东西会将值更改PagePadding回某个错误的值?
丹尼尔罗斯编辑的答案也不适合我.这是XAML:
<FlowDocument PagePadding="{Binding PagePadding}">
<Paragraph x:Name="paragraph" Padding="0"
TextIndent="0" Margin="0,0,0,0" >hello</Paragraph>
</FlowDocument>
Run Code Online (Sandbox Code Playgroud)
这是代码:
public static DependencyProperty PagePaddingProperty …Run Code Online (Sandbox Code Playgroud) 我正在使用WinForms RichTextBox.看起来当RichTextBox在表单上时,\r\n转换为\n.这是一个测试:
我有两个丰富的文本框.一个是richTextBox1,放在表格上:
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(37, 12);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(100, 96);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
Run Code Online (Sandbox Code Playgroud)
另一个是rtb我现场制作的.当我运行此代码时(在窗体的load事件中):
var rtb = new RichTextBox();
string enl = "Cheese" + Environment.NewLine + "Whiz";
rtb.Text = enl;
string ncr = rtb.Text;
MessageBox.Show(string.Format("{0}{1}{2}{3}---{4}{5}{6}{7}{8}{9}",
enl.Replace("\n", "\\n").Replace("\r", "\\r"), Environment.NewLine,
ncr.Replace("\n", "\\n").Replace("\r", "\\r"), Environment.NewLine,
Environment.NewLine,
(enl == ncr), Environment.NewLine,
enl.Contains(Environment.NewLine), Environment.NewLine,
ncr.Contains(Environment.NewLine)));
/*
Cheese\r\nWhiz
Cheese\r\nWhiz …Run Code Online (Sandbox Code Playgroud) 我知道这里有很多"如何加载文本"的问题,但没有一个答案有帮助,我认为可能是在运行时创建了富文本框.
我正在创建一个聊天客户端,所以我有一个分成行的富文本框,消息如下:{Name}:{Message}\r \n
我想加入这个名字,我尝试了很多代码示例,但这是我最接近它的工作:
int length = textBox.Text.Length;
textBox.Text += roomChatMessage.from + ": " + roomChatMessage.text + "\r\n";
textBox.Select(length, roomChatMessage.from.Length);
textBox.SelectionFont = new Font(textBox.Font, FontStyle.Bold);
Run Code Online (Sandbox Code Playgroud)
第一条消息,它完全正常,名称以粗体显示.但是当我添加第二条消息时,即使第二轮我选择了起始索引(这个例子是37),一切都变成了粗体,但是所有过去的消息都变成了粗体!
任何可能导致这种情况的想法?提前致谢!
我有一个WPF RichTextBox isReadOnly设置为True.我希望用户能够点击RichTextBox中包含的HyperLink,而不必按住它们Ctrl.
HyperLink上的Click事件似乎没有触发,除非Ctrl被按下,所以我不确定如何继续.
检测WPF RichTextBox/FlowDocument是否为空的最佳方法是什么?
如果文档中仅存在文本,则以下内容有效.如果它包含UIElement,则不是
new TextRange(Document.ContentStart, Document.ContentEnd).IsEmpty
Run Code Online (Sandbox Code Playgroud) 我正在使用TinyMCE提供富文本编辑文本编辑器.但是线之间的线间距太大了.我添加了一个屏幕截图,显示了按Enter键时的行间距.可以做些什么呢 
我是wpf的新手,我想将富文本框的数据及其格式(Italic,Colored,Bold ..)存储到数据库(Mysql)中.目前,当我保存数据时,将忽略格式化.此外,当我从数据库中将其加载回富文本框时,它会显示同一行中的所有文本.期待您的帮助和建议!
public void save()
{
MySqlConnection conn = new MySqlConnection(connString);
MySqlCommand command = conn.CreateCommand();
string richText = new TextRange(rt1.Document.ContentStart, rt1.Document.ContentEnd).Text;
string s = WebUtility.HtmlEncode(richText);
command.Parameters.AddWithValue("@s", s);
command.CommandText = "insert into proc_tra (procedures) values (@s)";
conn.Open();
command.ExecuteNonQuery();
conn.Close();
}
public void load()
{ MySqlConnection conn = new MySqlConnection(connString);
MySqlCommand command = conn.CreateCommand();
command.CommandText = "select * from proc_tra where id_pt=4";
rt1.Document.Blocks.Clear();
conn.Open();
MySqlDataReader dr;
dr = command.ExecuteReader();
string k="";
while (dr.Read())
{
k += dr["procedures"].ToString();
}
var p = new …Run Code Online (Sandbox Code Playgroud)