0

I wanted to make a wpf program that when you click the Generate button the class SSales will get/store the arrays of values to the class then return it to the listbox. New here. Sorry.

   private void GenerateButton_Click(object sender, EventArgs e)
   {
        Random rand = new Random();

        int[] year = { 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 };

        double[] Sales = new double[10];

        for (int i = 0; i < 10; i++)
        {
            Sales[i] = rand.Next(1000, 50000);

        }

        SSales yearSale = new SSales(year, Sales);

        for (int j = 0; j < 10; j++){

        //I want the listbox to display the values of yearSale.Year and yearSale.Sales

            listBox1.Items.Add(yearSale);
        }
    }

public class SSales
{
    public int[] Year { get; set; }
    public double[] Sales { get; set; }

    public SSales(int[] iYear, double[] dSales)
    {
        Year = iYear;
        Sales = dSales;
    }

    public override string ToString()
    { 
       //I'm trying to make this format "2001     $25,000.00" then return it to listbox

        return string.Format("{0}\t\t{1:C0}", Year, Sales);  
    }

}
ASh
  • 34,632
  • 9
  • 60
  • 82
  • I'd recommend creating a readonly property that returns the formatted string, instead of overriding `ToString`, then set the `DisplayMemberPath` of the `ListBox` to the name of that property. – Bradley Uffner Mar 10 '20 at 03:33
  • Whats is the problem? Not sure what the problem is. – Jonathan Alfaro Mar 10 '20 at 04:12
  • `listBox1.Items.Add(yearSale.ToString());` ? – vasily.sib Mar 10 '20 at 04:53
  • In WPF you would typically use a ListView with a GridView with two columns. Then turn the SSales class into something that hold the sales for a *single* year, i.e. two properties `int Year` and `double Sales`. Then assign a collection of Sales objects to the ItemsSource property of the ListView, and in the two column templates bind to the two properties. Start reading here: [Data Templating Overview](https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/data-templating-overview). – Clemens Mar 10 '20 at 08:07

2 Answers2

1

Since you are adding 10 SSales objects to the ListBox, each object should accept a single int and a single double:

public class SSales
{
    public int Year { get; set; }
    public double Sales { get; set; }

    public SSales(int iYear, double dSales)
    {
        Year = iYear;
        Sales = dSales;
    }

    public override string ToString()
    {
        return string.Format("{0}\t\t{1:C0}", Year, Sales);
    }
}

Try this:

private void GenerateButton_Click(object sender, EventArgs e)
{
    Random rand = new Random();

    int[] year = { 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 };

    double[] Sales = new double[10];

    for (int i = 0; i < 10; i++)
    {
        Sales[i] = rand.Next(1000, 50000);
    }

    for (int j = 0; j < 10; j++)
    {
        listBox1.Items.Add(new SSales(year[j], Sales[j]));
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • How do I randomize the elements' index inside the array so that everytime I click the generate button there will be a randomized position for the years? TIA! – Justin Lopez Mar 10 '20 at 21:02
  • @JustinLopez: Please ask a new question if you have another issue. – mm8 Mar 11 '20 at 07:41
-1

That's not how you use ToString - it isn't supposed to return an array.

Instead, you should create a function on SSales that takes a parameter, and that would give you the formatted result of the sale you want. Something like this:

public string GetFormattedSale(int s) { string.Format("{0}\t\t{1:C0}", Year[s] , Sales[s]);
}

You could then call this from your button click code like this:

listBox1.Items.Add(yearSale.GetFirmattedSale(j));

If you really must use ToString, the you need to make another class that contains the data for a single sale, not all 10 sales. You can then implement ToString on just that single sale.

Kramii
  • 8,379
  • 4
  • 32
  • 38