|
-
Aug 5th, 2006, 06:57 AM
#1
Thread Starter
Junior Member
[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.
-
Aug 5th, 2006, 07:01 AM
#2
Hyperactive Member
Re: please help project about due!
What your looking to do is pretty straight forward.
Simply set dtpLastDay = dtpFirstDay.AddDays(CDbl(Me.txtDaysBetween.txt))
-
Aug 5th, 2006, 07:07 AM
#3
Thread Starter
Junior Member
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.
-
Aug 5th, 2006, 08:18 AM
#4
Re: please help project about due!
 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:
If (Not IsNumeric(e.KeyChar)) AndAlso (e.KeyChar <> vbBack) Then
e.Handled = True
Beep()
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.
-
Aug 5th, 2006, 08:30 AM
#5
Thread Starter
Junior Member
Re: please help project about due!
 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
-
Aug 5th, 2006, 09:28 AM
#6
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.
-
Aug 5th, 2006, 05:21 PM
#7
Thread Starter
Junior Member
Re: please help project about due!
 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.
-
Aug 5th, 2006, 07:55 PM
#8
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.
-
Aug 5th, 2006, 08:03 PM
#9
Thread Starter
Junior Member
Re: please help project about due!
 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.
-
Aug 5th, 2006, 08:05 PM
#10
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...
-
Aug 5th, 2006, 08:35 PM
#11
Thread Starter
Junior Member
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:
Dim NODs As TimeSpan = DateTimePicker2.Value.Subtract(DateTimePicker1.Value)
Dim days As Integer = NODs.Days
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:
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:
'Only allow numbers to be entered into the TextBox
If Not Char.IsNumber(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
e.Handled = True
Beep()
MessageBox.Show("enter numbers only")
End If
as always I'm very greatful for any help
-
Aug 5th, 2006, 08:55 PM
#12
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:
Dim dayCount As Integer
If Integer.TryParse(myTextBox.Text, dayCount) Then
myDTP2.Value = myDTP1.Value.AddDays(DayCount)
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.
-
Mar 25th, 2008, 07:22 PM
#13
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|