By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,377 Members | 471 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,377 IT Pros & Developers. It's quick & easy.

How to display a message box on a specific date

P: 1
I want to create one program that I can register informations in file, till here it works, BUT i want it when any information will finish the month, i want it to show me MessageBox, the code that i use is below, but it doesn't work:
Expand|Select|Wrap|Line Numbers
  1. if (date.Text == DateTime.Now.AddMonths(1).ToString()) {
  2.        MessageBox.Show("Hello... the time has come !! ");
  3. }//end if
ah date.text is variable of datetime
May 29 '10 #1
Share this Question
Share on Google+
4 Replies

P: 32
Try this:
if (DateTime.Parse(date.Text).Month.ToString() == DateTime.Now.AddMonths(1).ToString())

make sure you have validation in the text box that check the input is a valid date.

Hope this one helps.
May 30 '10 #2

Expert 100+
P: 750
On your Form load you can call a function that would
a. Check if there are any information to display.
b. If yes then display the message.
Also consider using a database for storing information. Maybe sql express.
May 31 '10 #3

Expert 2.5K+
P: 3,525
mratif: ah date.text is variable of datetime
Is it really a variable? Or is it a textbox and you are getting the .Text property from it? If it were really a variable, then you probably wouldn't have to get the .Text of it.

liadmz: if (DateTime.Parse(date.Text).Month.ToString() == DateTime.Now.AddMonths(1).ToString())
It is unlikely that this will every give you a match.
DateTime object goes down to 3 decimal places of milliseconds.
So a DateTime of 'now' is 31may2010 10:18:30.123
Expand|Select|Wrap|Line Numbers
  1. DateTime.Now.AddMonths(1).Tostring()
is only going to take the long DateTime and add one month.
So "31may2010 10:18:30.123" will become "31june2010 10:18:30.123"

The odds of you doing your comparrison at precisely the right millisecond so your two DateTime objects are exactly equally are so high as to border on the impossible.

Also, there is no need to parse the DateTime object to make a comparrison. You can do that directly.

Expand|Select|Wrap|Line Numbers
  1. DateTime StartTime = DateTime.Now;
  2. If (DateTime.Now > StartTime)
  3. {
  4. // Do somethine
  5. }
If you are just trying to determine if a new month has started (and thus the old month has ended)
Expand|Select|Wrap|Line Numbers
  1. If (DateTime.Now.Month > StartTime.Month)
  2. {
  3.    // Then it is a new month, the old month is over
  4. }
May 31 '10 #4

Expert 5K+
P: 7,872
You may need to go a little smarter then just checking the .Month property too. January 2011 is later then Decemeber 2010 but 1 is not greater then 12
Jun 1 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.