Saving textbox data based on date selected

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 5
Reputation: andrewp3498 is an unknown quantity at this point 
Solved Threads: 0
andrewp3498 andrewp3498 is offline Offline
Newbie Poster

Saving textbox data based on date selected

 
0
  #1
Mar 17th, 2008
Hi

I am looking to create a program to help with my running training. It would be like a diary where I could select a date using either a DateTimePicker or MonthCalendar and then enter text into the textbox based on that date.

Can you please help me with coding this as it would be great to keep track of my training?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 178
Reputation: sbv is an unknown quantity at this point 
Solved Threads: 8
sbv's Avatar
sbv sbv is offline Offline
Junior Poster

Re: Saving textbox data based on date selected

 
0
  #2
Mar 18th, 2008
Originally Posted by andrewp3498 View Post
Hi

I am looking to create a program to help with my running training. It would be like a diary where I could select a date using either a DateTimePicker or MonthCalendar and then enter text into the textbox based on that date.

Can you please help me with coding this as it would be great to keep track of my training?
What you want?
What you dont know. adding controls or db connectivity or what?
Last edited by sbv; Mar 18th, 2008 at 1:42 am.
Accept Challenges and Enjoy Coding... :)
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 5
Reputation: andrewp3498 is an unknown quantity at this point 
Solved Threads: 0
andrewp3498 andrewp3498 is offline Offline
Newbie Poster

Re: Saving textbox data based on date selected

 
0
  #3
Mar 18th, 2008
Originally Posted by sbv View Post
What you want?
What you dont know. adding controls or db connectivity or what?
Sorry for not being so clear. I am stuck with all of it, i've been looking through various sites such as msdn for days but cannot find a solution

I basically want to now from start to finish if possible. I have just got a form with a DateTimePicker and Textbox on currently.

I have figured out how to save to a text file but i'd end up with an awful lot of textfiles if i did it that way because i'd have an rtf file for each day of the week

Thanks again
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 52
Reputation: bwkeller is an unknown quantity at this point 
Solved Threads: 6
bwkeller bwkeller is offline Offline
Junior Poster in Training

Re: Saving textbox data based on date selected

 
0
  #4
Mar 18th, 2008
Originally Posted by andrewp3498 View Post
...and then enter text into the textbox based on that date.
I'm not clear what you mean by this, can you elaborate?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 5
Reputation: andrewp3498 is an unknown quantity at this point 
Solved Threads: 0
andrewp3498 andrewp3498 is offline Offline
Newbie Poster

Re: Saving textbox data based on date selected

 
0
  #5
Mar 18th, 2008
Originally Posted by bwkeller View Post
I'm not clear what you mean by this, can you elaborate?
Sure, I mean if you select a date on the MonthCalendar you can enter text in the textbox. Then if you select another date the textbox will clear and allow you to enter more data for that particular date. If you then click back to a date where text has been entered previously the program would remember that and display it in the textbox.

Hope that makes sense

I have actually got that part of it working. Here is the code;

  1. Public Class Form1
  2.  
  3.  
  4.  
  5. Dim DataDictionary As Dictionary(Of String, String)
  6.  
  7. Dim CurrentDate As Date
  8.  
  9.  
  10.  
  11. Private Sub MonthCalendar1_DateSelected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateSelected
  12.  
  13. DateSelect()
  14.  
  15. End Sub
  16.  
  17.  
  18.  
  19. Public Sub New()
  20.  
  21. InitializeComponent()
  22.  
  23. CurrentDate = New Date
  24.  
  25. DataDictionary = New Dictionary(Of String, String)
  26.  
  27. DateSelect()
  28.  
  29. End Sub
  30.  
  31.  
  32.  
  33. Private Sub DateSelect()
  34.  
  35. If MonthCalendar1.SelectionStart.Date = MonthCalendar1.SelectionEnd.Date Then
  36.  
  37. CurrentDate = MonthCalendar1.SelectionStart.Date
  38.  
  39. If Not ComboBox1.Items.Contains(CurrentDate.Date.ToLongDateString()) Then
  40.  
  41. ComboBox1.Items.Add(CurrentDate.Date.ToLongDateString())
  42.  
  43. DataDictionary.Add(CurrentDate.Date.ToLongDateString(), "")
  44.  
  45. ComboBox1.SelectedItem = CurrentDate.Date.ToLongDateString()
  46.  
  47. Else
  48.  
  49. ComboBox1.SelectedItem = CurrentDate.Date.ToLongDateString()
  50.  
  51. If DataDictionary.ContainsKey(CurrentDate.Date.ToLongDateString()) Then
  52.  
  53. TextBox1.Text = DataDictionary.Item(CurrentDate.Date.ToLongDateString())
  54.  
  55. Else
  56.  
  57. TextBox1.Text = ""
  58.  
  59. End If
  60.  
  61. End If
  62.  
  63. End If
  64.  
  65. End Sub
  66.  
  67.  
  68.  
  69. Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  70.  
  71. If DataDictionary.ContainsKey(CurrentDate.Date.ToLongDateString()) Then
  72.  
  73. TextBox1.Text = DataDictionary.Item(CurrentDate.Date.ToLongDateString())
  74.  
  75. End If
  76.  
  77. End Sub
  78.  
  79.  
  80.  
  81. Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  82.  
  83. If DataDictionary.ContainsKey(CurrentDate.Date.ToLongDateString()) Then
  84.  
  85. DataDictionary.Item(CurrentDate.Date.ToLongDateString()) = TextBox1.Text
  86.  
  87. End If
  88.  
  89. End Sub
  90.  
  91.  
  92.  
  93. End Class


