Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I have a little issues, while auto-refresh all values (remaing time), in summary the program pick the datatime stored in a combobox then subtract it with current time ( and return the value (remaining time) stored in another one combobox. The issues is that when I click the refresh button only the last item on the remaining time combobox will change. I need that it will change from the first and so on till the last, using the previously stored datatimes in the combobox.

What I have tried:

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        For i = 0 To ComboBox3.Items.Count - 1
            ComboBox3.SelectedIndex = i
            Dim d As DateTime = DateTime.Now
            Dim parsedate As DateTime = ComboBox2.SelectedItem.ToString
            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.Items(i) = (365 - dysdiff).ToString + " Days"

    End Sub
Updated 16-Jan-22 14:37pm

1 solution


ComboBox3.SelectedIndex = i
is not doing anything useful.

Dim parsedate As DateTime = ComboBox2.SelectedItem.ToString
will give the same result on each iteration of your loop, and all items in ComboBox3 will end up showing the same number of days, which depends on which ComboBox2 item was selected when you clicked the button.

So my guess is you wanted the former to be
ComboBox2.SelectedIndex = i
That would result in varying strings in ComboBox3.

But then I'm puzzled why you are using SelectedIndex and SelectedItem at all. A simple
Dim parsedate As DateTime = ComboBox2.Items[i].ToString
should suffice.

Share this answer
MXwell8 17-Jan-22 14:10pm    
Hi, thank you for the answer, it's possible to do this on a form laod without selection an item from combobox? Like only a refresh of all the values?
Also when I put
Dim parsedate As DateTime = ComboBox2.Items[i].ToString
it's say to me that it's not possible to convert combobox object collection in date. It's there a difference between [i] and (i) ?
Luc Pattyn 17-Jan-22 14:25pm    
(1) I copied one line of yours without noticing it contained a bug (wrong type!); should be
Dim parsedate As String = ComboBox2.SelectedItem.ToString
(2) yes VB uses parentheses, my mistake, I'm used to C# and brackets, so
Dim parsedate As String = ComboBox2.Items(i).ToString
MXwell8 17-Jan-22 15:51pm    
Thanks again for the help :)
Luc Pattyn 17-Jan-22 15:58pm    
you're welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900