• Log in or sign up for an account here: https://www.sythe.org/login

Save/Load ListBox C#

  • Thread starter Thread starter Skeletron
  • Start date Start date
S

Skeletron

Guest
Load:

I used this code snippet in C# for a cracker that loaded lines of text into a listbox:

{
this.listBox1.Items.Clear();
OpenFileDialog Open = new OpenFileDialog();
Open.Filter = "Text Document|*.txt|All Files|*.*";
try
{
Open.ShowDialog();
StreamReader Import = new StreamReader(Convert.ToString(Open.FileName));
while (Import.Peek() >= 0)
listBox1.Items.Add(Convert.ToString(Import.ReadLine()));
}
catch (Exception ex)
{
MessageBox.Show(Convert.ToString(ex.Message));
return;
}
}

Save:

This is the exact opposite of the last code snippet but this one saves it to a text file:

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 < listBox4.Items.Count; I++)
{
Write.WriteLine(Convert.ToString(listBox4.Items));
}
Write.Close();
}
catch (Exception ex)
{
MessageBox.Show(Convert.ToString(ex.Message));
return;
}


Both of these code snippets are very easy to incorporate into your own project by changing the names in here to the updated names in your code.

It is also easy to modify so you can import it into a System.Collections.ArrayList or even a System.Collections.ObjectModel.Collection if you are using points etc.
 
Thanks for the tutorial. I'm keen to use this when my computer's internet with Visual C# 2005 Express Edition starts working again.

Do you use C#--normal or Visual C# .NET 2005 Express edition?
You should try it out. The Visual C#.net express edition is free. Just search it in Google.
 
Runescape Gamblingrunescape gambling
Back
Top