Home > Blockchain >  File handling with windows forms in c#
File handling with windows forms in c#

Time:11-16

I'm using windows form in c# to write data into a text file. I can't able to display information from the list pets to the listbox and save it to a text file.

Can anyone help?

Here is the question:

enter image description here

Here is the code:

 private void btnRun_Click(object sender, EventArgs e)
    {
        List<Pet> pets = new List<Pet>();

        pets.Add(new Pet("Rex", 4, 500.00));
        pets.Add(new Pet("Mittens", 2, 125.50));
        pets.Add(new Pet("Clifford", 7, 30.00));

        writePets(pets);
    }

    
    private void writePets(List<Pet> pets)
    {
        foreach(var a in pets)
        {
            listBox1.Items.Add(a);
        }
        //listBox1.DataSource = pets;
        ofd = new OpenFileDialog();
        if(ofd.ShowDialog() == DialogResult.OK)
        {
            StreamReader sr = new StreamReader(ofd.FileName);
            string line = sr.ReadLine();
            while(line != null)
            {
                listBox1.Items.Add(line);
                line = sr.ReadLine();
            }
            sr.Close();
        }
        
        SaveFileDialog sfd = new SaveFileDialog();
        if(sfd.ShowDialog() == DialogResult.OK)
        {
            StreamWriter sw = new StreamWriter(sfd.FileName);
            listBox1.DataSource = pets;
            sw.Close();
        }
        
    }

CodePudding user response:

Despite the other problems with your code I think what should answer your question is that in the stream writer you need to create a loop that will go through each list box item and write it to the text file.

        SaveFileDialog sfd = new SaveFileDialog();
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            StreamWriter sw = new StreamWriter(sfd.FileName);

            foreach (var listBoxItem in listBox1.Items)
            {
                sw.WriteLine(listBoxItem.ToString());
            }
            
           
            sw.Close();
        }

But take some time to work on the way you are populating your list box. The error you are getting is probably due to you trying to add records to the list box in different manners and its probably not liking it.

CodePudding user response:

Let's just make WritePets do one thing, and do it fairly well. This is how we should write our methods in c#; a method named WritePets (note the pascal case) should write the pets to disk, only. It shouldn't show them in a list, it shouldn't read pets. If you want to show pets, make a ShowPets, if you want to read pets make a ReadPets; there is no penalty for making more methods

private void WritePets(List<Pet> pets)
{
    SaveFileDialog sfd = new SaveFileDialog();
    if(sfd.ShowDialog() != DialogResult.OK)
      return;

    var sb = new StringBuilder();

    //build the string we will write to the file
    for each(var p in pets)
      sb.AppendFomat("{0},{1},{2:F2}", p.Name, p.Age, p.Weight).AppendLine();

    File.AppendAllText(sb.ToString(), sfd.FileName); 
}

Please note, because I strongly suspect that this is homework the code above contains 3 deliberate, but minor, errors that I'd like you to fix so that you think about the code rather than just paste in. One of them will probably require taking a quick look at the documentation for File.

There is also a good chance that I've guessed the names of the properties of a pet incorrectly (like that 500 isn't the pet's weight..) which you'll need to patch up. If you don't want a csv you can adjust the format of a line by tweaking the placeholder string in the AppendFormat call

  •  Tags:  
  • c#
  • Related