What I need help with now is saving the data so it is still there when the program is closed and re-opened. Does anybody know the solution for this?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 52
Reputation: bwkeller is an unknown quantity at this point 
Solved Threads: 6
bwkeller bwkeller is offline Offline
Junior Poster in Training

Re: Saving textbox data based on date selected

 
0
  #6
Mar 19th, 2008
Well, you could save to an Access database that you would update over time or an XML file that you would replace each time if you don't want a database, but you need to decide what method you want to use. I think yo can get help here for either method.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 5
Reputation: andrewp3498 is an unknown quantity at this point 
Solved Threads: 0
andrewp3498 andrewp3498 is offline Offline
Newbie Poster

Re: Saving textbox data based on date selected

 
0
  #7
Mar 19th, 2008
I like the idea of using XML for my program

I was given the following bit of code but I don't really understand it or where it needs to fit into my code. Any help would be appreciated

  1. <?xml version="1.0"?>
  2.  
  3. <dates>
  4.  
  5. <entry date="...">...</entry>
  6.  
  7. <entry date="...">...</entry>
  8.  
  9. </dates>
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 52
Reputation: bwkeller is an unknown quantity at this point 
Solved Threads: 6
bwkeller bwkeller is offline Offline
Junior Poster in Training

Re: Saving textbox data based on date selected

 
0
  #8
Mar 20th, 2008
Originally Posted by andrewp3498 View Post
I like the idea of using XML for my program

I was given the following bit of code but I don't really understand it or where it needs to fit into my code. Any help would be appreciated

  1. <?xml version="1.0"?>
  2.  
  3. <dates>
  4.  
  5. <entry date="...">...</entry>
  6.  
  7. <entry date="...">...</entry>
  8.  
  9. </dates>
OK, this article should get you going as it covers how to use the System.Xml.XmlTextWriter class. The example code is in C# but just cut and paste it here and click the Convert to VB.NET button for VB code. If you have questions, let us know.
Last edited by bwkeller; Mar 20th, 2008 at 11:16 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 5
Reputation: andrewp3498 is an unknown quantity at this point 
Solved Threads: 0
andrewp3498 andrewp3498 is offline Offline
Newbie Poster

Re: Saving textbox data based on date selected

 
0
  #9
Mar 21st, 2008
I now have the following code which works perfectly. You can paste the code into a blank Windows Application Project to see it in action.

Now I need to add a couple more textboxes but I am told the code below only supports a single key and value.

