我試圖將列表框的內容保存到文本文件中。它的工作原理,而不是文本輸入到列表框中,我得到這個:將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