Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing small winform app to send an automatic email to customer at a specific time everyday.The customer will set the time from the UI, so when the system time = time set by customer, my app will send email. I included a datetimepicker in which customer set his desired trigger time. The customized the format of date time picker to time.


C#
private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Interval = 1000;
            toolStripTextBox1.Text = DateTime.Now.ToString("dd-MM-yyyy");
            toolStripTextBox2.Text = DateTime.Now.ToString("h:mm:ss tt");
           
        }


C#
private void Form1_Load(object sender, EventArgs e)
       {

           dateTimePicker1.Format = DateTimePickerFormat.Time;
           dateTimePicker1.ShowUpDown = true;
           dateTimePicker1.CustomFormat = "h:mm:ss tt";
           dateTimePicker1.Value = DateTime.Now;
       }


I am confused now that, how to comapre if datetimepicker.value = toolStripTextBox2.Text? Because even if I comapre, there is no error but still email is not getting sent.

What I have tried:

if (dateTimePicker1.Value.ToString() == toolStripTextBox2.Text)
{
textBox3.Text = "problem solved";
}
Posted
Updated 5-Jul-16 21:49pm
Comments
Garth J Lancaster 6-Jul-16 3:53am    
The one thing I see straight off, is the assumption that dateTimePicker1.Value.ToString() actually returns the value in the format you've specified - have you checked what you get in that string ? My thoughts from reading the docco is that setting the Custom Format is for the dateTimePicker control display only, not necessarily what you get back from it when you read the value .. do you get what Im saying ? ,, ie, what I think you need is

if (dateTimePicker1.Value.ToString("h:mm:ss tt") == toolStripTextBox2.Text)
Member 12226114 6-Jul-16 4:33am    
Thnaks a lot. It works

What is the purpose of the textboxes? All the app needs on load is to get the time value from the user. It then saves that time as a variable, and can then compare that to the clock time, in order to decide when to send the email.
 
Share this answer
 
Comments
Member 12226114 6-Jul-16 3:52am    
Yes, you're right exactly. The text boxes are just to display the date and time to the operator of UI. Just a formal thing. But I don't get to know on how to check the system current time = datetimepicker value or not?
Richard MacCutchan 6-Jul-16 4:18am    
Like I said above: save the time in a variable and then get the time from the system and compare the two values. And use actual values, don't convert to strings.
Member 12226114 6-Jul-16 4:34am    
Thanks a lot. Works too. Got two solutions
BillWoodruff 6-Jul-16 16:05pm    
+5 direct, effective :)
Richard MacCutchan 7-Jul-16 2:50am    
:thumbsup:
I suggest you to use the Windows Task Scheduler[^] instead.
 
Share this answer
 
Comments
Member 12226114 6-Jul-16 3:57am    
Cannot use task scheduler for this requirement. Because it's having UI and it's a some other customized options the user needs to be defined.
BillWoodruff 6-Jul-16 10:58am    
You can use the Win Task Scheduler from C#, and there are CodeProject articles that show you how to do that. Start here:

https://www.google.ca/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c%23%20using%20windows%20task%20scheduler
BillWoodruff 6-Jul-16 10:59am    
+5 While the OP may not want to invest in exploring the Win Task Scheduler, now, I think that is the best way to go ... for the "long run."
CPallini 6-Jul-16 13:02pm    
Exactly. Thank you.

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