问题
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.
Here's the code...
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.
回答1:
This is the problem:
newsFeedControl.Text += newNews["title"]...
Never directly set the Text
or all previous formatting is messed up. Always use AppendText
instead:
newsFeedControl.AppendText(newNews["title"] + ...);
The same is true for any other manipulation of the Text
property. Always use the SelectedText
instead and Cut()
, Copy()
or Paste()
..!
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..
来源:https://stackoverflow.com/questions/28933957/richtextbox-formatting-not-correct