Member Avatar for Micheal87

Hi, how can I make sure that all the datetime stored previously by user input will be updated in a cycle for?
I explain better the structure
Combobox2 <----- is where list of datatime are previously stored
Combobox3 <------- is where remaining time is given by a substraction of two dates, stored in days remaing.
The fact, is now that these values are only stored and doesn't change with time passing by.
This is my code

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

        For i = 0 To ListBox1.Items.Count
            Dim d As DateTime = DateTime.Now
            Dim d2 As DateTime = ComboBox2.SelectedItem(i)
            Dim dysdiff As String = (d - d2).Days.ToString()
            ComboBox3.SelectedItem(i) = 365 - dysdiff & " Days"
        Next
    End Sub

You may add a timer and in Timer.tickevent call to Button5_Click(sender, e).
BTW, number 365 in line#7 is numeric and dysdiffis a string, so it's not possible the substraction to work.

Member Avatar for Micheal87

I changed the code, but still doesn't work, I changed also the 3rd line to Convert.todatetime because the program was giving to me an error of chart to date conversion.

 For i = 0 To ComboBox2.Items.Count
            Dim d As DateTime = DateTime.Now
            Dim d2 As DateTime = Convert.ToDateTime(ComboBox2.SelectedItem)
            Dim dysdiff As Integer = (d - d2).Days
            ComboBox3.SelectedItem(i) = 365 - dysdiff & " Days"
        Next
Member Avatar for Micheal87

Changed again this part of code using dateparse.exact now the problem is that the date time is the same from the combobox but it's saying that's not a validdate. I also put an image of the debug value, there's something in the d2 that seem strange, is because parsedate doesn't have the AM?
Code

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        For i = 0 To ComboBox2.Items.Count
            Dim parsedate As String = ComboBox2.SelectedItem.ToString
            Dim d As DateTime = DateTime.Now
            Dim d2 As DateTime = DateTime.ParseExact(parsedate, "dd/MM/yyyy hh:mm:ss", System.Globalization.CultureInfo.CurrentCulture)
            Dim dysdiff As Integer = (d - d2).Days
            ComboBox3.SelectedItem(i) = 365 - dysdiff & " Days"
        Next
    End Sub

Debug image

devenv_5sMnXEi97A.png

Member Avatar for Micheal87

Code fixed for the date time string not valid (moved parsedate just below the datetime.now)
The problem now it's I can't complete the cycle, the error now it's on Combobox3.selecteditem(i) telling me that char type is readonly.

  Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Dim d As DateTime = DateTime.Now
        Dim parsedate As String = ComboBox2.SelectedItem.ToString
        Dim d2 As DateTime = DateTime.ParseExact(parsedate.ToString, "dd/MM/yyyy hh:mm:ss", System.Globalization.CultureInfo.CurrentCulture)
        Dim dysdiff As Integer = (d - d2).Days
        ComboBox3.SelectedItem(i) = 365 - dysdiff & " Days"
    Next
End Sub

I am not sure what you are trying to do. At a guess, you may replace line #6 by:

        ComboBox3.Items(i) = (365 - dysdiff).ToString + " Days"
        ComboBox3.SelectedIndex = i
Member Avatar for Micheal87

Thanks a lot, and sorry I forgot to include the cycle for, I was trying to do, a auto-refreshing of all stored datetimes (with old timestamp and remaing days) with new one, all this with datetime.now to make the difference. Also I appreciate your first post for doing this with a timer tick, I can try to put 24h in millisecond for see value that will auto-change. This is the last updated code

 For i = 0 To ComboBox3.Items.Count -1    //the -1 is for next loop by all combobox stored items (in this case remaining time) 
            Dim d As DateTime = DateTime.Now
            Dim parsedate As String = ComboBox2.SelectedItem.ToString //this is where old datetime.now is stored
            Dim d2 As DateTime = DateTime.ParseExact(parsedate.ToString, "dd/MM/yyyy hh:mm:ss", System.Globalization.CultureInfo.CurrentCulture) //now this string it's giving me an error to datatime not valid
            Dim dysdiff As Integer = (d - d2).Days //this one is to make the subtraction between old timestamp and new, the old one are stored in combobox2, both will have always same number of items
            ComboBox3.Items(i) = (365 - dysdiff).ToString + " Days" //this one is for substitute the old value of remaining time to new one, always in days format
            ComboBox3.SelectedIndex = i
        Next

In final thanks a lot for helping me.

You are welcome.
Look at the contents of parsedate, most probably it is formated as "MM/dd/yyyy hh:mm:ss" and not as "dd/MM/yyyy hh:mm:ss" as expected.

Member Avatar for Micheal87

devenv_bfsF2AjA2C.png

I checked trough the debug, seem also that d2 is a bit strange of a date, maybe it's the format not going by all the process.

Time format is "HH:mm:ss" and not "hh:mm:ss" so I think you should change instruction:

 Dim d2 As DateTime = DateTime.ParseExact(parsedate.ToString, "dd/MM/yyyy hh:mm:ss", System.Globalization.CultureInfo.CurrentCulture)

into:

 Dim d2 As DateTime = DateTime.ParseExact(parsedate.ToString, "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.