How can I change the code to allow further textboxes to be added?



  1. Public Class Form1
  2.  
  3. Dim DataDictionary As Dictionary(Of String, String)
  4.  
  5. Dim DataDocument As Xml.XmlDocument
  6.  
  7. Dim FilePath As String
  8.  
  9. Dim CurrentDate As Date
  10.  
  11. Friend WithEvents DateSelector As System.Windows.Forms.MonthCalendar
  12.  
  13. Friend WithEvents DateCombo As System.Windows.Forms.ComboBox
  14.  
  15. Friend WithEvents DateInfo As System.Windows.Forms.TextBox
  16.  
  17.  
  18.  
  19. Public Sub New()
  20.  
  21. InitializeComponent()
  22.  
  23. 'Manually add the controls needed
  24.  
  25. Me.Text = "LogMyJog version 1"
  26.  
  27. Me.Size = New Size(800, 600)
  28.  
  29. Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
  30.  
  31. DateSelector = New MonthCalendar()
  32.  
  33. DateSelector.Location = New Point(0, 0)
  34.  
  35. Me.Controls.Add(DateSelector)
  36.  
  37. DateCombo = New ComboBox()
  38.  
  39. DateCombo.DropDownStyle = ComboBoxStyle.DropDownList
  40.  
  41. DateCombo.Location = New Point(0, 164)
  42.  
  43. DateCombo.Width = Me.ClientSize.Width
  44.  
  45. DateCombo.Size = New Size(180, 50)
  46.  
  47. Me.Controls.Add(DateCombo)
  48.  
  49. DateInfo = New TextBox()
  50.  
  51. DateInfo.Multiline = True
  52.  
  53. DateInfo.ScrollBars = ScrollBars.Vertical
  54.  
  55. DateInfo.Location = New Point(0, 188)
  56.  
  57. DateInfo.Size = New Size(500, 200)
  58.  
  59. Me.Controls.Add(DateInfo)
  60.  
  61. CurrentDate = New Date
  62.  
  63. DataDictionary = New Dictionary(Of String, String)
  64.  
  65. DataDocument = New Xml.XmlDocument()
  66.  
  67. 'This is where the data will be stored
  68.  
  69. FilePath = Application.StartupPath & "\dateinfo.xml"
  70.  
  71. If FileIO.FileSystem.FileExists(FilePath) Then
  72.  
  73. DataDocument.Load(Application.StartupPath & "\dateinfo.xml")
  74.  
  75. Dim Items As Xml.XmlNodeList
  76.  
  77. Dim Item As Xml.XmlNode
  78.  
  79. Dim ItemDate As Date
  80.  
  81. Items = DataDocument.DocumentElement.SelectNodes("//item")
  82.  
  83. For Each Item In Items
  84.  
  85. 'Add data into the DataDictionary and ComboBox
  86.  
  87. ItemDate = Date.Parse(Item.Attributes("date").Value)
  88.  
  89. DataDictionary.Add(ItemDate.Date.ToLongDateString(), Item.FirstChild.InnerText)
  90.  
  91. DateCombo.Items.Add(ItemDate.Date.ToLongDateString())
  92.  
  93. Next
  94.  
  95. End If
  96.  
  97. 'Add the event handlers for the three controls
  98.  
  99. AddHandler DateInfo.TextChanged, AddressOf Me.DateInfo_TextChanged
  100.  
  101. AddHandler DateCombo.SelectedIndexChanged, AddressOf Me.DateCombo_SelectedIndexChanged
  102.  
  103. AddHandler DateSelector.DateChanged, AddressOf Me.DateSelector_DateChanged
  104.  
  105. 'Initialize the controls using the CurrentDate
  106.  
  107. DateSelect()
  108.  
  109. End Sub
  110.  
  111.  
  112.  
  113. Private Sub DateSelect()
  114.  
  115. If DateSelector.SelectionStart.Date = DateSelector.SelectionEnd.Date Then
  116.  
  117. CurrentDate = DateSelector.SelectionStart.Date
  118.  
  119. If Not DateCombo.Items.Contains(CurrentDate.Date.ToLongDateString()) Then
  120.  
  121. DateCombo.Items.Add(CurrentDate.Date.ToLongDateString())
  122.  
  123. DataDictionary.Add(CurrentDate.Date.ToLongDateString(), "")
  124.  
  125. DateCombo.SelectedItem = CurrentDate.Date.ToLongDateString()
  126.  
  127. Else
  128.  
  129. DateCombo.SelectedItem = CurrentDate.Date.ToLongDateString()
  130.  
  131. If DataDictionary.ContainsKey(CurrentDate.Date.ToLongDateString()) Then
  132.  
  133. DateInfo.Text = DataDictionary.Item(CurrentDate.Date.ToLongDateString())
  134.  
  135. Else
  136.  
  137. DateInfo.Text = ""
  138.  
  139. End If
  140.  
  141. End If
  142.  
  143. End If
  144.  
  145. End Sub
  146.  
  147.  
  148.  
  149. Private Sub DateSelector_DateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs)
  150.  
  151. DateSelect()
  152.  
  153. End Sub
  154.  
  155.  
  156.  
  157. Private Sub DateCombo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
  158.  
  159. If DataDictionary.ContainsKey(DateCombo.Text) Then
  160.  
  161. DateSelector.SelectionStart = Date.Parse(DateCombo.Text)
  162.  
  163. DateSelector.SelectionEnd = Date.Parse(DateCombo.Text)
  164.  
  165. End If
  166.  
  167. End Sub
  168.  
  169.  
  170.  
  171. Private Sub DateInfo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
  172.  
  173. If DataDictionary.ContainsKey(CurrentDate.Date.ToLongDateString()) Then
  174.  
  175. DataDictionary.Item(CurrentDate.Date.ToLongDateString()) = DateInfo.Text
  176.  
  177. End If
  178.  
  179. End Sub
  180.  
  181.  
  182.  
  183. Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
  184.  
  185. 'Save the DataDictionary to the Xml Document...
  186.  
  187. DataDocument = New Xml.XmlDocument()
  188.  
  189. Dim Root As Xml.XmlElement
  190.  
  191. Dim Item As Xml.XmlElement
  192.  
  193. Dim Data As Xml.XmlCDataSection
  194.  
  195. Dim DateAttribute As Xml.XmlAttribute
  196.  
  197. Dim Key As String
  198.  
  199. Root = DataDocument.CreateElement("data")
  200.  
  201. For Each Key In DataDictionary.Keys
  202.  
  203. Item = DataDocument.CreateElement("item")
  204.  
  205. DateAttribute = DataDocument.CreateAttribute("date")
  206.  
  207. DateAttribute.Value = Key
  208.  
  209. Item.Attributes.Append(DateAttribute)
  210.  
  211. Data = DataDocument.CreateCDataSection(DataDictionary(Key))
  212.  
  213. Item.AppendChild(Data)
  214.  
  215. Root.AppendChild(Item)
  216.  
  217. Next
  218.  
  219. DataDocument.AppendChild(Root)
  220.  
  221. DataDocument.Save(FilePath)
  222.  
  223. End Sub
  224.  
  225. End Class
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC