'How to access list to fill listbox items and show specific items. From CSV

Hello programmers I'm working on a program that takes information from a csv file in the format of

6-Jan-17/19963.80,19906.96,199999.63,19834.08.

I have written the csv information into a list with class properties of String Date, Decimal Closing, Decimal Opening, Decimal High, Decimal Low. I used a while statement to read each line, separate them by commas and assign them to their correct class property. Then I added the information into the List.

I have added the date to the listbox on my GUI and want to make it so that when a user clicks a certain date. All the values for each of its properties will show up on in some textboxes on the GUI. I'm unsure of how to access the list in my listbox1_SelectedIndexChanged to add the correct values to their textboxes depending on which index is selected. I know I need to return the list somehow but I cant seem to figure out exactly how to do this.

Can someone please provide me with a step in the correct direction with this? I want to be able to analyze each column of the row and show different results inside my listbox aswell.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace meade_13_5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            List<DowData> list = new List<DowData>();
            //grab the DJIA csv file using StreamReader
            using (StreamReader sr = new StreamReader("DJIA.csv"))
            {
                string line;
                //Assigns cvs file lines into their role and adds to the List
                while ((line = sr.ReadLine()) != null)
                {
                    //Split the lines by comma 
                    string[] columns = line.Split(',');
                    //Create new list for the data
                    DowData theData = new DowData();
                    //Change format of datetime to date
                    string dateString = DateTime.Parse(columns[0]).ToString("dd/mm/yyyy");
                    theData.Date = dateString;
                    theData.Closing = Decimal.Parse(columns[1]);
                    theData.Opening = Decimal.Parse(columns[2]);
                    theData.High = Decimal.Parse(columns[3]);
                    theData.Low = Decimal.Parse(columns[4]);
                    //Adds the values to the list
                    list.Add(theData);
                    //Adds date to the listbox to select
                    listBox1.Items.Add(string.Format("{0}", theData.Date));
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //listBox1.Items.Add(string.Format("{0} | {1} | {2} | {3} | {4}", theData.Date, theData.Closing, theData.Opening, theData.High, theData.Low));

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtClosing.Text = listBox1.SelectedIndex.ToString();
            txtOpening.Text = listBox1.SelectedIndex.ToString();
            txtHigh.Text = listBox1.SelectedIndex.ToString();
            txtLow.Text = listBox1.SelectedIndex.ToString();
        }
    }
}
class DowData
{
    public String Date
    {
        get;
        set;
    }
    public decimal Closing
    {
        get;
        set;
    }
    public decimal Opening
    {
        get;
        set;
    }
    public decimal High
    {
        get;
        set;
    }
    public decimal Low
    {
        get;
        set;
    }

}


Solution 1:[1]

You can set the DisplayMember property of listBox1 to Date, and instead of doing listBox1.Items.Add(string.Format("{0}", theData.Date), just do listBox1.Items.Add(theData).

In private void listBox1_SelectedIndexChanged(object sender, EventArgs e) you can then do (Requires C#9/.NET 5 or higher):

if(sender is ListBox { SelectedItem: DowData dataItem } )
{
    txtClosing.Text = dataItem.Closing.ToString();
    txtOpening.Text = dataItem.Opening.ToString();
    txtHigh.Text = dataItem.High.ToString();
    txtLow.Text = dataItem.Low.ToString();
}

A more compatible version of the above would be:

var lstSender = sender as ListBox;
if(lstSender != null)
{
    var dataItem = lstSender.SelectedItem as DowData;
    if(dataItem != null)
    {
        txtClosing.Text = dataItem.Closing.ToString();
        txtOpening.Text = dataItem.Opening.ToString();
        txtHigh.Text = dataItem.High.ToString();
        txtLow.Text = dataItem.Low.ToString();
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1