[英]How do add List items to a RichTextBox?
The code im trying wich is not working: 我正在尝试的代码不起作用:
private void clearRichtextBox()
{
richTextBox2.Clear();
foreach (KeyValuePair<string, List<string>> kvp in LocalyKeyWords)
{
richTextBox2.AppendText("Url: " + kvp.Key + " --- " + "Localy KeyWord: " + kvp.Value);
}
}
LocalyKeyWords is a List > type of Dictionary. LocalyKeyWords是“列表”>“字典”类型。 Where string is the Key and List is the Value.
其中字符串是键,列表是值。
http://www.google.com is the Key for example and google is the value. 例如, http://www.google.com是键,而google是值。
Now in this case LocalyKeywords contain 3 indexs each one have a Key and a Value. 现在在这种情况下,LocalyKeywords包含3个索引,每个索引都有一个键和一个值。 I want after i cleared the richTextBox2 to add from the List all the Keys and Values to the richTextBox in a Format like this:
我想在清除richTextBox2之后从列表中将所有键和值以如下格式添加到richTextBox中:
Url: http://www.google.com --- Localy Keyword: google
Url: http://www.cnet.com --- Localy Keyword: cnet
Url: http://www.microsoft.com --- Localy Keyword: microsoft
But im not getting it the way i did it. 但即时通讯没有我做的方式。
How can i do it ? 我该怎么做 ? Something wrong with the foreach i prefer to make it with FOR and not foreach.
foreach出了点问题,我更喜欢使用FOR而不是foreach。
The List is built as Key,Value in the text file on my hard disk fhr format is like this: 该列表在我的硬盘上的文本文件中以Key,Value的形式构建,格式如下:
http://www.cnet.com,cnet
Then in the constructor im reading it from the text file and put back the key and value into the List like this: 然后在构造函数中,im从文本文件中读取它,并将键和值放回List中,如下所示:
private void richTextBoxLoadKeys(Dictionary<string, List<string>> dictionary, string FileName)
{
string line = System.String.Empty;
using (StreamReader sr = new StreamReader(keywords))
{
while ((line = sr.ReadLine()) != null)
{
string[] tokens = line.Split(',');
dictionary.Add(tokens[0], tokens.Skip(1).ToList());
richTextBox2.AppendText("Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]);
}
sr.Close();
}
}
After loading the key and values in the constructor and using a breakpoint on the List i see: 将键和值加载到构造函数中并在List上使用断点后,我看到:
您应该遍历kvp.Value,它也是一个列表,因此您必须在RichTextBox中添加列表中的每个项目。
You need to either iterate through the strings in Value or just report the first string in Value. 您需要遍历Value中的字符串或只报告Value中的第一个字符串。 What you are getting is the ToString() of a List.
您得到的是列表的ToString()。 I'm curious whether you really need a List for the value since your example only has one Local Keyword per key.
我很好奇您是否真的需要一个值列表,因为您的示例每个键只有一个本地关键字。 Perhaps you should be creating a Dictionary instead.
也许您应该改为创建字典。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.