Hi
I have produced the following code which allows me to save textbox input to an XML file based on date selected. Copy the code into a blank Windows Application if you want to see it in action.
What I want to do is add another textbox and save that to a separate XML File.
Can you please help me with this?
I have produced the following code which allows me to save textbox input to an XML file based on date selected. Copy the code into a blank Windows Application if you want to see it in action.
What I want to do is add another textbox and save that to a separate XML File.
Can you please help me with this?
VB.NET:
Public Class Form1
Dim DataDictionary As Dictionary(Of String, String)
Dim DataDocument As Xml.XmlDocument
Dim FilePath As String
Dim CurrentDate As Date
Friend WithEvents DateSelector As System.Windows.Forms.MonthCalendar
Friend WithEvents DateCombo As System.Windows.Forms.ComboBox
Friend WithEvents DateInfo As System.Windows.Forms.TextBox
Public Sub New()
InitializeComponent()
'Manually add the controls needed
Me.Text = "LogMyJog version 1"
Me.Size = New Size(800, 600)
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
DateSelector = New MonthCalendar()
DateSelector.Location = New Point(0, 0)
Me.Controls.Add(DateSelector)
DateCombo = New ComboBox()
DateCombo.DropDownStyle = ComboBoxStyle.DropDownList
DateCombo.Location = New Point(0, 164)
DateCombo.Width = Me.ClientSize.Width
DateCombo.Size = New Size(180, 50)
Me.Controls.Add(DateCombo)
DateInfo = New TextBox()
DateInfo.Multiline = True
DateInfo.ScrollBars = ScrollBars.Vertical
DateInfo.Location = New Point(0, 188)
DateInfo.Size = New Size(500, 200)
Me.Controls.Add(DateInfo)
CurrentDate = New Date
DataDictionary = New Dictionary(Of String, String)
DataDocument = New Xml.XmlDocument()
'This is where the data will be stored
FilePath = Application.StartupPath & "\dateinfo.xml"
If FileIO.FileSystem.FileExists(FilePath) Then
DataDocument.Load(Application.StartupPath & "\dateinfo.xml")
Dim Items As Xml.XmlNodeList
Dim Item As Xml.XmlNode
Dim ItemDate As Date
Items = DataDocument.DocumentElement.SelectNodes("//item")
For Each Item In Items
'Add data into the DataDictionary and ComboBox
ItemDate = Date.Parse(Item.Attributes("date").Value)
DataDictionary.Add(ItemDate.Date.ToLongDateString(), Item.FirstChild.InnerText)
DateCombo.Items.Add(ItemDate.Date.ToLongDateString())
Next
End If
'Add the event handlers for the three controls
AddHandler DateInfo.TextChanged, AddressOf Me.DateInfo_TextChanged
AddHandler DateCombo.SelectedIndexChanged, AddressOf Me.DateCombo_SelectedIndexChanged
AddHandler DateSelector.DateChanged, AddressOf Me.DateSelector_DateChanged
'Initialize the controls using the CurrentDate
DateSelect()
End Sub
Private Sub DateSelect()
If DateSelector.SelectionStart.Date = DateSelector.SelectionEnd.Date Then
CurrentDate = DateSelector.SelectionStart.Date
If Not DateCombo.Items.Contains(CurrentDate.Date.ToLongDateString()) Then
DateCombo.Items.Add(CurrentDate.Date.ToLongDateString())
DataDictionary.Add(CurrentDate.Date.ToLongDateString(), "")
DateCombo.SelectedItem = CurrentDate.Date.ToLongDateString()
Else
DateCombo.SelectedItem = CurrentDate.Date.ToLongDateString()
If DataDictionary.ContainsKey(CurrentDate.Date.ToLongDateString()) Then
DateInfo.Text = DataDictionary.Item(CurrentDate.Date.ToLongDateString())
Else
DateInfo.Text = ""
End If
End If
End If
End Sub
Private Sub DateSelector_DateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs)
DateSelect()
End Sub
Private Sub DateCombo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
If DataDictionary.ContainsKey(DateCombo.Text) Then
DateSelector.SelectionStart = Date.Parse(DateCombo.Text)
DateSelector.SelectionEnd = Date.Parse(DateCombo.Text)
End If
End Sub
Private Sub DateInfo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
If DataDictionary.ContainsKey(CurrentDate.Date.ToLongDateString()) Then
DataDictionary.Item(CurrentDate.Date.ToLongDateString()) = DateInfo.Text
End If
End Sub
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
'Save the DataDictionary to the Xml Document...
DataDocument = New Xml.XmlDocument()
Dim Root As Xml.XmlElement
Dim Item As Xml.XmlElement
Dim Data As Xml.XmlCDataSection
Dim DateAttribute As Xml.XmlAttribute
Dim Key As String
Root = DataDocument.CreateElement("data")
For Each Key In DataDictionary.Keys
Item = DataDocument.CreateElement("item")
DateAttribute = DataDocument.CreateAttribute("date")
DateAttribute.Value = Key
Item.Attributes.Append(DateAttribute)
Data = DataDocument.CreateCDataSection(DataDictionary(Key))
Item.AppendChild(Data)
Root.AppendChild(Item)
Next
DataDocument.AppendChild(Root)
DataDocument.Save(FilePath)
End Sub
End Class