1 Attachment(s)
[RESOLVED] Datetimepicker Calculation
HI...:)
I've got a form with following controls:
Two Datetimepickers
Two Buttons
One TextBox
One ComboBox
Attachment 72891
The idea is adding and subtracting dates using Datetimepicker contols...
I found codes for doing Dates Calculation but is'nt quit correct;for example:
if i want to add 21years from today the result will be:
sat 30 Aug 1930 :eek2:
The Adding Codes on comand button:
Code:
Private Sub BTNadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNadd.Click
Dim Date1, NewDate As DateTime
Date1 = DateTimePicker1.Text
If ComboBox1.SelectedItem = "DAYS" Then
NewDate = Date1.AddDays(TextBox1.Text)
DateTimePicker2.Text = NewDate
ElseIf ComboBox1.SelectedItem = "WEEKS" Then
NewDate = Date1.AddDays((TextBox1.Text) * 7)
DateTimePicker2.Text = NewDate
ElseIf ComboBox1.SelectedItem = "MONTHS" Then
NewDate = Date1.AddMonths(TextBox1.Text)
DateTimePicker2.Text = NewDate
ElseIf ComboBox1.SelectedItem = "YEARS" Then
NewDate = Date1.AddYears(TextBox1.Text)
DateTimePicker2.Text = NewDate
End If
End Sub
The Subtracting Codes on comand button:
Code:
Private Sub BTNsub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNsub.Click
Dim Date1, NewDate As DateTime
Date1 = DateTimePicker1.Text
If ComboBox1.SelectedItem = "DAYS" Then
NewDate = Date1.AddDays(-TextBox1.Text)
DateTimePicker2.Text = NewDate
ElseIf ComboBox1.SelectedItem = "WEEKS" Then
NewDate = Date1.AddDays(-(TextBox1.Text) * 7)
DateTimePicker2.Text = NewDate
ElseIf ComboBox1.SelectedItem = "MONTHS" Then
NewDate = Date1.AddMonths(-TextBox1.Text)
DateTimePicker2.Text = NewDate
ElseIf ComboBox1.SelectedItem = "YEARS" Then
NewDate = Date1.AddYears(-TextBox1.Text)
DateTimePicker2.Text = NewDate
End If
End Sub
Anybody help me to correct the code PLZ ...:(
Re: Datetimepicker Calculation
I think that you must calculate with Value part of DateTimePickerControl and not Text while Text will give you string representation and with strings you can not add or substract numbers.
Try to change "DateTimePicker2.Text" => "DateTimePicker2.Value"... etc
EDIT: And Date1 is also VALUE and not TEXT
Re: Datetimepicker Calculation
Quote:
Originally Posted by
Zeljko
I think that you must calculate with Value part of DateTimePickerControl and not Text while Text will give you string representation and with strings you can not add or substract numbers.
Try to change "DateTimePicker2.Text" => "DateTimePicker2.Value"... etc
EDIT: And Date1 is also VALUE and not TEXT
THANKS Zeljko :wave:
The codes much better for the calculation now after replacing text with value ...:thumb:
Do you have any idea how to validate the input selection(for case adding & Subtracting) in the textbox so the Calculation will be in correct range of min and max date of the Datetimepicker control dynamically...:confused:
Best regards...:)
Re: Datetimepicker Calculation
Quote:
Originally Posted by
HOTFIX
THANKS Zeljko :wave:
N.P.
Quote:
Originally Posted by
HOTFIX
Do you have any idea how to validate the input selection(for case adding & Subtracting) in the textbox so the Calculation will be in correct range of min and max date of the Datetimepicker control dynamically...:confused:
If I understand you corectly:
You wannt to limit Textbox number so that the User can not acidentaly substract 25 days from sunday 14 august 2008 (if is 25 in TextBox, days => 25-14=-11 days)?
You can do that with:
VB.Net Code:
'1 - It's easyer to change TextBox to Numeric UpDown control
'2 - Numeric UpDown can limit Min and max number :)
'3 - Use OnChange event of Numeric UpDown control to limit min max number inside control...
But if you do not want that, and you want to substract 25 from 14 and go to previous month then it's a bit more math included. huh. You must first calculate Days and then add extra to Months. Then calculete Months and add extra to Years and so on and on.
But be carefull: Each Month is not equal in number of Days so I strongly sugest for easynes that you take 1 Month = 30 Days (offcourse, if you can do that with posible little error ;) )!
3 Attachment(s)
Re: Datetimepicker Calculation
Sorry if my query wasn't clear...
Datetimepicker control is set to:
Max Date : 31/12/9998
Min Date :1/1/1753
For example if I add 7989 years to today date the result will be dd/mm/9998
Attachment 72896
But if I add 7990 years to today date error will show the input selection is not within range of min and max date of D.T.P
Attachment 72897
Other case I can add 7790 years if the current year set to 2008
Attachment 72898
So the validation dynamically change based on first Datetimepicker
I know it's complicated a little bit...:eek:
REGARDS...:)
Re: Datetimepicker Calculation
The maximum year you can have is 9998. Thus the year is outside the max & min allowed.
Re: Datetimepicker Calculation
I'm confused,
Try On TExtBox Change event:
If TExtBox is > 7989 then
TExtBox.Text = 7989
'and maybe some kind of MsgBox to inform user
...
But I will still replace TextBox with Numeric control (while then its limited to only recive numbers)
Re: Datetimepicker Calculation
Quote:
Originally Posted by
computerman
The maximum year you can have is 9998. Thus the year is outside the max & min allowed.
What I’m looking for is validation code to inform the user when the numeric value in the textbox (for all cases days, weeks...etc) is outside of the max & min Calculation allowed by warning message…
Regards...:)
Re: Datetimepicker Calculation
Quote:
Originally Posted by
Zeljko
I'm confused,
Try On TExtBox Change event:
If TExtBox is > 7989 then
TExtBox.Text = 7989
'and maybe some kind of MsgBox to inform user
...
But I will still replace TextBox with Numeric control (while then its limited to only recive numbers)
Thanks Zeljko for your support... :)
I've got the code to allow user limiting the input of the textbox to receive numbers only by using keypress event:
Code:
Private Sub TBxValue_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TBxValue.KeyPress
If (Char.IsControl(e.KeyChar) = False) Then
If (Char.IsDigit(e.KeyChar)) Then
'do nothing
Else
e.Handled = True
MsgBox("Sorry Only Digits Allowed!!")
TBxValue.Focus()
End If
End If
End Sub
Unfortunately I'm looking for validation code to inform the user when the numeric value in the textbox is out of range Datetimepicker MIN and MAX date Calculation... :confused:
Any idea PLZ ...:o
Re: Datetimepicker Calculation
This is from one of my progies for TextBox but it can easily be applied to NumericUpDown if you wanna:
VB:Net Code:
Friend Sub txtboxQs_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtboxQs.Leave
'...
'limit beetwen 0.05 and 0.1
If CDbl(txtboxQs.Text) < 0.05 Or CDbl(txtboxQs.Text) > 0.1 Then
'so if its "out of range" set it to zero or you can call msgbox or something else
txtboxQs.Text = 0.0
MessageBox.Show("Out of Range (<0.05 or > 0.1)", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, 0)
End If
'...
End Sub
Re: Datetimepicker Calculation
Thanks Zeljko... :wave:
I appreciate your efforts for helping...:)
Best regards... :)
Re: Datetimepicker Calculation
Np.
With your way of learning and trying it was such an easy way to guide you.
Good luck with programing...