Click here to Skip to main content
15,888,590 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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 solution

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
 
Share this answer
 
v2
Comments
Dale 2012 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...
Sergey Alexandrovich Kryukov 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 2012 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 :(
Sergey Alexandrovich Kryukov 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 2012 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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900