Member Avatar for Member #1201803

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

Dani AI

Generated

Quick summary: wants the list of stored datetimes (ComboBox2) to auto-update so ComboBox3 shows the current "days remaining." The thread shows three common mistakes: iterating with the wrong index bounds or using SelectedItem instead of Items(i), parsing with the wrong format (hh vs HH), and trying to assign into SelectedItem (read‑only). correctly suggested a Timer and pointed out the hh/HH issue — use HH for 24‑hour timestamps or include AM/PM when using hh.

Practical, robust approach:

  • Store real DateTime objects in ComboBox2.Items instead of strings. If you keep strings, parse them with DateTime.TryParseExact (multiple formats) and CultureInfo to avoid brittle parsing.
  • Put the update logic in one method (e.g. UpdateRemainingDays) and call that from your Button click and your Timer.Tick.
  • Iterate 0 To ComboBox2.Items.Count - 1 and use ComboBox2.Items(i) to read the stored value; write back with ComboBox3.Items(i) = ... (or rebuild ComboBox3 each run).

Example pattern (VB.NET):

' store DateTime objects when the user adds dates
ComboBox2.Items.Add(someDateTimeValue)

' update method
Private Sub UpdateRemainingDays()
    For i As Integer = 0 To ComboBox2.Items.Count - 1
        Dim target As DateTime = CType(ComboBox2.Items(i), DateTime)
        Dim daysLeft As Integer = CInt(Math.Max(0, Math.Ceiling((target - DateTime.Now).TotalDays)))
        ComboBox3.Items(i) = daysLeft.ToString() & " Days"
    Next
End Sub

Troubleshooting tips and cautions:

  • If items are strings, use DateTime.TryParseExact with an array of acceptable formats and CultureInfo.InvariantCulture (or CurrentCulture) so parsing won't fail silently.
  • Do not change SelectedIndex inside the loop; set it only if you want a specific selection after the update.
  • Use a UI timer (System.Windows.Forms.Timer) if updates must touch controls directly; for background timers (System.Timers or Threading.Timer) marshal to the UI thread with Invoke.
  • If you need daily accuracy, consider running the update hourly or at midnight rather than relying on one large millisecond interval.

Following this pattern removes parsing fragility, avoids the SelectedItem vs Items confusion, and makes the auto‑refresh reliable.

Recommended Answers

All 9 Replies

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 Member #1201803

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 Member #1201803

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 Member #1201803

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 Member #1201803

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 Member #1201803

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.