C# 在c windows窗体中突出显示文本到语音
在c windows窗体中说话时如何突出显示文本? 我试过这个:C# 在c windows窗体中突出显示文本到语音,c#,C#,在c windows窗体中说话时如何突出显示文本? 我试过这个: public void HighlightWordInRichTextBox(System.Windows.Forms.RichTextBox richTextBox,string word, SolidColorBrush color) { //clear all formatings //System.Windows.Controls.RichTextBox rt= (System.Windows.Contr
public void HighlightWordInRichTextBox(System.Windows.Forms.RichTextBox richTextBox,string word, SolidColorBrush color)
{
//clear all formatings
//System.Windows.Controls.RichTextBox rt= (System.Windows.Controls.RichTextBox.ichTextBox;
TextRange textRange = new TextRange(richTextBox.SelectionStart, richTextBox.Document.ContentEnd);
textRange.ApplyPropertyValue(TextElement.BackgroundProperty, null);
//Current word at the pointer
TextRange tr = FindWordFromPosition(textPointer,word);
if (!object.Equals(tr, null))
{
//set the pointer to the end of "word"
textPointer = tr.End;
//apply highlight color
tr.ApplyPropertyValue(TextElement.BackgroundProperty, color);
}
}
public TextRange FindWordFromPosition(TextPointer position, string word)
{
while (position != null)
{
if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
string textRun = position.GetTextInRun(LogicalDirection.Forward);
// Find the starting index of any substring that matches "word".
int indexInRun = textRun.IndexOf(word);
if (indexInRun >= 0)
{
TextPointer start = position.GetPositionAtOffset(indexInRun);
TextPointer end = start.GetPositionAtOffset(word.Length);
return new TextRange(start, end);
}
}
position = position.GetNextContextPosition(LogicalDirection.Forward);
}
// position will be null if "word" is not found.
return null;
}
void reader_SpeakProgress(object sender, SpeakProgressEventArgs e)
{
//show the synthesizer's current progress
label2.Text= e.Text;
SolidColorBrush highlightColor = new SolidColorBrush(Colors.Yellow);
HighlightWordInRichTextBox(richTextBox1, e.Text, highlightColor);
}
但是这段代码中有错误
在文档中获取错误:
“system.windows.forms.richtextbox”不包含“document”的定义,并且找不到接受类型为“system.windows.forms.richtextbox”的第一个参数的扩展方法“document”。是否缺少using指令或程序集引用
您已经将WPF代码用于WinForms控件。WPF的,但是 您可能正在寻找以下内容:
new TextRange(richTextBox.SelectionStart, richTextBox.SelectionLength);
请将收到的错误附加到问题的末尾。尝试修复错误。您好,欢迎使用StackOverflow!请告诉我们您有哪些错误,以便我们可以帮助您解决问题。您的错误非常清楚。TextRange TextRange=new TextRangerichTextBox.SelectionStart,**richTextBox.Document**.ContentEnd属性文档不存在。