I would like to make a program in which the user selects a time frame, for example if he selects from a DateTimePicker1 the time 7 (July), 17th, 2012 through DateTimePicker2 7 (July), 20th, 2012, and presses the button, a text will show up in a textbox from which he can make a copy of those days. The results will be shown as follows:
7/17/2012
7/18/2012
7/19/2012
7/20/2012

I also want to make a case in which if the user selects dates from: 7/17/2012 through 7/10/2012,
the program will throw out a message that the second date is earlier than the first one.

I'm not sure from where to begin, if somebody could help me in this situation, I would really appreciate your efforts.
THANKS.

Edited 3 Years Ago by Start4me: Added image for better visual understanding

Attachments Date_Generator.jpg 65.97 KB

The logic would be, figure out how many days the 2 dates are apart, check that the diff is greater than 0 for your last condition, and then create loop where you would add 1 day to the first date until the new date is equal to your second date or the loop run as many times as the difference between the 2 dates.

I dont know about text boxes but i managed to pull it off using a list view

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListView1.Columns.Add("Dates", 100, HorizontalAlignment.Center)
        ListView1.View = View.Details
        ListView1.GridLines = True
        ListView1.FullRowSelect = True
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim x As Date = DateTimePicker1.Value.Date
        Dim y As Date = DateTimePicker2.Value.Date
        Dim z As Date
        Dim a As Integer = 0
        Dim c As Integer = (DateTimePicker2.Value.Date - DateTimePicker1.Value.Date).TotalDays
        For b As Integer = 0 To c
            z = x.Date.AddDays(a)
            ListView1.Items.Add(z.ToString)
            a = a + 1
        Next
    End Sub
End Class

for a textbox this should work:

For b As Integer = 0 To c
    z = x.Date.AddDays(b)
    TextBox1.Text = String.Concat(TextBox1.Text, b, vbNewLine)
Next

Cool, i didnt know about the String.Concat() but are we not supposed to be adding the z value instead of b ?
I mean

TextBox1.Text = String.Concat(TextBox1.Text, z, vbNewLine)

The revised routine, to handle the To date being after the From date, might look like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim x As Date = DateTimePicker1.Value.Date
    Dim y As Date = DateTimePicker2.Value.Date
    Dim z As Date
    Dim c As Integer = (DateTimePicker2.Value.Date - DateTimePicker1.Value.Date).TotalDays
    If c >= 0 Then
        For b As Integer = 0 To c
            z = x.Date.AddDays(b)
            TextBox1.Text = String.Concat(TextBox1.Text, z, vbNewLine)
        Next
    Else
        MsgBox("The To date must be before the From date")
    End If  
End Sub

Or you could get fancy and allow for the dates begin backward and adjust:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As Date = DateTimePicker1.Value.Date
        Dim y As Date = DateTimePicker2.Value.Date
        Dim z As Date
        Dim c As Integer = (DateTimePicker2.Value.Date - DateTimePicker1.Value.Date).TotalDays
        TextBox1.Text = ""
        If c >= 0 Then
            For b As Integer = 0 To c
                z = x.Date.AddDays(b)
                TextBox1.Text = String.Concat(TextBox1.Text, z, vbNewLine)
            Next
        Else
            c = Math.Abs(c)
            For b = 0 to c
                z= y.Date.AddDays(b)
                TextBox1.Text = String.Concat(TextBox1.Text, z, vbNewLine)
            Next
        End If  
    End Sub

Edited 3 Years Ago by tinstaafl

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