Answered by:
DateTimePicker - Add day

Question
-
hi i wanted to create a simple code to add one day to the datetimepicker but it crushes whenn it should change the month...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim day As Integer = DateTimePicker1.Value.ToString("dd")
DateTimePicker1.Value = CStr(CInt(day) + 1) & "." & DateTimePicker1.Value.ToString("MM.yyyy")
End Sub
...i could check wheather the day is for example 31 to change the month but it depends on the month so .... i had to check it, too....would be a long code...
so my question: is there an easier way to do that?Wednesday, October 1, 2008 8:38 AM
Answers
-
"Dim day as Integer = Something.ToString
DTP.Value = Something.ToString"
You really need to turn Option Strict On and learn a bit about data types
Try:
DateTimePicker1.Value = DateTimePicker1.Value.AddDays(1)
Wednesday, October 1, 2008 10:13 AM
All replies
-
"Dim day as Integer = Something.ToString
DTP.Value = Something.ToString"
You really need to turn Option Strict On and learn a bit about data types
Try:
DateTimePicker1.Value = DateTimePicker1.Value.AddDays(1)
Wednesday, October 1, 2008 10:13 AM -
thxWednesday, October 1, 2008 11:18 AM
-
I want to do the same thing. However I run into a problem.
I have an app that adds records to a database based on a selected date (using datetimepicker). I also had a new associated record which contains values from the record being added that will have the NEXT days date as its key .
I noticed that when I use datetimepicker1.value=datetimepicker1.value.addDays(1) to set the key of the associated record it caused a value changed condition which causes my date to be used to as the key of the new main record to be increased to match the new date (next day date). How to I changed the date in datetimepicker WITHOUT causing the value changed condition?
Thursday, January 10, 2013 8:19 PM -
If you don't want to change the value in DateTimePicker1, don't change it, use a variable instead.
Dim nextDay As DateTime = DateTimePicker1.Value.AddDays(1)
Thursday, January 10, 2013 8:37 PM -
I did that and now I have TWO new problems.
Prob 1: I WANT mm/dd/yyyy. I get d/d/yyyy when I select a date between 1 and 9. I set the format to mm/dd/yyyy. How do I get 01/01/2013 instead of 1/1/2013? here's the code
Private Sub Ledgeradd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DateTimePicker1.Format = DateTimePickerFormat.Custom
DateTimePicker1.CustomFormat = "mm/dd/yyyy"
End Subprob 2. My app doesn't show the date time picker and automatically assumes I changed the value so that i don't get to pick a date. This is very strange.
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
selected_date = DateTimePicker1.Text - this get executed even though the form containing the datetimepicker hasn't been shown and I haven't selected a date. Selected_date contains "". Of course the routine - load_register_drawer fails.
db_process = 0
load_register_drawer()
Select Case db_process
Case Is = 1
Me.Close()
ledgermain.Show()Friday, January 11, 2013 6:42 PM -
I'm not sure if I understand your post. For your first problem, there is an error in your custom format string, you need MM for the month (mm is minutes). So change the code to
DateTimePicker1.Format = DateTimePickerFormat.Custom DateTimePicker1.CustomFormat = "MM/dd/yyyy"
I really don't understand your second problem. Could you try explaining it in more detail. If the ValueChanged event is being fired, something must be changing the value in the DateTimePicker (it doesn't have to be the user, it could be your code - perhaps the Form's Load event).
I don't know what type of variable selected_date is, but it sounds as though it is supposed to be a DateTime variable. If that is the case, you should be using the Value property of DateTimePicker1 to set it, not the Text property.
selected_date = DateTimePicker1.Value
Friday, January 11, 2013 7:04 PM -
Made the changes but it STILL doesn't return MM/dd/yyyy. It returns M/dd/yyyy.
As for the second problem. the form is a loaded form loaded by a main form. here a quick overview,
I have an app that contains multiple forms, add, update and delete. Everything is driven from a main form. The form I am having problems with is the 'add' form which contains the date time picker.
What I expect is that the when the add form is loaded (with the datetimepicker showing), I select the date I want and then processing goes forward from there. What I'm getting is I select the add from the main form, the form is loaded but not shown. Instead it uses the date currently set and begins processing immediately.
Friday, January 11, 2013 8:35 PM -
I can't reproduce your first problem, with the custom format set to "MM/dd/yy", the text shown in the DateTimePicker1 control, and the string returned by DateTimePicker1.Text has a two digit month. Where are you seeing the text with a one digit month?
In your second problem, you say you show the Add Form (which contains DateTimePicker1) and that the DateTimePicker1.ValueChanged event is being fired after you cal the form's Show method but before the form appears on the screen. That suggests that the value of DateTimePicker1 is having its value set in your code (for example in the Load event of the Add Form).
Friday, January 11, 2013 10:41 PM -
On the first problem. When I ADD 1 to the datetimepicker it show M/d/yyyy. The first time is fine.
I haven't been have to fix the second problem so I removed the datetimepicker altogether and added it anew to the form. Fix that problem. Still don't know why it did that.
In any case, I found the BEST work around. I was trying to use a 10 digit date as my record key. I had the key field set as nchar(10). I changed the data base tables to vnchar(10). That way I could use M/d/yyyy date for dates for those dates less than 10. Works fine. don't know I didn't think of that from the start.
Saturday, January 12, 2013 1:34 AM