当我将项目保存在列表框中时,之间有太多行
我的程序中的一个小功能需要帮助。
我的程序从网站获取项目,然后将其保存到列表框。它将它从该列表框传输到另一个列表框,将项目放在自己的行中。
从那里我将文件保存在文本文件中。
当我检查桌面上的文件时,每个项目之间有两行,即使在我的列表框中它没有显示任何项目。你能帮忙吗?
以下是将项目添加到其他列表框的代码:
listBox6.Items.Add(listBox5.SelectedItem.ToString() + ":" + txtBox2.Text + Environment.NewLine);
这是保存列表框项目的代码:
StreamWriter Write;
SaveFileDialog Open = new SaveFileDialog();
try
{
Open.Filter = ("Text Document|*.txt|All Files|*.*");
Open.ShowDialog();
Write = new StreamWriter(Open.FileName);
for (int I = 0; I < listBox6.Items.Count; I++)
{
Write.WriteLine(Convert.ToString(listBox6.Items[I]));
}
Write.Close();
}
catch (Exception ex)
{
MessageBox.Show(Convert.ToString(ex.Message));
return;
}
I need help in a little feature in my program.
My program gets items from a website then saves it to a listbox. From that listbox it transfers it to another listbox putting the items, each in their own line.
From there I save the file in a text file.
When I check the file on my desktop there are two lines in between each item even though in my listbox it didnt show any items in between. Can you help?
Here is the code to add the items to the other listbox:
listBox6.Items.Add(listBox5.SelectedItem.ToString() + ":" + txtBox2.Text + Environment.NewLine);
Here is the code to save the listbox items:
StreamWriter Write;
SaveFileDialog Open = new SaveFileDialog();
try
{
Open.Filter = ("Text Document|*.txt|All Files|*.*");
Open.ShowDialog();
Write = new StreamWriter(Open.FileName);
for (int I = 0; I < listBox6.Items.Count; I++)
{
Write.WriteLine(Convert.ToString(listBox6.Items[I]));
}
Write.Close();
}
catch (Exception ex)
{
MessageBox.Show(Convert.ToString(ex.Message));
return;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
Write.WriteLine
替换为Write.Write
,因为您之前已经添加了Environment.NewLine
。Replace
Write.WriteLine
withWrite.Write
since you already addedEnvironment.NewLine
before.最好删除显式的新行:
Better yet remove the explicit new line: