0

My program reads data (date, time, temperature value) from a CSV file and indicates the stored times after selecting a date in a list box. When clicking on a time of the list box, the associated temperature value should be displayed.

The CSV file is set up as follows:

date, time, temperature value

Example of the CSV-File:

date        time    temperature value
18.09.2022  19:05   28,33
18.09.2022  20:05   29,33
18.09.2022  21:05   30,33
18.09.2022  22:05   31,33
18.09.2022  23:05   32,33

So my program insert the times 19:05, 20:05, 21:05, 22:05, 23:05 into a listbox.

For example, if I click 21:05, the temperature value should be output 30.33.

Here's how I read the CSV file:

        private List<werte> value_liste; 
        //...

        private void read_values(string path)
        {
            using (TextFieldParser csvParser = new TextFieldParser(path))
            {                
                csvParser.CommentTokens = new string[] { "#" };
                csvParser.SetDelimiters(new string[] { "," });
                csvParser.HasFieldsEnclosedInQuotes = true;

                // Skip the row with the column names
                csvParser.ReadLine();

                while (!csvParser.EndOfData)
                {
                    fields = csvParser.ReadFields();                 
                    value_list.Add(new value { date = fields[0], time = fields[1],
                        temperature = Convert.ToDouble(fields[2]) });
                }
            }
        }

        public class values
        {
            public string date { get; set; }
            public string time { get; set; }
            public double temperatur { get; set; }
        }

private void time_listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string listbox_text = time_listbox.SelectedItem.ToString();
            
    // Here I want to get the corresponding temperature value for
    // the clicked time listbox item
}

How to get the corresponding temperature value?

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Vik0809
  • 379
  • 4
  • 18
  • You file doesn't look like a csv, those aren't comma separated values. I'm not familiar with this csv parser, but try to use a different delimiter here: ```csvParser.SetDelimiters(new string[] { "," });```. Try to use space or \t. – Monsieur Merso Sep 18 '22 at 12:55
  • What is your CultureInfo ? – Orenico Sep 18 '22 at 12:57
  • My Cultureinfo is de-DE – Vik0809 Sep 18 '22 at 13:02
  • refer to this post you may find it useful https://stackoverflow.com/questions/2583362/how-to-convert-string-to-double-with-proper-cultureinfo – Orenico Sep 18 '22 at 13:10
  • @Vik0809: How do you set the `ItemsSource` of the `ListBox`? And what's the difference between `values` and `werte`? – mm8 Sep 19 '22 at 14:13

2 Answers2

1

You can use .SelectedIndex property and get the temprature from your value_liste

private void time_listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string listbox_text = time_listbox.SelectedItem.ToString();
    var index = time_listbox.SelectedIndex;
    var temperatur = value_liste[index].temperatur;
}
Muhammad Sulaiman
  • 2,399
  • 4
  • 14
  • 28
0

Cast time_listbox.SelectedItem your type and access the property directly:

private void time_listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    values selectedItem = time_listbox.SelectedItem as values;
    if (values != null)
    {
        double temp = values.temperatur;
    }
}

This assumes you have set the ItemsSource of the ListBox to an IEnumerable<values>.

Or use the DLR:

private void time_listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    dynamic selectedItem = time_listbox.SelectedItem;
    double temp = values.temperatur;
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • "So my program insert the times 19:05, 20:05, 21:05, 22:05, 23:05 into a listbox".. It seems like the listbox is populated with times only (even the event handler's name is time_listbox_...) – Muhammad Sulaiman Sep 19 '22 at 14:36