|
List Info
Thread: RE: Re: DateTimePicker
|
|
| RE: Re: DateTimePicker |
  United States |
2007-03-20 19:41:28 |
|
Assuming that you have the date difference
calculated, it should be a straightforward assignment of that variable to the
text box.
myTextBox.Text = dateDifference.ToString
Cheers,
/tr
From: helpwithvb yahoogroups.com [mailto:helpwithvb yahoogroups.com] On Behalf Of Eddie Farrell
Sent: Tuesday, March 20, 2007 5:49
PM
To: helpwithvb yahoogroups.com
Subject: RE: [helpwithvb] Re:
DateTimePicker
Timothy
Your code works brilliantly in the small project that I
instigated following your advice . It certainly does help.
Are you able to advise me how I could use the code to
display the result in the small text box that I have between the two DTP
controls on my main form? (In my original project) If so , I would regard
this problem as totally solved.
In any event, thank you very much.
Regards
Eddie
First off, you are running VB.NET correct?
Second, the example that I provided was a program in and of itself.
If you tried integrating this with what you currently had…bad results
could follow.
Third,if you are running VB2003 or later and you would like to see
this run. Start a new project. (Windows Form) Add two DateTimePicker controls
to the form. Open the code window for the form (Right click on the form and
select View Code.) The first line that you will most likely see is
“;Public Class Form1.R21; Copy all of the code that I had sent
and except for the first line and last lines (Public Class Form1 and End Class)
and past into your code window.
Go ahead and run it.
Does that help?
/tr
From: helpwithvb yahoogroups.com
[mailto:helpwithvb yahoogroups.com]
On Behalf Of Eddie Farrell
Sent: Tuesday, March 20, 2007 4:39
PM
To: helpwithvb yahoogroups.com
Subject: RE: [helpwithvb] Re:
DateTimePicker
Hi Timothy and All the other Guys who have tried to help
me.
I tried to run the code that you sent me Timothy by entering it
into the main form of my project and received a message indicating that there
were 43 errors. The first one said that an “end of statement was expected8221;
I have seen this message before when I tried to do something but
have never been able to find out what it meant.
This, in spite of looking at the Help file and on the Internet.
It may be that I did not understand the content I did find.
Thinking I was quite clever I declared some constants
(after reading about them in a book) and these now all have a wavy blue line
beneath them with an error message which reads: “statement is not valid
in a namespace”
Again, I have no idea what this means.
I would really appreciate some help with these problems and I am
including a copy of the code as I entered it at the top of my Forms̵7; code
window:
Public Class Form1
Dim date1 As Date
Dim date2 As Date
Dim difference As Long
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DateTimePicker1.Value = Now
DateTimePicker2.Value = Now
End Sub
Private Sub TimeValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged,
DateTimePicker2.ValueChanged
date1 = DateTimePicker1.Value
date2 = DateTimePicker2.Value
difference = DateDiff("d",
date2, date1))
MessageBox.Show(difference) 'here is where you could use this string
value and send it to a textbox
End Sub
End Class
I changed Dim
difference to Long with a view to performing some math later in the programme
and I also removed Cstr for the same reason.
Regards to All
Eddie
From: helpwithvb yahoogroups.com
[mailto:helpwithvb yahoogroups.com]
On Behalf Of Timothy Rupp
Sent: 18 March 2007 16:57
To: helpwithvb yahoogroups.com
Subject: RE: [helpwithvb] Re:
DateTimePicker
Hi Eddie,
Taking a look at your code…;why did you declare the Difference
as a String?
If I recall correctly, the DateDiff function returns a Long value. I’m
going to take a guess that you’re not turning on Option Strict or Option Explicit because code of this type would have cause
it to squawk at you.
As for not wanting a command button on your form, you can trigger
the event with a change in the DateTimePicker control. Here’;s a little sample that
I knocked together:
Option Strict On
Public Class Form1
Dim date1 As Date
Dim date2 As Date
Dim difference As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DateTimePicker1.Value = Now
DateTimePicker2.Value = Now
End Sub
Private Sub TimeValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged,
DateTimePicker2.ValueChanged
date1 = DateTimePicker1.Value
date2 = DateTimePicker2.Value
difference = CStr(DateDiff("d", date2,
date1))
MessageBox.Show(difference) ‘here is where you could use this
string value and send it to a textbox
End Sub
End Class
Notice that I left the difference
as a string, but in
calculating it we used the CStr conversion.
You will also see that I’ve created my own event handler that
handles both the DTP1 and DTP2 changed function. Each time that the value
changes we calculate the difference and show the message box.
Is that more like what you had envisioned?
/tr
From: helpwithvb yahoogroups.com
[mailto:helpwithvb yahoogroups.com]
On Behalf Of Eddie Farrell
Sent: Sunday, March 18, 2007 10:29
AM
To: helpwithvb yahoogroups.com
Subject: RE: [helpwithvb] Re:
DateTimePicker
Steve
I really appreciate your comprehensive response to my problem
and I also appreciate the advice I have been given by other members of the
group.
Regarding your envisaged scenario, which I greatly value:
In my proposed scenario I have the two DTP controls to enable
the user to enter a starting date and a finishing date.
I also have a small text box which I hope can be used to accept
the returned value that I need.
On reflection, I now consider that what I am really looking for,
is a returned value which should show 3, i.e. if date one is 18/03/2007
and date two is 21/03/2007 I would be hoping 3 would be returned to the text
box. I realise that this is not the inclusive return I originally thought I would
need and apologise to every one who has responded to me.
From the Internet I downloaded the following code sample and
wonder if it would do the job. Could I double click the text box in design view
and use/amend this code to pick up the start/finish dates and return the
required value to the text box in a numerical format? If possible I do not want
to have another command button on the form.
I think I am pretty ambitious here for a beginner, but I would
hope to be able to use the text box value in a second form (the only
other one, discounting a LogIn form)̷0;….From other reading material,
I think that means the date variables in the code sample would have to be
declared as Public. Are you able to verify this?
Here is the code (I changed the font for readability) which I
hope you will consider:
Dim Date1 As Date
Dim Date2 As Date
Dim Difference As String
Date1 = Date
Date2 = 12 / 31 / 2030
Difference = DateDiff("d", Date2, Date1)
MsgBox Difference
You have given me plenty to think about as I did not
consider validation of user entry in connection with dates entered etc., and I
can see more questions emerging once I attempt to do this. I looked into the
web site vb-helper.com that you recommended and was quite impressed. I had
never heard of it before.
In concluding, I feel that I really must reiterate my
thanks for your assistance.
Sincere regards
Eddie Farrell
Hi Eddie,
The Dot Net people on the list will probably
offer some more specific advise, but when we
write code we want it to be enclosed within
a function or subroutine that does the work
on our data, so we can provide what is needed
in response to something that the User does
( called an ‘Event’ ) such as pressing a
button, menu control, et cetera.
VB dot Net may require that certain things
be located in specific places, or areas, but
within any such area it does not matter what
order you type in your routines, functions,
and/or event handlers.
How you place things onto the form that
the User looks at, has nothing to do with
the placement of your code within the
Design Environment software [ I-D-E ].
For some code that works, and which you can
download for study, go to this web site
and look for interesting example projects:
www.vb-helper.com
Scan through this page, but I didn't see any
examples with the DTP, but there are tonnes
of other projects that will be interesting:
http://vb-helper.com/index_controls.html
---------------------------------
Consider this short scenario:
On your form you have the two DTPs, and
one command button, and one textbox
where we will write the final answer.
Your form allows the User to enter 2 dates
so that they can figure out how old they are.
Basically, where the code goes on the page does not
really matter because the Operating System will
know what to look for and where to find it,
on the page, or within your project solution.
Your User Presses that Button.
The Operating System looks to see if you have
provided an EVENT HANDLER for that Button Click.
If you did, the OS will execute the code inside
that event handler217;s code block:
a) code to verify the User entered 2 dates
b) code to make sure that both days are valid
c) code to call your function to calculate
the age, to do the math so to speak.
d) code to write the answer into the textbox.
There are a hundred other ‘little-things’ to do,
or to consider, but for now that is the basic set-up.
All The Best,
Steve
.
11:26 AM EST Saturday, March 17, 2007
.
.
________________________________________
From: helpwithvb%40yahoogroups.com">helpwithvb yahoogroups.com
[mailto: helpwithvb%40yahoogroups.com">helpwithvb yahoogroups.com]
On Behalf Of Eddie Farrell
Sent: Saturday, March 17, 2007 6:46 AM
To: helpwithvb%40yahoogroups.com">helpwithvb yahoogroups.com
Subject: RE: [helpwithvb] Re: DateTimePicker
Richard
Sincere thanks for your assistance. I am pretty much a beginner and I picked
up the DateTimePicker knowledge from a text book and a couple of examples
from the Internet. I am not sure of the layout of the code and also where to
place the code. Does the code target the textbox control between the two
DateTimePicker controls and where would the code be written ?
From: helpwithvb%40yahoogroups.com">helpwithvb yahoogroups.com
[mailto: helpwithvb%40yahoogroups.com">helpwithvb yahoogroups.com]
On
Behalf Of Richard
Sent: 16 March 2007 17:17
To: helpwithvb%40yahoogroups.com">helpwithvb yahoogroups.com
Subject: [helpwithvb] Re: DateTimePicker
If you want to include both the start date and end date in the
calculation, it's pretty easy. Use the DateDiff() function that you
already knew about, then add two to that value - one for the start
date, one for the end date. If you only want to include ONE OR the
OTHER, only add one after you calculate the span using the DateDiff()
function.
HTH!
Alienwebmaster
.
.
.
__._,_.___
.
__,_._,___
|
| RE: Re: DateTimePicker |
  United Kingdom |
2007-03-21 14:18:38 |
|
Timothy
Thanks a bundle, because of all the other code etc. which became
affected in my original programme I am going to start afresh. I see the logic
in what you say and can only forward sincere thanks.
Eddie
Assuming
that you have the date difference calculated, it should be a straightforward
assignment of that variable to the text box.
myTextBox.Text
= dateDifference.ToString
Cheers,
/tr
From: helpwithvb yahoogroups.com
[mailto:helpwithvb yahoogroups.com] On Behalf Of Eddie Farrell
Sent: Tuesday, March 20, 2007 5:49 PM
To: helpwithvb yahoogroups.com
Subject: RE: [helpwithvb] Re: DateTimePicker
Timothy
Your code works brilliantly in the small
project that I instigated following your advice . It certainly does help.
Are you able to advise me how I
could use the code to display the result in the small text box that I have
between the two DTP controls on my main form? (In my original project) If
so , I would regard this problem as totally solved.
In any event, thank you very much.
Regards
Eddie
First off, you are running VB.NET correct?
Second, the example that I provided was a
program in and of itself. If you tried integrating this with what you currently
had…bad results could follow.
Third,if you are running VB2003 or later
and you would like to see this run. Start a new project. (Windows Form) Add two
DateTimePicker controls to the form. Open the code window for the form (Right
click on the form and select View Code.) The first line that you will most likely
see is “Public Class Form1.R21; Copy all of the code that I had sent and
except for the first line and last lines (Public Class Form1 and End Class) and
past into your code window.
Go ahead and run it.
Does that help?
/tr
From: helpwithvb yahoogroups.com
[mailto:helpwithvb yahoogroups.com] On Behalf Of Eddie Farrell
Sent: Tuesday, March 20, 2007 4:39 PM
To: helpwithvb yahoogroups.com
Subject: RE: [helpwithvb] Re: DateTimePicker
Hi Timothy and All the other Guys
who have tried to help me.
I tried to run the code that you sent me
Timothy by entering it into the main form of my project and received a message
indicating that there were 43 errors. The first one said that an “end of
statement was expected8221;
I have seen this message before when I
tried to do something but have never been able to find out what it meant.
This, in spite of looking at the Help
file and on the Internet. It may be that I did not understand the content
I did find.
Thinking I was quite clever I declared
some constants (after reading about them in a book) and these now all
have a wavy blue line beneath them with an error message which reads:
8220;statement is not valid in a namespace”
Again, I have no idea what this means.
I would really appreciate some help with
these problems and I am including a copy of the code as I entered it at the top
of my Forms̵7; code window:
Public Class
Form1
Dim date1 As Date
Dim date2 As Date
Dim difference As Long
Private Sub
Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
DateTimePicker1.Value = Now
DateTimePicker2.Value = Now
End Sub
Private Sub
TimeValueChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles
DateTimePicker1.ValueChanged, DateTimePicker2.ValueChanged
date1 = DateTimePicker1.Value
date2 = DateTimePicker2.Value
difference = DateDiff("d", date2,
date1))
MessageBox.Show(difference) 'here is where you could use this string value and send it to
a textbox
End Sub
End Class
I changed Dim difference to Long with a view to
performing some math later in the programme and I also removed Cstr for the
same reason.
Regards to All
Eddie
From: helpwithvb yahoogroups.com
[mailto:helpwithvb yahoogroups.com] On Behalf Of Timothy Rupp
Sent: 18 March 2007 16:57
To: helpwithvb yahoogroups.com
Subject: RE: [helpwithvb] Re: DateTimePicker
Hi Eddie,
Taking a look at your code…;why did you declare the
Difference as a String?
If I recall correctly, the DateDiff function
returns a Long value. I’m going to take a guess that you’re not turning on Option Strict or Option Explicit because code of
this type would have cause it to squawk at you.
As for not wanting a command button on your form, you can
trigger the event with a change in the DateTimePicker control. Here’;s a
little sample that I knocked together:
Option Strict
On
Public Class
Form1
Dim date1 As Date
Dim date2 As Date
Dim difference As String
Private Sub
Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
DateTimePicker1.Value = Now
DateTimePicker2.Value = Now
End Sub
Private Sub
TimeValueChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles
DateTimePicker1.ValueChanged, DateTimePicker2.ValueChanged
date1 = DateTimePicker1.Value
date2 = DateTimePicker2.Value
difference = CStr(DateDiff("d", date2, date1))
MessageBox.Show(difference) ‘here is where you could use this string value and send it
to a textbox
End Sub
End Class
Notice that I left the difference as a string, but in
calculating it we used the CStr conversion.
You will also see that I’ve created my own event handler
that handles both the DTP1 and DTP2 changed function. Each time that the value
changes we calculate the difference and show the message box.
Is that more like what you had envisioned?
/tr
From: helpwithvb yahoogroups.com
[mailto:helpwithvb yahoogroups.com] On Behalf Of Eddie Farrell
Sent: Sunday, March 18, 2007 10:29 AM
To: helpwithvb yahoogroups.com
Subject: RE: [helpwithvb] Re: DateTimePicker
Steve
I really appreciate your comprehensive
response to my problem and I also appreciate the advice I have been given by
other members of the group.
Regarding your envisaged scenario, which
I greatly value:
In my proposed scenario I have the two
DTP controls to enable the user to enter a starting date and a finishing date.
I also have a small text box which I
hope can be used to accept the returned value that I need.
On reflection, I now consider that what
I am really looking for, is a returned value which should show 3, i.e. if
date one is 18/03/2007 and date two is 21/03/2007 I would be hoping 3 would be
returned to the text box. I realise that this is not the inclusive return I
originally thought I would need and apologise to every one who has responded to
me.
From the Internet I downloaded the
following code sample and wonder if it would do the job. Could I double click
the text box in design view and use/amend this code to pick up the start/finish
dates and return the required value to the text box in a numerical format? If
possible I do not want to have another command button on the form.
I think I am pretty ambitious here for a
beginner, but I would hope to be able to use the text box value in a
second form (the only other one, discounting a LogIn form)̷0;….From other reading
material, I think that means the date variables in the code sample would have
to be declared as Public. Are you able to verify this?
Here is the code (I changed the font for
readability) which I hope you will consider:
Dim Date1 As Date
Dim Date2 As Date
Dim Difference As String
Date1 = Date
Date2 = 12 / 31 / 2030
Difference = DateDiff("d", Date2, Date1)
MsgBox Difference
You have given me plenty to think about as I did
not consider validation of user entry in connection with dates entered etc.,
and I can see more questions emerging once I attempt to do this. I looked into
the web site vb-helper.com that you recommended and was quite impressed. I had
never heard of it before.
In concluding, I feel that I really must
reiterate my thanks for your assistance.
Sincere regards
Eddie Farrell
Hi Eddie,
The Dot Net people on the list will probably
offer some more specific advise, but when we
write code we want it to be enclosed within
a function or subroutine that does the work
on our data, so we can provide what is needed
in response to something that the User does
( called an ‘Event’ ) such as pressing a
button, menu control, et cetera.
VB dot Net may require that certain things
be located in specific places, or areas, but
within any such area it does not matter what
order you type in your routines, functions,
and/or event handlers.
How you place things onto the form that
the User looks at, has nothing to do with
the placement of your code within the
Design Environment software [ I-D-E ].
For some code that works, and which you can
download for study, go to this web site
and look for interesting example projects:
www.vb-helper.com
Scan through this page, but I didn't see any
examples with the DTP, but there are tonnes
of other projects that will be interesting:
http://vb-helper.com/index_controls.html
---------------------------------
Consider this short scenario:
On your form you have the two DTPs, and
one command button, and one textbox
where we will write the final answer.
Your form allows the User to enter 2 dates
so that they can figure out how old they are.
Basically, where the code goes on the page does not
really matter because the Operating System will
know what to look for and where to find it,
on the page, or within your project solution.
Your User Presses that Button.
The Operating System looks to see if you have
provided an EVENT HANDLER for that Button Click.
If you did, the OS will execute the code inside
that event handler217;s code block:
a) code to verify the User entered 2 dates
b) code to make sure that both days are valid
c) code to call your function to calculate
the age, to do the math so to speak.
d) code to write the answer into the textbox.
There are a hundred other ‘little-things’ to do,
or to consider, but for now that is the basic set-up.
All The Best,
Steve
.
11:26 AM EST Saturday, March 17, 2007
.
.
________________________________________
From: helpwithvb%40yahoogroups.com">helpwithvb yahoogroups.com
[mailto: helpwithvb%40yahoogroups.com">helpwithvb yahoogroups.com]
On Behalf Of Eddie Farrell
Sent: Saturday, March 17, 2007 6:46 AM
To: helpwithvb%40yahoogroups.com">helpwithvb yahoogroups.com
Subject: RE: [helpwithvb] Re: DateTimePicker
Richard
Sincere thanks for your assistance. I am pretty much a beginner and I picked
up the DateTimePicker knowledge from a text book and a couple of examples
from the Internet. I am not sure of the layout of the code and also where to
place the code. Does the code target the textbox control between the two
DateTimePicker controls and where would the code be written ?
From: helpwithvb%40yahoogroups.com">helpwithvb yahoogroups.com
[mailto: helpwithvb
| |