Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, September 26, 2008 8:51 AM | 1 vote
How to use SaveFileDialog for save text file from listbox ?
Thanks, gazeclub
All replies (7)
Friday, September 26, 2008 9:19 AM ✅Answered
Your question is confusing. What do you mean with "from listbox". Anyways, here's some code. I didn't try to compile it, so it's still rough on the edges.
| 1 | ListBox listBox1 = new ListBox(); |
| 2 | listBox1.Items.Add("one"); |
| 3 | listBox1.Items.Add("two"); |
| 4 | listBox1.Items.Add("three"); |
| 5 | listBox1.Items.Add("four"); |
| 6 | listBox1.Items.Add("five"); |
| 7 | |
| 8 | SaveFileDialog save = new SaveFileDialog(); |
| 9 | if (save.ShowDialog() == DialogResult.Ok) { |
| 10 | using(StreamWriter writer = new StreamWriter(save.OpenFile())) { |
| 11 | for (int i = 0; i < listBox1.Items.Length; i++) { |
| 12 | writer.WriteLine(listBox1.Items[i]); |
| 13 | } |
| 14 | } |
| 15 | } |
if a problem looks too big, break it into smaller objects
Tuesday, September 30, 2008 7:30 AM ✅Answered | 1 vote
Hello,
I think ruijoel has already solved your problem. Here I just add some more and correct one little mistake. We use ListBox.Items.Count, and not ListBox.Items.Length here.
SaveFileDialog save = new SaveFileDialog();
save.FileName = "DefaultOutputName.txt";
save.Filter = "Text File | *.txt";
if (save.ShowDialog() == DialogResult.OK)
{
StreamWriter writer = new StreamWriter(save.OpenFile());
for (int i = 0; i < listBox1.Items.Count; i++)
{
writer.WriteLine(listBox1.Items[i].ToString());
}
writer.Dispose();
writer.Close();
}
If you have any more questions, please just tell me.
Thanks, also to ruijoel,
Best Regards,
Lingzhi
Please remember to mark the replies as answers if they help and unmark them if they provide no help. http://forums.msdn.microsoft.com/en-US/csharpide/thread/8e9ed0d7-11ff-402a-8489-9b5f05eeb706 http://forums.msdn.microsoft.com/en-US/vssetup/thread/60424309-bd78-4ca2-b618-03c4a16123b6
Friday, September 26, 2008 9:11 AM
Hi The SaveFileDialog just returns a Filename i.e. it doesn't actually save anything. This Filename can be used with a StreamWriter to write the text to a file.
| SaveFileDialog sd = new SaveFileDialog(); |
| if (sd.ShowDialog() == DialogResult.OK) |
| { |
| using (StreamWriter sw = new StreamWriter(sd.FileName)) |
| { |
| sw.Write(... your text here ...); |
| sw.Flush; |
| sw.close; |
| } |
| } |
Do you like to write the content of the list box ?
Friday, September 26, 2008 9:17 AM
In the first i have a result in listbox (e.g. x+y=3) so i want to save that to text file (.txt) .
Thanks.
Friday, September 26, 2008 9:30 AM
Suppose my code is :
double test1= double.Parse(textBox1.Text);
double test2 = 12 + test1;
string strTest2 = test2.ToString();
listBox1.Items.Add(strTest2);
if the result in listBox1 is 15 so i want to save that to text file?
Wednesday, October 1, 2008 1:55 AM
Now i can solve this problem, thanks everybody for help.
Friday, July 6, 2012 10:03 PM
When I use Lingzhi's code, my listbox looks like
test
test
test
but my .txt file looks like
Serial.Form1+Line
Serial.Form1+Line
Serial.Form1+Line
How can I fix this?