我试图将列表框的内容保存到文本文件中。它的工作原理,而不是文本输入到列表框中,我得到这个:将Windows窗体列表框保存为文本文件C#
System.Windows.Forms.ListBox+ObjectCollection
这是我用于窗体本身的相关代码。
listString noted = new listString();
noted.newItem = textBox2.Text;
listBox1.Items.Add(textBox2.Text);
var radioOne = radioButton1.Checked;
var radioTwo = radioButton2.Checked;
var radioThree = radioButton3.Checked;
if (radioButton1.Checked == true)
{
using (StreamWriter sw = new StreamWriter("C:\\windowsNotes.txt"))
{
sw.Write(listBox1.Items);
}
}
else if (radioButton2.Checked == true)
{
using (StreamWriter sw = new StreamWriter("C:\\Users\\windowsNotes.txt"))
{
sw.Write(listBox1.Items);
}
}
else if (radioButton3.Checked == true)
{
using (StreamWriter sw = new StreamWriter("../../../../windowsNotes.txt"))
{
sw.Write(listBox1.Items);
}
}
else
{
MessageBox.Show("Please select a file path.");
}
}
类是只是简单的一个:
namespace Decisions
{
public class listString
{
public string newItem {get; set;}
public override string ToString()
{
return string.Format("{0}", this.newItem);
}
}
}
循环'listBox1.Items'并写入它们 – I4V 2013-04-27 22:19:49