CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Hybrid View

  1. #1
    Join Date
    Apr 2010

    Read from file into hashtable and populate textBoxes

    I'm creating an agenda program using windows form application, I'm trying to load data from a file into a hash table and display this data into text boxes. The labels on the text boxes are dates but these dates are of the form (dd MMMM dddd e.g 30 November Friday) and are used as key values for the hash table. The dates in the file are of the form (dd MM yyyy). A sample of the data in the file is shown.

    10/07/2012,Having an exam at 10.30 *

    30/11/2012,Will go to swimming at 15.30 *

    When I load the form, the data should be shown in the corresponding textbox. For example Will go to swimming at 15.30 * must be shown under text box with label "30 November Friday" (because it corresponds to date 30/11/2012). My code so far is this:-

     while ((line = sr.ReadLine()) != null)
                     string[] inData = line.Split(',');
                     key = inData[0];
                     value = inData[1];
                     hT.Add(key, value);
                     foreach (DictionaryEntry dataEntry in hT)
                         //Will throw a FormatException if 'dataEntry.Key' doesn't represent a valid date format
                         DateTime keyAsDate = Convert.ToDateTime(key);
                         //This overload of 'ToString' uses CurrentCulture
                         string comparisonKey = keyAsDate.ToString("dd MMMM dddd");
                         if (label1.Text.Equals(comparisonKey, StringComparison.OrdinalIgnoreCase))
                             textBox1.Text = dataEntry.Value.ToString();
                         if (label2.Text.Equals(comparisonKey, StringComparison.OrdinalIgnoreCase))
                             textBox2.Text = dataEntry.Value.ToString();
                         if (label3.Text.Equals(comparisonKey, StringComparison.OrdinalIgnoreCase))
                             textBox3.Text = dataEntry.Value.ToString();
    However the textBoxes are not being populated. I cant find what the error is. How can i get the data populated? thanks

  2. #2
    Join Date
    Feb 2011
    United States

    Re: Read from file into hashtable and populate textBoxes

    So, first, your design using a key of the date allows for only one event per day... I would do something like creating Dictionary of lists, keyed on the date where the list is all the events that occur on that date. Then you can do:

    string[] lines = File.ReadAllLines(path);
    Dictionary<string, List<string>> dayDictionary = new Dictionary<string, List<string>>();
    for( string line in lines )
        //Parse the lines to get the date and some text string about it
        string date = //whatever
        string text = //whatever
        //Check to see if this date is already in the dictionary
        if( ! dayDictionary.ContainsKey(date) )
            dayDictionary.Add(date, new List<string>());  //If not create a new list
        //Now add the displaytext to the day's list
    You can do it slightly more performantly with TryGetValue, but I omitted that here for clarity.

    Then write a second loop that queries the dayDictionary based on the label of the textbox you want to query and then dump each item of the list into a textbox.

    Does that make sense?

    That probably isn't the best design pattern ever, but I didn't want to get too far away from your initial design.
    Best Regards,

    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.