After select a date, from a Date Time Picker How can I add three full months To a Textbox

Gary Simpson 471 Reputation points
2021-01-04T19:28:30.503+00:00

Hi Good People,

On My Form I have Two textboxes and one Date Time Picker
When I select a date from the Date Time Picker The result gets put into textbox1.

Is what I require now is the Second textbox (textbox2) to show exactly three full months ahead of the date selected in the date Time Picker

Example1: If I select a date (01/11/2020) I would Like Textbox2 To Show the Date (31/01/2021) end of the third month

Example2: if I select a date (01/12/2020) I would Like Textbox2 To Show the Date (28/02/2021) end of the third month

Kind Regards
Gary

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,659 questions
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-01-04T19:47:45.32+00:00

    Hi

    OK I see your requirements now. Just needed 1 day subtracted to fit your pattern.

    ' FORM1 with DateTimePicker1, 
    ' and 2 Labels (1,2)
    Option Strict On
    Option Explicit On
    Public Class Form1
     Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
     Label1.Text = DateTimePicker1.Value.ToString("ddd, dd MMM yyyy")
     Label2.Text = DateTimePicker1.Value.AddMonths(3).AddDays(-1).ToString("ddd, dd MMM yyyy")
     End Sub
    End Class
    
    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,386 Reputation points
    2021-01-04T20:42:20.34+00:00

    Hello @Gary Simpson

    Here is another way via data binding.

    • Add DateTimePicker DateTimePicker1
    • Add TextBox ThreeMonthsTextBox

    53422-1111.png

    Public Class Form1  
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown  
            Dim binding = New Binding("Value", DateTimePicker1, "Value")  
            AddHandler binding.Format, AddressOf FormatDate  
            DateTimePicker1.DataBindings.Add(binding)  
        End Sub  
      
        Private Sub FormatDate(sender As Object, e As ConvertEventArgs)  
            ThreeMonthsTextBox.Text = $"{DateTimePicker1.Value.AddMonths(3).ToShortDateString()}"  
        End Sub  
    End Class  
    
    1 person found this answer helpful.

  2. Anonymous
    2021-01-04T21:39:54.673+00:00

    Hi
    Slightly different code. Choose any date from DataTimePicker1 and Label1 will show first day of month and Label2 will show last day of 3rd month.

    ' FORM1 with DateTimePicker1, 
    ' and 2 Labels (1,2)
    Option Strict On
    Option Explicit On
    Public Class Form1
     Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
     Dim d As DateTime = DateTimePicker1.Value
     Dim nd As New Date(d.Year, d.Month, 1)
    
     Label1.Text = nd.ToString("ddd, dd MMM yyyy")
     Label2.Text = nd.AddMonths(3).AddDays(-1).ToString("ddd, dd MMM yyyy")
     End Sub
    End Class
    
    1 person found this answer helpful.
    0 comments No comments

  3. Anonymous
    2021-01-04T19:30:26.077+00:00

    Hi The problem is - how long is a month? Do you mean 3 calander months - eg June maps to September? Or something else? ![53344-1.png][1] ' FORM1 with DateTimePicker1, ' and 2 Labels (1,2) Option Strict On Option Explicit On Public Class Form1 Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged Label1.Text = DateTimePicker1.Value.ToString("ddd, dd MMM yyyy") Label2.Text = DateTimePicker1.Value.AddMonths(3).ToString("ddd, dd MMM yyyy") End Sub End Class [1]: /api/attachments/53344-1.png?platform=QnA