Results 1 to 12 of 12

Thread: [RESOLVED] Datetimepicker Calculation

  1. #1

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Resolved [RESOLVED] Datetimepicker Calculation

    HI...

    I've got a form with following controls:
    Two Datetimepickers
    Two Buttons
    One TextBox
    One ComboBox

    Name:  DateTimePicker.jpg
Views: 3194
Size:  60.2 KB

    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



    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 ...

  2. #2
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    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
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


  3. #3

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: Datetimepicker Calculation

    Quote Originally Posted by Zeljko View Post
    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

    The codes much better for the calculation now after replacing text with value ...

    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...

    Best regards...

  4. #4
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: Datetimepicker Calculation

    Quote Originally Posted by HOTFIX View Post
    THANKS Zeljko
    N.P.
    Quote Originally Posted by HOTFIX View Post
    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...
    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. '1 - It's easyer to change TextBox to Numeric UpDown control
    2. '2 - Numeric UpDown can limit Min and max number :)
    3. '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 )!
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


  5. #5

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    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

    Name:  dtp001.jpg
Views: 2056
Size:  29.1 KB

    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

    Name:  dtp002.jpg
Views: 2034
Size:  78.8 KB

    Other case I can add 7790 years if the current year set to 2008

    Name:  dtp003.jpg
Views: 1975
Size:  25.2 KB



    So the validation dynamically change based on first Datetimepicker


    I know it's complicated a little bit...


    REGARDS...

  6. #6
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Somewhere else today
    Posts
    355

    Re: Datetimepicker Calculation

    The maximum year you can have is 9998. Thus the year is outside the max & min allowed.
    It was much easier in VB6, but I am now liking Vb.Net alot more.

  7. #7
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    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)
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


  8. #8

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: Datetimepicker Calculation

    Quote Originally Posted by computerman View Post
    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...

  9. #9

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: Datetimepicker Calculation

    Quote Originally Posted by Zeljko View Post
    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...

    Any idea PLZ ...

  10. #10
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    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:
    1. Friend Sub txtboxQs_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtboxQs.Leave
    2.         '...
    3.         'limit beetwen 0.05 and 0.1
    4.         If CDbl(txtboxQs.Text) < 0.05 Or CDbl(txtboxQs.Text) > 0.1 Then
    5.             'so if its "out of range" set it to zero or you can call msgbox or something else
    6.             txtboxQs.Text = 0.0
    7.             MessageBox.Show("Out of Range (<0.05 or > 0.1)", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, 0)
    8.         End If
    9.         '...
    10. End Sub
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


  11. #11

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: Datetimepicker Calculation

    Thanks Zeljko...

    I appreciate your efforts for helping...


    Best regards...

  12. #12
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: Datetimepicker Calculation

    Np.
    With your way of learning and trying it was such an easy way to guide you.
    Good luck with programing...
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width