-4

How to save a file in a list box into a .txt file?

return string.Format("{0} = {1}",   
JaialaiNumber, "₱" + Bet.ToString()); How to save this file on a .txt file?

Here's the code of my program. ): ^_^ .

public partial class frmJaialai : Form
{
    List<Jaialai> source;

    public frmJaialai()
    {
        InitializeComponent();
    }        

    private void frmJaialai_Load(object sender, EventArgs e)
    {
        source = new List<Jaialai>();          
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        int bet;
        if (string.IsNullOrEmpty(txtJaialaiNumber.Text) || !Int32.TryParse(txtBet.Text, out bet))
        {
            MessageBox.Show("Must be a required field.", "Entry Error");
            return;
        }
        var existingProduct = source.Where(x => x.JaialaiNumber == txtJaialaiNumber.Text).SingleOrDefault();
        if (existingProduct != null)
        {
            existingProduct.Bet += bet;
        }
        else
            source.Add(new Jaialai { Bet = bet, JaialaiNumber = txtJaialaiNumber.Text });

        lstJaialaiNumbersList.DataSource = null;
        lstJaialaiNumbersList.DataSource = source;

        txtJaialaiNumber.Text = "";      
    }

    public class Jaialai
    {
        public string JaialaiNumber { get; set; }
        public int Bet { get; set; }

        public override string ToString()
        {
            return string.Format("{0} = {1}", 
                JaialaiNumber, "₱" + Bet.ToString());
        }
    }
Koopakiller
  • 2,719
  • 3
  • 30
  • 45
Mercy
  • 13
  • 4

2 Answers2

1

If I understood you correct, you want to write your List<Jaialai> to a file. In this case you can use File.WriteAllLines.

File.WriteAllLines("FILE-PATH", source.Select(x => x.ToString()));

The first Parameter is the path of the file. The second Parameter is an IEnumerable<string> which contains the lines.
The Select method calls the ToString method for all items to get strings which can be written to the file. You can also change the lambda to write some other stuff per record to the file.

Koopakiller
  • 2,719
  • 3
  • 30
  • 45
0

To Save File Use System.IO.File.WriteAllText.

PurTahan
  • 621
  • 9
  • 23
  • 3
    Please add more details and explanations for OP about why he or she needs your code ;). – shA.t Apr 26 '15 at 11:03