<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 中格式化单词

        Format words in RichTextBox(在 RichTextBox 中格式化单词)

          <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 中格式化单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
                  Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
                  Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
                  How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
                  how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
                  JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)
                    <tbody id='Ys98C'></tbody>
                    • <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'>

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