Add days to a DT variable based on NumericUpDown value

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
I'm using a NumericUpDown control and I have a DateTimePicker control that holds a date

what I need done is when the NumericUpDown control's value changes I need to show a date in a label that is # days higher than the date in the DateTimePicker

I'm using the NumericUpDown's ValueChanged event which does fire everytime the value changes, except this only works the first time the value changes, the code executes but it does not produce a new date in the label after the first value change.
Here's the code I have so far:
VB.NET:
    'dtpDateOut is a DateTimePicker
    'nudDaysBack is a NumericUpDown
    'lblNewDate is a Label

    Private Sub nudDaysBack_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudDaysBack.ValueChanged
       Dim newDT As DateTime = dtpDateOut.Value
       newDT.AddDays(nudDaysBack.Value)
       lblNewDate.Text = newDT.ToShortDateString
    End Sub
 
Never mind, I neglected to see that newDT.AddDays(##) is a function, not a method. The code is:
VB.NET:
            Dim newDT As DateTime = dtpDateOut.Value
            lblNewDate.Text = newDT.AddDays(nudDaysBack.Value).ToShortDateString
 
You have to catch the return value of the AddDays function:
VB.NET:
[B][COLOR="DarkGreen"]newDT [/COLOR][/B]= newDT.AddDays(nudDaysBack.Value)
 
or even shorter:
VB.NET:
lblNewDate.Text = dtpDateOut.Value.AddDays(nudDaysBack.Value).ToShortDateString
 
Back
Top