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?

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?

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

...and then enter text into the textbox based on that date.

I'm not clear what you mean by this, can you elaborate?

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;

Public Class Form1

 

    Dim DataDictionary As Dictionary(Of String, String)

    Dim CurrentDate As Date

     

    Private Sub MonthCalendar1_DateSelected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateSelected

        DateSelect()

    End Sub

     

    Public Sub New()

        InitializeComponent()

        CurrentDate = New Date

        DataDictionary = New Dictionary(Of String, String)

        DateSelect()

    End Sub

     

    Private Sub DateSelect()

        If MonthCalendar1.SelectionStart.Date = MonthCalendar1.SelectionEnd.Date Then

            CurrentDate = MonthCalendar1.SelectionStart.Date

            If Not ComboBox1.Items.Contains(CurrentDate.Date.ToLongDateString()) Then

                ComboBox1.Items.Add(CurrentDate.Date.ToLongDateString())

                DataDictionary.Add(CurrentDate.Date.ToLongDateString(), "")

                ComboBox1.SelectedItem = CurrentDate.Date.ToLongDateString() 

            Else

                ComboBox1.SelectedItem = CurrentDate.Date.ToLongDateString()

                If DataDictionary.ContainsKey(CurrentDate.Date.ToLongDateString()) Then

                    TextBox1.Text = DataDictionary.Item(CurrentDate.Date.ToLongDateString())

                Else

                    TextBox1.Text = ""

                End If

            End If

        End If

    End Sub

     

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        If DataDictionary.ContainsKey(CurrentDate.Date.ToLongDateString()) Then

            TextBox1.Text = DataDictionary.Item(CurrentDate.Date.ToLongDateString())

        End If

    End Sub

     

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        If DataDictionary.ContainsKey(CurrentDate.Date.ToLongDateString()) Then

            DataDictionary.Item(CurrentDate.Date.ToLongDateString()) = TextBox1.Text

        End If

    End Sub

     

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

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.

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

<?xml version="1.0"?>

<dates>

<entry date="...">...</entry>

<entry date="...">...</entry>

</dates>

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

<?xml version="1.0"?>

<dates>

<entry date="...">...</entry>

<entry date="...">...</entry>

</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.

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?

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

Sir! Andrew, why if i used mysql or sql database what is the codes for that?

And also Sir! can ask a question, how to create a .exe program that automatically run like a start up programs and what is the codes?

example sir: i have a sample program that needs to run automatically!

Help Sir Please!

This article has been dead for over six months. Start a new discussion instead.