Reputation: 984
I have 1 textbox and 1 datetimepicker. The textbox is to count how many days that I want to add.
Then when I'm lostfocus from the textbox, the datetimepicker will add the new value which is plus from the textbox.
This is how I'm doing but I've got nothing:
Dim waktu As Integer = pengerjaan_tb.Text
Dim datex As DateTime = DateTime.ParseExact(
pengerjaan_dtp.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture)
datex.AddDays(5)
MsgBox(datex)
pengerjaan_dtp.Value = datex.ToString("MM/dd/yyyy")
I show it in msgbox but the date is not renew with the new value. How could this be happening? What have I done wrong?
Upvotes: 0
Views: 6798
Reputation: 82474
A Couple of things:
DateTimePicker
has a Value
property that is of type DateTime
. You are using this property to set a value to the DateTimePicker
, but you don't use it to get the value from the DateTimePicker
.
DateTime
is Immutable. This means that AddDays
does not change the value of the current instance of the DateTime
struct, but returning a new instance of DateTime
instead.
Try this instead:
Dim waktu As Integer = Integer.Parse(pengerjaan_tb.Text)
pengerjaan_dtp.Value = pengerjaan_dtp.Value.AddDays(waktu)
Upvotes: 4
Reputation: 38865
First, turn on Option Strict
:
Dim waktu As Integer = pengerjaan_tb.Text
This is implicitly converting Text to integer. Option Strict converts potential runtime exceptions into compiler errors.
There is no need to parse the contents of a DateTimePicker
, the value property will be a DateTime
type. Then, DateTime
types are immutable - the AddDays
method returns a new DateTime
with the new value:
Dim datex As DateTime = pengerjaan_dtp.Value
pengerjaan_dtp.Value = datex.AddDays(5)
Upvotes: 3