Results 1 to 13 of 13

Thread: [RESOLVED] please help project about due!

  1. #1

    Thread Starter
    Junior Member Phantom Coder's Avatar
    Join Date
    Aug 2006
    Posts
    24

    Resolved [RESOLVED] please help project about due!

    Hello everyone

    If you can help me you can save my grade.

    Im new to visual basic, I'm using visual basic.net 2005 standard edition for a school project.

    QUESTION 1
    I just need to know how to get the amount of days between 2 DateTimePickers

    Here is what i'm doing

    I have 2 DateTimePickers called

    dtpFirstDay
    dtpLastDay

    and a TextBox called

    tbNumberOfDays

    I know I could use a label to show the number of days between the two DateTimePickers, but I was wanting to make the program a little more functional for extra credit.

    QUESTION 2 For extra Credit
    with the text I wanted to input any number and have it automatically adjust the dtpLastDay.value to the correct date.

    Please Help

    Thank you
    Last edited by Phantom Coder; Aug 5th, 2006 at 07:05 AM.

  2. #2
    Hyperactive Member The_Duck's Avatar
    Join Date
    May 2005
    Location
    Leamington, UK
    Posts
    351

    Re: please help project about due!

    What your looking to do is pretty straight forward.

    Simply set dtpLastDay = dtpFirstDay.AddDays(CDbl(Me.txtDaysBetween.txt))

  3. #3

    Thread Starter
    Junior Member Phantom Coder's Avatar
    Join Date
    Aug 2006
    Posts
    24

    Thumbs up Re: please help project about due!

    Ty for the help to my extra credit part of my problem. It was close and gave me another way to look at the problem. This is what I had to do to get it to work.

    dtpLastDate.Value = dtpFirstDate.Value.AddDays(tbNumberOfDays.Text)

    this works fine unless I put in a date then erase it then leave the box. but that should not be a problem if I can get some help on first question. as for making the textbox accept only numbers I believe I can use a case statement to lock out the alphabet and unwanted character if im not mistaken.


    Would you happen to know how to get the number of days between the two DTP's when I change the LastDate one to show up in the TextBox?

    Thanks again for you help The Duck
    Last edited by Phantom Coder; Aug 5th, 2006 at 07:27 AM.

  4. #4
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: please help project about due!

    Quote Originally Posted by Phantom Coder
    ...but that should not be a problem if I can get some help on first question. as for making the textbox accept only numbers I believe I can use a case statement to lock out the alphabet and unwanted character if im not mistaken.
    There are some options, this is how I do it:
    VB Code:
    1. If (Not IsNumeric(e.KeyChar)) AndAlso (e.KeyChar <> vbBack) Then
    2.             e.Handled = True
    3.             Beep()
    4.         End If
    Use it in the KeyPress event of the textbox. It will accept only numbers and backspace and beep otherwise. The beep is a nice touch but not really needed except for ergonomic reasons.
    VB 2005, Win Xp Pro sp2

  5. #5

    Thread Starter
    Junior Member Phantom Coder's Avatar
    Join Date
    Aug 2006
    Posts
    24

    Re: please help project about due!

    Quote Originally Posted by Phantom Coder
    Hello everyone

    If you can help me you can save my grade.

    Im new to visual basic, I'm using visual basic.net 2005 standard edition for a school project.

    QUESTION 1
    I just need to know how to get the amount of days between 2 DateTimePickers

    Here is what i'm doing

    I have 2 DateTimePickers called

    dtpFirstDay
    dtpLastDay

    and a TextBox called

    tbNumberOfDays

    I know I could use a label to show the number of days between the two DateTimePickers, but I was wanting to make the program a little more functional for extra credit.

    First of all thank you for helping me out it is going much smoother now.

    But What my main question was is how do i calculate the number of days between 2 DTP's named above and have the total number of days apear in the TextBox Names above. with your guys help I have the text box to DTP working but I need to have the DTP put the number of days in the text box.

    Thanks alot I really appreciate this

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: please help project about due!

    Each DTP has a Value property that is a Date object. If you subtract one Date from another Date you get a TimeSpan object, which has a Days property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Junior Member Phantom Coder's Avatar
    Join Date
    Aug 2006
    Posts
    24

    Question Re: please help project about due!

    Quote Originally Posted by jmcilhinney
    Each DTP has a Value property that is a Date object. If you subtract one Date from another Date you get a TimeSpan object, which has a Days property.
    am I doing this right?

    TextBox1.Text = DateTimePicker2.Value.Day - DateTimePicker1.Value.Day

    it works ok until i change the month then it does not count the months that is between the days. it only subtracts the two day numbers.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: please help project about due!

    Read what I posted. I did not say to subtract the Day property of one Date from the Day property of another Date. In fact, I didn't mention the Day property of the Date object at all.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Junior Member Phantom Coder's Avatar
    Join Date
    Aug 2006
    Posts
    24

    Question Re: please help project about due!

    Quote Originally Posted by jmcilhinney
    Read what I posted. I did not say to subtract the Day property of one Date from the Day property of another Date. In fact, I didn't mention the Day property of the Date object at all.
    as mentioned in my firsts post im pretty new to VB and this is only my 10th project I read your post and that is what I got from it. I'm sorry that I do not know or understand completely how to use the timespan thingy I searched the web and have not found anything I could learn from. I tried several dirfferent things and they all had a blue line under the statement. If you had an example would be a great help

    I used the value.date property but when i tried to subtract one from the other I get some error about can convert to string


    as I stated before Im very greatful of any help.

  10. #10
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: please help project about due!

    Looks like similar homework from This thread. Refer to that thread for an example on subtracting dates... just remember to use the Date from the DTP...

  11. #11

    Thread Starter
    Junior Member Phantom Coder's Avatar
    Join Date
    Aug 2006
    Posts
    24

    Re: please help project about due!

    I finally gotten this part to work right thanks everyone for the help. this is what I did.

    VB Code:
    1. Dim NODs As TimeSpan = DateTimePicker2.Value.Subtract(DateTimePicker1.Value)
    2.         Dim days As Integer = NODs.Days
    3.         TextBox1.Text = CInt(days)

    If anbyone have a fix for this problem that creapt up? I would be greatful

    I'm using a textbox.text to enter the number of days to set the value of the datetimepicker2.value I have that working with the following code

    VB Code:
    1. DateTimePicker1.Value = DateTimePickerDateIn.Value.AddDays(TextBox.Text)

    what happens is when i delete the default 1 in the TextBox.Text to enter like 30 it crashes since null values seem to crash the program. I need it where it wont let the box be empty. I'm using the following code to only to allow numbers and system keys.

    VB Code:
    1. 'Only allow numbers to be entered into the TextBox
    2.         If Not Char.IsNumber(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
    3.  
    4.             e.Handled = True
    5.             Beep()
    6.             MessageBox.Show("enter numbers only")
    7.  
    8.         End If

    as always I'm very greatful for any help

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: please help project about due!

    Preventing the TextBox being empty is not a good idea. What you need to do is check that the text in the TextBox is a valid integer before you use it:
    VB Code:
    1. Dim dayCount As Integer
    2.  
    3. If Integer.TryParse(myTextBox.Text, dayCount) Then
    4.     myDTP2.Value = myDTP1.Value.AddDays(DayCount)
    5. End If
    Remember that a TextBox is a VERY general purpose field for accepting text input. If you want numerical input then it is normally advisable to use a NumericUpDown control. If you want the user to enter text that has some specific meaning then it's up to you to validate what is entered and make sure that it means what it's supposed to. If it doesn't then you act accordingly, which may mean rejecting the input, ignoring it or alerting the user. Preventing the user clearing the TextBox is just counter-intuitive. They should be able to clear it in preparation for entering a new value.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13
    New Member LadyJustice179's Avatar
    Join Date
    Mar 2008
    Location
    San Diego Ca
    Posts
    1

    Counting Days Between Date And Time Pickers

    Sub CalculateDays()
    'THIS IS GOING TO BE THE DIFFERENCE OF THE START DATE AND THE END DATE
    txtDays.Text = CStr(DateTimePicker2.Value.Date.DayOfYear - DateTimePicker1.Value.Date.DayOfYear)
    End Sub

    This is a function you can call to make your goal happen. I have written several programs that deal with this to include databases and deadlines. It appeared to me that the other posts were not very clear and a bit confusing, so I thought I would step in and help you out.

    1. by using the day of the year you are working with a julian calendar and therefore, it allows you to get accurate calculations for the months ahead. I noticed you were using "day" and other objects without success. I hope this helps

    Tiffani Hume

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