如何访问列表以填充列表框项目并显示特定项目。来自 CSV

发布于 2025-01-10 08:03:39 字数 3094 浏览 0 评论 0原文

你好,程序员,我正在开发一个程序,该程序从 csv 文件中获取信息,格式为:

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

我已将 csv 信息写入一个列表,其中的类属性为字符串日期、十进制结束、十进制开头、十进制高位、十进制低位。 我使用 while 语句来读取每一行,用逗号分隔它们并将它们分配给正确的类属性。然后我将信息添加到列表中。

我已将日期添加到 GUI 上的列表框中,并希望在用户单击某个日期时进行此操作。每个属性的所有值都将显示在 GUI 上的某些文本框中。我不确定如何访问 listbox1_SelectedIndexChanged 中的列表,以根据选择的索引将正确的值添加到其文本框中。我知道我需要以某种方式返回列表,但我似乎无法确切地弄清楚如何做到这一点。

有人可以向我提供朝着正确方向迈出的一步吗? 我希望能够分析该行的每一列并在列表框中显示不同的结果。

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;
    }

}

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;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云归处 2025-01-17 08:03:39

您可以将 listBox1DisplayMember 属性设置为 Date,而不是执行 listBox1.Items.Add(string.Format(" {0}", theData.Date),只需执行 listBox1.Items.Add(theData)

private void listBox1_SelectedIndexChanged(object sender, EventArgs) 中e) 然后您可以执行以下操作(需要 C#9/.NET 5 或更高版本):

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();
}

以上版本的更兼容版本是:

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();
    }
}

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文