gpt4 book ai didi

c# - 当用户在 richtextbox 中输入时,我可以使用 WPF 进行自动单词替换吗

转载 作者:可可西里 更新时间:2023-11-01 11:49:51 24 4
gpt4 key购买 nike

    public void overallTextReplace(RichTextBox[] rtb) {
string[] keyword = { "FCI", "CNG", "DCR", "EZR", "VASC", "CND" };
string[] newString = { "Forecourt Controller","Case Number Declined" ,"Case Number Given", "Dispenser Card reader", "Enhanced Zone Router", "Verifone Authorized Service Contractor" };
TextRange[] text = new TextRange[rtb.Length];

for (int I = 0; I < rtb.Length; I++) {
text[I] = new TextRange(rtb[I].Document.ContentStart, rtb[I].Document.ContentEnd);
}


for (int I = 0; I < text.Length; I++) {
for (int K = 0; K < keyword.Length; K++) {
TextPointer current = text[I].Start.GetInsertionPosition(LogicalDirection.Forward);
string textInRun = current.GetTextInRun(LogicalDirection.Forward);
if (!string.IsNullOrEmpty(textInRun)) {
int index = textInRun.IndexOf(keyword[K]);
if (index != -1) {
TextPointer selectionStart = current.GetPositionAtOffset(index, LogicalDirection.Forward);
TextPointer selectionEnd = selectionStart.GetPositionAtOffset(keyword.Length, LogicalDirection.Forward);
TextRange selection = new TextRange(selectionStart, selectionEnd);
selection.Text = newString[K];
selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Regular);
rtb[I].Selection.Select(selection.Start, selection.End);
rtb[I].Focus();
}
}
current = current.GetNextInsertionPosition(LogicalDirection.Forward);
}
}
}

好的,当传递给函数时,这段代码将查看 WPF 表单中的所有 RichTextBox,然后查找列出的关键字并将它们替换为 newString。我遇到的问题是程序从头到尾只看一行文本。如果它检测到一个换行符,它不会看过去,例如:第 1 行:FCI 是一个燃料 Controller 。它替换它就好了,但如果我在第 2 行有更多内容,它就不会进行替换。如果有什么不同的话,它们是 6 个 richTextBoxes 被传递给这个函数。

只是发现了一个错误,但与我的第一个问题无关。所以看起来有 6 个数组索引会阻止代码运行并在 TextRange selection = new Textrange(selectionStart, selectionEnd) 上抛出一个空引用;但如果我用 VASC 作为要替换的词,他们也不异常(exception)。我不确定为什么。

最佳答案

对于 winforms:试试这个(虽然我没有运行这段代码,但从逻辑上讲它应该可以工作):

 public void overallTextReplace(RichTextBox[] rtb) 
{
string[] keyword = { "FCI", "CNG", "DCR", "EZR", "VASC", "CND" };
string[] newString = { "Forecourt Controller","Case Number Declined" ,"Case Number Given", "Dispenser Card reader", "Enhanced Zone Router", "Verifone Authorized Service Contractor" };
for (int i = 0; i < rtb.Length; i++)
{
for (int j = 0; j < 6; j++)
{
rtb[i].Rtf=rtb[i].Rtf.Replace(keyword[j],newString[j]);
}
}
}

对于 wpf:

for (int i = 0; i < rtb.Length; i++) 
{
RichTextBox rtb_wording= rtb[i];
var textRange = new TextRange(rtb_wording.Document.ContentStart, rtb_wording.Document.ContentEnd);
string rtf;
using (var memoryStream = new MemoryStream())
{
textRange.Save(memoryStream, DataFormats.Rtf);
rtf = ASCIIEncoding.Default.GetString(memoryStream.ToArray());
}
for (int j = 0; j < 6; j++)
{
rtf =rtf.Replace(keyword[j],newString[j]);
}
MemoryStream stream = new MemoryStream (ASCIIEncoding.Default.GetBytes(rtf));
rtb_wording.SelectAll();
rtb_wording.Selection.Load(stream, DataFormats.Rtf);
}

关于c# - 当用户在 richtextbox 中输入时,我可以使用 WPF 进行自动单词替换吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32751741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com