Click here to Skip to main content
12,295,457 members (56,098 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
I have a few datetimepicker controls on my form that I wish to set to trigger a button click when the date of the datetimepicker = the date of your computer.

Basically I what im looking to do is schedule a scanning date and time.....
Is there any good examples on the net or will someone inform me of how to go about this?

I am aware of the need for a timer , a datetimepicker with labels that show the value of the current date and the value of the datetimepicker once the user has made a new selection.

thank you all for your help and sorry once again for what seems like a simple question.
Posted 1-May-11 6:49am
Dale 20121.8K

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

What sample do you need? It's too easy so hardly anyone would write an article on the problem.

In the handler of your timer get value = System.Windows.Forms.DateTimePicker.Value; the value is of the type System.DateTime. This is how to find out if it is a time to trigger: System.DateTime.Now >= value. If and when it's true, trigger your button's Click event.

Now, how to trigger it? Even though it is possible (see next paragraph) — don't do it. You can use Button.Click event by adding a handler to this event as a separate handler method. Just call this method.

The problem with click invocation based just on the Button instance is this. First, always tag your question!. I should have specify if you're using System.Windows.Forms, WPF or anything else — the answer will be different. You need to call System.Windows.Forms.Button.InvokeOnClick (Forms) or System.Windows.Controls.ButtonOnClick (WPF), but those methods are protected, so you would have to create your own derived Button class inheriting Button — just for the purpose of calling one method. It would not be practical.

—SA
  Permalink  
v2
Comments
Dale Seeley 2-May-11 3:09am
   
This is what I have worked on since your response.

objects on my form:

Datetimepicker1
Button Name "SetScan"
Button Name "cmdstartscan"
Label1 Name "lbldate"
Label2 Name "Datepickvalue"
Timer

Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
Label18.Text = DateTimePicker1.Value.Date
Refresh()
End Sub

Private Sub Timer3_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer3.Tick
lbldate.Text = Now.ToShortDateString
lbltime.Text = TimeOfDay
End Sub

Private Sub SetVS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetVS.Click
If System.DateTime.Now = DateTimePicker1.Value.Date Then
cmdStartScanning.PerformClick()
End If
End Sub

Will this work? can you give some guidance please?

thank you once again...
SAKryukov 2-May-11 4:14am
   
You don't need Refresh. From the first glance -- should work. How come you did not test it?
--SA
Dale Seeley 2-May-11 15:14pm
   
I have tested it but with no luck.

For better insight into what Im trying to do I will first off explain that the button is in a controlpannel. the schedule scan datetimepicker is on my main menu Controlpannel1. when I select a future date I have a label that displays the date the user wishes to set and if the system time is = to the label from the datetimepiker to do the button.performclick.

I have worked more on my code but still nothing happens:

Private Sub Timer3_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer3.Tick
lbldate.Text = Now.ToShortDateString
lbltime.Text = TimeOfDay
End Sub


Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
Datepickvalue.Text = DateTimePicker1.Value.Date
End Sub


Private Sub SetVS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetVS.Click
If Datepickvalue.Text = System.DateTime.Now Then
Timer2.Start()
End If
End Sub


Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
cmdStartScanning.PerformClick()
End Sub


I am running VB.Net 2010 and as far as I know my application is a windows application form.

I am having a feeling that I have to invoke a button? idk :(
SAKryukov 2-May-11 15:23pm
   
There is no such thing as "Invoke a button". What is it? Why all that trial-and-error? Do you use the Debugger? Put a break point to the timer handler. How do you know it it is fired? And so one, one step at a time? When you see it it is fired, go step by step, see if you call the method. And so on, until you get to the end...
--SA
Dale Seeley 4-May-11 4:26am
   
Im sorry but will or can you provide a working example.... I seem to be having no luck with anything and by adding break points to my project is still not definitively telling me where im going wrong.
SAKryukov 4-May-11 15:35pm
   
Oh, I see at least one bug here:
"If System.DateTime.Now = DateTimePicker1.Value.Date Then". The problem is "=". I explicitly advised to do ">=". Strict "=" will not work, because in one event time can be "not yet" and at the next time it can be "too late", so you never get to "exact same" time, or probability of exactly equal time could be extremely low. Isn't that logical?
So fix it and see. Now, if you put a break point at that event, but not inside if you would find this bug by yourself. You need to narrow down the source of the problem step by step. I cannot check it all because I don't have VB.NET installed.
--SA
Dale Seeley 4-May-11 18:14pm
   
Ok I will give that a spin and let you know what conclusion I come to. I must say thank you very much to have taken the time to write such detailed responses to my questions as not many will. It helps me to understand and learn the different types of objects and classes to use and how they work. Your a god save cheers!!
SAKryukov 4-May-11 21:30pm
   
You're welcome.
If it helps you, please consider formally accepting this answer.
Thank you.
--SA
Dale Seeley 4-May-11 18:34pm
   
Ok I made the small correction but now I am faced with maybe a logical problem?
am I calling the correct statements in the correct places?


Private Sub Timer3_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer3.Tick
lbldate.Text = Now.ToShortDateString
lbltime.Text = TimeOfDay
End Sub
Private Sub DateTimePicker1_ValueChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DateTimePicker1.ValueChanged
Datepickvalue.Text = DateTimePicker1.Value.Date
End Sub
Private Sub SetVS_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SetVS.Click
Timer2.Start()
End Sub
Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer2.Tick
If Datepickvalue.Text >= DateTime.Now Then
cmdStartScanning.PerformClick()
Else
Timer2.Stop()
End If
End Sub


please let me know if you spot any error as I am stuck and need to move onto the next project in my program.... (Sorry I don't mean that to sound pushy or impatient) your response is greatly appreciated.
SAKryukov 4-May-11 21:36pm
   
It's OK.
If you say "logical problem", how come you cannot see it?
Look at the Tick handler -- it stops the timer immediately in a first call. Can you see it? Remove
"else" to Stop() right after PerformClick(). That should fix it.
--SA
Dale Seeley 4-May-11 18:35pm
   
please ignore timer3 I do not believe it has anything to do with my problem......
Dale Seeley 5-May-11 0:03am
   
Once again sorry but I removed the else timer2.stop() as you said but still nothing happens.

Could any one of these be the cause?

I have noticed that the system date format is "Wednesday, May 04,2011", does the date time picker need to also follow that format?

also my cmdstartscanning button is located in control panel 2...... Do I need to give more instruction in the code to find the button?

I am just about out of ideas.... Moan :(
SAKryukov 5-May-11 11:20am
   
What a minute?
How can this even compile?

What are you doing?!!!!!!!!!!!!!
Look by yourself:
"Datepickvalue.Text >= DateTime.Now"
Why Text?

Compare time with time!!! must be
DateTime.Now >= Datepickvalue

Did you check the time value used to trigger a button. Are you sure it is not tomorrow?

Did you set timer interval? After all, find a working sample where a timer does something periodically, without any logic. Put something like System.Console.Beep(400, 50) to hear each tick and start from there. You again don't say what happens...

--SA
Dale Seeley 5-May-11 0:11am
   
ok got it to work now thank you so much for your help I will now rate your answers to close this topic..... :) :) :)

SAKryukov your the man!!
SAKryukov 5-May-11 11:27am
   
OK, working now? Thanks goodness.

Please formally accept this answer.
Thank you.

Good luck,
--SA
Dale Seeley 5-May-11 0:14am
   
I do have just one last question for you...........

I notice when the scheduler activates and the cmdstartbutton is clicked it does not stop clicking?????
SAKryukov 5-May-11 11:30am
   
Maybe you did not really stop timer. Again, go with debugger and see why it's missing. You say, it's working. Now, ***before*** PerformClick stop the timer.
--SA
Dale Seeley 5-May-11 20:15pm
   
Nothing is missing but once the Cmdstartscanning.click has been called it does not stop clicking the the button and if I call timer2.stop it makes the scheduler not work at all.
SAKryukov 5-May-11 22:55pm
   
You stop it too earlier. Put a break point on this stop!!!!!!!!!!!!!!
--SA
Dale Seeley 6-May-11 4:16am
   
Thank you!!!......... I have made the corrections and all is good!!
SAKryukov 6-May-11 11:17am
   
Ah finally!
Good luck,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160525.2 | Last Updated 2 May 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100