<tfoot id='Hax8Z'></tfoot>

    <legend id='Hax8Z'><style id='Hax8Z'><dir id='Hax8Z'><q id='Hax8Z'></q></dir></style></legend>

    1. <i id='Hax8Z'><tr id='Hax8Z'><dt id='Hax8Z'><q id='Hax8Z'><span id='Hax8Z'><b id='Hax8Z'><form id='Hax8Z'><ins id='Hax8Z'></ins><ul id='Hax8Z'></ul><sub id='Hax8Z'></sub></form><legend id='Hax8Z'></legend><bdo id='Hax8Z'><pre id='Hax8Z'><center id='Hax8Z'></center></pre></bdo></b><th id='Hax8Z'></th></span></q></dt></tr></i><div id='Hax8Z'><tfoot id='Hax8Z'></tfoot><dl id='Hax8Z'><fieldset id='Hax8Z'></fieldset></dl></div>

          <bdo id='Hax8Z'></bdo><ul id='Hax8Z'></ul>

        <small id='Hax8Z'></small><noframes id='Hax8Z'>

        在 RichTextBox 中格式化单词

        时间:2023-05-21

          <small id='eHb1D'></small><noframes id='eHb1D'>

          • <i id='eHb1D'><tr id='eHb1D'><dt id='eHb1D'><q id='eHb1D'><span id='eHb1D'><b id='eHb1D'><form id='eHb1D'><ins id='eHb1D'></ins><ul id='eHb1D'></ul><sub id='eHb1D'></sub></form><legend id='eHb1D'></legend><bdo id='eHb1D'><pre id='eHb1D'><center id='eHb1D'></center></pre></bdo></b><th id='eHb1D'></th></span></q></dt></tr></i><div id='eHb1D'><tfoot id='eHb1D'></tfoot><dl id='eHb1D'><fieldset id='eHb1D'></fieldset></dl></div>
              <tbody id='eHb1D'></tbody>
                <tfoot id='eHb1D'></tfoot>

                <legend id='eHb1D'><style id='eHb1D'><dir id='eHb1D'><q id='eHb1D'></q></dir></style></legend>
                • <bdo id='eHb1D'></bdo><ul id='eHb1D'></ul>
                • 本文介绍了在 RichTextBox 中格式化单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用以下代码查找以@"开头的每一行并将其设置为粗体:

                  I am using the following code to find each line that starts with "@" and format it by making it bold:

                  foreach (var line in tweetText.Document.Blocks)
                          {
                              var text = new TextRange(line.ContentStart,
                                             line.ContentEnd).Text;
                              line.FontWeight = text.StartsWith("@") ?
                                             FontWeights.Bold : FontWeights.Normal;
                          }
                  

                  但是,我想使用代码来查找每个单词而不是以@"开头的行,因此我可以格式化如下段落:

                  However, I would like to use the code to find each word instead of line beginning with "@" so I could format a paragraph like:

                  废话废话 @username废话废话 @anotherusername

                  推荐答案

                  这可能需要一些优化,因为我做得很快,但这应该可以帮助你开始

                  This could probably use some optimization as I did it quick, but this should get you started

                  private void RichTextBox_TextChanged(object sender, TextChangedEventArgs e)
                  {    
                       tweetText.TextChanged -= RichTextBox_TextChanged;
                       int pos = tweetText.CaretPosition.GetOffsetToPosition(tweetText.Document.ContentEnd);
                  
                       foreach (Paragraph line in tweetText.Document.Blocks.ToList())
                       {
                          string text = new TextRange(line.ContentStart,line.ContentEnd).Text;
                  
                          line.Inlines.Clear();
                  
                          string[] wordSplit = text.Split(new char[] { ' ' });
                          int count = 1;
                  
                          foreach (string word in wordSplit)
                          {
                              if (word.StartsWith("@"))
                              {
                                  Run run = new Run(word);
                                  run.FontWeight = FontWeights.Bold;
                                  line.Inlines.Add(run);
                              }
                              else
                              {
                                  line.Inlines.Add(word);
                              }
                  
                              if (count++ != wordSplit.Length)
                              {
                                   line.Inlines.Add(" ");
                              }
                          }
                       }
                  
                       tweetText.CaretPosition = tweetText.Document.ContentEnd.GetPositionAtOffset(-pos);
                       tweetText.TextChanged += RichTextBox_TextChanged;
                  }
                  

                  这篇关于在 RichTextBox 中格式化单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C# MVC.Net 格式货币不带小数 下一篇:Winforms 将文本框格式化为货币

                  相关文章

                  • <bdo id='Ys98C'></bdo><ul id='Ys98C'></ul>
                  <i id='Ys98C'><tr id='Ys98C'><dt id='Ys98C'><q id='Ys98C'><span id='Ys98C'><b id='Ys98C'><form id='Ys98C'><ins id='Ys98C'></ins><ul id='Ys98C'></ul><sub id='Ys98C'></sub></form><legend id='Ys98C'></legend><bdo id='Ys98C'><pre id='Ys98C'><center id='Ys98C'></center></pre></bdo></b><th id='Ys98C'></th></span></q></dt></tr></i><div id='Ys98C'><tfoot id='Ys98C'></tfoot><dl id='Ys98C'><fieldset id='Ys98C'></fieldset></dl></div>

                  <tfoot id='Ys98C'></tfoot>

                      <small id='Ys98C'></small><noframes id='Ys98C'>

                    1. <legend id='Ys98C'><style id='Ys98C'><dir id='Ys98C'><q id='Ys98C'></q></dir></style></legend>