[英]How do I add a line to a rich textbox?
我编写了以下代码来创建一个简单的字典应用程序:
private void btnDefine_Click(object sender, EventArgs e)
{
//string word = txtWords.Text;
XmlDocument xDoc = new XmlDocument();
try
{
string [] words = txtWords.Text.Split('\n');
foreach (string word in words ){
xDoc.Load("http://www.dictionaryapi.com/api/v1/references/collegiate/xml/" + word + "?key=[KEY]");
txtWords.Text = (xDoc.SelectSingleNode("entry_list/entry/def/dt").InnerText);
Clipboard.SetText(txtWords.Text);
lblCopied.Text = "Copied to the clipboard!";
}
}
catch
{
MessageBox.Show("That is not a word in the dictionary, please try again.", "Word not found in the dictionary", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
}
}此表单包含一个丰富的文本框,您可以在其中输入单词,它将为您定义单词。 现在,只要在文本框中输入一个单词,就可以得到定义。 但是,如果我在文本框中输入两个或多个单词,则会得到列表中最后一个单词的定义,该如何定义以便所有定义都以新行显示。 IE,如果我在文本框中输入三个单词并按btnDefine,我将在文本框中获得所有三个单词的定义。
您可以将定义输出与输入的方式类似:在单独的行上。 参见String.Join
。
List<string> definitions = new List<string>();
foreach (string word in words )
{
xDoc.Load("http://www.dictionaryapi.com/api/v1/references/collegiate/xml/" + word + "?key=[KEY]");
string definition = (xDoc.SelectSingleNode("entry_list/entry/def/dt").InnerText);
definitions.Add(definition);
}
txtWords.Text = String.Join("\n", definitions);
Clipboard.SetText(txtWords.Text);
lblCopied.Text = "Copied to the clipboard!";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.