C# 为SyndicationFeed标题使用与richtextbox-C中的摘要不同的字体#

C# 为SyndicationFeed标题使用与richtextbox-C中的摘要不同的字体#,c#,.net,C#,.net,我正在用C#制作RSS阅读器。它获取提要并将其添加到富文本框中。有没有办法让提要中的标题与富文本框中的摘要分开?如果是,怎么做?这是我的密码: if (feedsList.SelectedItem != null) { feedTxt.Text = ""; string url = feedsList.GetItemText(feedsList.SelectedItem); Uri myUri = new Uri(url, UriKind.Absolute);

我正在用C#制作RSS阅读器。它获取提要并将其添加到富文本框中。有没有办法让提要中的标题与富文本框中的摘要分开?如果是,怎么做?这是我的密码:

if (feedsList.SelectedItem != null)
{
     feedTxt.Text = "";
     string url = feedsList.GetItemText(feedsList.SelectedItem);
     Uri myUri = new Uri(url, UriKind.Absolute);
     XmlReader reader = XmlReader.Create(url);
     SyndicationFeed feed = SyndicationFeed.Load(reader);
     foreach (SyndicationItem item in feed.Items)
     {
          feedTxt.Text = feedTxt.Text + item.Title.Text;
          feedTxt.Text = feedTxt.Text + "\n\n" + item.Summary.Text + "\n\n";
     }
}

提前谢谢

您可以使用
RichTextBox
控件上的
Selection
属性。大概是这样的:

if (feedsList.SelectedItem != null)
{
    feedTxt.Text = "";
    string url = feedsList.GetItemText(feedsList.SelectedItem);
    Uri myUri = new Uri(url, UriKind.Absolute);
    XmlReader reader = XmlReader.Create(url);
    SyndicationFeed feed = SyndicationFeed.Load(reader);

    foreach (SyndicationItem item in feed.Items)
    {
        // Store current content length of RichTextBox
        int currentLength = feedTxt.Length;

        // Append new content
        feedTxt.Text += item.Title.Text + "\n\n";
        feedTxt.Text += item.Summary.Text + "\n\n";

        // Set the font for Title using selection
        feedTxt.SelectionStart = currentLength.Length;
        feedTxt.SelectionLength = item.Title.Text.Length;
        feedTxt.SelectionFont = new System.Drawing.Font("Tahoma", 24);

        // Set the font for Summary using selection
        feedTxt.SelectionStart = currentLength + item.Title.Text.Length;
        feedTxt.SelectionLength = item.Summary.Text;
        feedTxt.SelectionFont = new System.Drawing.Font("Arial", 16);

        // Reset Selection
        txtTest.SelectionStart = 0;
        txtTest.SelectionLength = 0;
    }
}
更新 看起来您不能同时添加新内容和样式(Winforms可能有限制?)。我的简单解决方案是在一次扫描中附加所有内容,计算所有要设置样式的选择,并将它们推送到列表中

然后,在所有选择中进行第二次循环,并进行样式设置

您首先需要此结构:

  struct ArticleSelection
  {
      public int TitleStart { get; set; }
      public int TitleEnd { get; set; }
      public int SummaryStart { get; set; }
      public int SummaryEnd { get; set; }
  };
更新代码

if (feedsList.SelectedItem != null)
{
    feedTxt.Text = "";
    string url = feedsList.GetItemText(feedsList.SelectedItem);
    Uri myUri = new Uri(url, UriKind.Absolute);
    XmlReader reader = XmlReader.Create(url);
    SyndicationFeed feed = SyndicationFeed.Load(reader);


    // Create List to store our selections
    List<ArticleSelection> articleSelections = new List<ArticleSelection>();

    // Loop all incoming content
    foreach (SyndicationItem item in feed.Items)
    {
        // Store current content length of RichTextBox
        int currentLength = feedTxt.Length;

        // Append new content
        feedTxt.Text += item.Title.Text + "\n\n";
        feedTxt.Text += item.Summary.Text + "\n\n";

        // Calculate selection
        articleSelections.Add(new ArticleSelection()
        {
            TitleStart = currentLength,
            TitleEnd = feed.Item1.Length,
            SummaryStart = currentLength + feed.Item1.Length,
            SummaryEnd = feedTxt.Text - currentLength // This accounts for new lines above
        }); ; 
    }


    // Loop through the content and style it
    foreach (ArticleSelection selection in articleSelections)
    { 
        // Set the selection for Title
        txtTest.SelectionStart = selection.TitleStart;
        txtTest.SelectionLength = selection.TitleEnd;
        txtTest.SelectionFont = new Font("Tahoma", 24);

        // Set the selection for Summary
        txtTest.SelectionStart = selection.SummaryStart;
        txtTest.SelectionLength = selection.SummaryEnd;
        txtTest.SelectionFont = new Font("Arial", 14);
    }

    // Remove Selection
    txtTest.DeselectAll();
}
if(feedsList.SelectedItem!=null)
{
feedTxt.Text=“”;
字符串url=feedsList.GetItemText(feedsList.SelectedItem);
Uri myUri=新Uri(url,UriKind.Absolute);
XmlReader=XmlReader.Create(url);
SyndicationFeed=SyndicationFeed.Load(读卡器);
//创建列表以存储我们的选择

列表

谢谢!这真的很有帮助!唯一的问题是它只能处理提要中的一个项目。有没有一种方法可以处理多个项目?@UnknownGobo你使用了最新的代码吗?我编辑了以循环方式工作。我使用了最新的代码。这是该程序的屏幕截图:@UnknownGobo图像不可公开查看抱歉,我需要eload谷歌硬盘让它工作。我的错。