RichTextBox格式不正确 [英] RichTextBox Formatting Not Correct

查看:167
本文介绍了RichTextBox格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我要从JSON文件中获取内容,并将每个项目附加到富文本框控件中.但是,当我追加和修改外观时,它看起来像是随机的...

So I'm grabbing content from a JSON file and appending each item to a rich text box control. However, when I append and modify the look...it randomly looks like...

当然中间的格式正确,外面的格式不正确.

Of course the one in the middle is formatted correctly, it's the ones on the outside that aren't.

这也是完全随机的.有些可以工作,有些则不能.那就是让我失望的原因.

It's completely random also. Some will work, others won't. That's what's throwing me off.

这是代码...

private void getNews()
    {
        WebClient newsClient = new WebClient();
        Stream newsStream = newsClient.OpenRead("http://example.com/file.json");
        StreamReader newsReader = new StreamReader(newsStream);
        String newsReaderContent = newsReader.ReadToEnd();
        Console.WriteLine(newsReaderContent);
        JObject newsResults = JObject.Parse(newsReaderContent);
        foreach (var newNews in newsResults["news"])
        {
            newsFeedControl.Text += newNews["title"] +
                                    Environment.NewLine +
                                    newNews["date"] +
                                    Environment.NewLine +
                                    newNews["content"] +
                                    Environment.NewLine +
                                    Environment.NewLine;
            setNewsContent(newNews);
        }
    }

    private void setNewsContent(JToken news)
    {
        List<int> indexDate = new List<int>();
        List<int> indexContent = new List<int>();
        List<int> indexTitle = new List<int>();
        string postTitle = news["title"].ToString();
        string postDate = news["date"].ToString();
        string postContent = news["content"].ToString();
        indexTitle = indexFeed(postTitle);
        indexDate = indexFeed(postDate);
        indexContent = indexFeed(postContent);
        foreach (int item in indexTitle)
        {
            newsFeedControl.Select(item, postTitle.Length);
            newsFeedControl.SelectionColor = Color.White;
            newsFeedControl.SelectionFont = new System.Drawing.Font("Segoe UI", 11, FontStyle.Underline);
        }
        foreach (int item in indexDate)
        {
            newsFeedControl.Select(item, postDate.Length);
            newsFeedControl.SelectionColor = Color.White;
            newsFeedControl.SelectionFont = new System.Drawing.Font("Segoe UI", 7, FontStyle.Italic);
        }
        foreach (int item in indexContent)
        {
            newsFeedControl.Select(item, postContent.Length);
            newsFeedControl.SelectionColor = Color.White;
            newsFeedControl.SelectionFont = new System.Drawing.Font("Segoe UI", 10);
        }
    }

    private List<int> indexFeed(String word)
    {
        List<int> indexes = new List<int>();
        int i = 0;
        int ind = 0;
        while (i < newsFeedControl.Text.Length)
        {
            ind = newsFeedControl.Find(word, i, RichTextBoxFinds.WholeWord);
            if (ind != -1)
            {
                indexes.Add(ind);
                i = i + ind + 1;
            }
            else
                return indexes;
        }
        return indexes;
    }

我不确定是什么原因造成这种混乱,或者我有什么办法可以解决. 指出正确方向的任何帮助将不胜感激.谢谢你.

I'm not sure what's causing this mess up or if there's something I can do to help it. Any help pointing in the right direction would be greatly appreciated. Thank you SO.

推荐答案

这是问题所在

newsFeedControl.Text += newNews["title"]...

从不直接设置Text,否则以前的所有格式都弄乱了.始终使用AppendText代替:

Never directly set the Text or all previous formatting is messed up. Always use AppendText instead:

newsFeedControl.AppendText(newNews["title"] + ...);

对于Text属性的任何其他操作也是如此.始终使用SelectedText而不是Cut()Copy()Paste() ..!

The same is true for any other manipulation of the Text property. Always use the SelectedTextinstead and Cut(), Copy() or Paste()..!

看似随机的结果来自RTB尝试忽略损坏的标签的事实,因此某些情况看起来比其他情况要好.看看Rtf属性,了解我的意思.

The seemingly random consequences come from the fact that the RTB tries ignore corrupted tags, so some things look better than others. Have a look at the Rtf property to see what I mean..

这篇关于RichTextBox格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