Connecting Tech Pros Worldwide Forums | Help | Site Map

DateTimePicker event in C#.net

Member
 
Join Date: Apr 2008
Posts: 65
#1: Jul 3 '09
Hi All,
Can anyone suggest me, to display error message as"Select Valid Date", wht event i cn use for DateTimePicker. If i use leave event, user can change the date by closeup event, It should not happen. If i use both leave and closeup it posting the error message twice. Suggest whtz the correct value to display tht Error at single time, itz should not happen, whn i click another control(like textbox,button).
Reply me fast.
Thanks in Advance.

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,783
#2: Jul 3 '09

re: DateTimePicker event in C#.net


Please take a look at this posting guidelines topic regarding the use of abbreviations and SMS style text.

http://bytes.com/faq.php?faq=posting...ask_a_question

I genuinely want to see people get answers to their questions/problems. But speaking for myself, when its as much work to decypher the question as it to come up with a solution I stop reading the question about half way through the first paragraph. My general though process is something like: "If its not important enough to the poster to use all the letters in the words, why should I consider it important enough to read?"

wht
cn
whtz
tht
whn
i (instead of capital 'I')

These did not come from an on-line translator or foreign language to English dictionary. This is just ebonics for the keyboard.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,783
#3: Jul 3 '09

re: DateTimePicker event in C#.net


Quote:
Can anyone suggest me, to display error message as"Select Valid Date"
Expand|Select|Wrap|Line Numbers
  1. MessageBox.Show("Please select a valid date", "Invalid date");
Quote:
wht event i cn use for DateTimePicker
With any control, a good place to start is the default event. This will be the event that most people, mostly commonly need from a control. In this case the "value changed" event is the default for a DateTimePicker. After dragging your DateTimePicker from the toolbox to the form designer, just double-click the DateTimePicker. Visual Studio will add an empty method for you for the "value changed" event.
Expand|Select|Wrap|Line Numbers
  1.         private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
  2.         {
  3.  
  4.         }
Quote:
Suggest whtz the correct value to display tht Error at single time, itz should not happen, whn i click another control(like textbox,button).
I have no idea what this is asking.
Expert
 
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 410
#4: Jul 3 '09

re: DateTimePicker event in C#.net


Quote:

Originally Posted by Limno View Post

Suggest whtz the correct value to display tht Error at single time, itz should not happen, whn i click another control(like textbox,button).

I think the OP doesn't want validation to occur for all controls on the page, only one control validated at a time before it loses focus. Then again I might be totally wrong (guessing here).

But if my guess is correct, the method suggested by tlhintoq will work perfectly i.e. code validation into the control's default behavior.
Member
 
Join Date: Apr 2008
Posts: 65
#5: Jul 3 '09

re: DateTimePicker event in C#.net


yes Cloud255, you are right, my code is just like this

if (dtpdate.text==convert.tostring(datetime.now()))
{
textbox1.text=0;
dtpdate.text="";
return;
}

I am writing this code in valuechange event, everytime dtpdate getting clear, so value is changing, it repeating its event. If i use closeup event, user changing value using keyboard, to avoid this i m using LEAVE event too, but everytime that message gets displaying. Suggest me good code.
Expert
 
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 410
#6: Jul 6 '09

re: DateTimePicker event in C#.net


Hi,

I'm sorry, but I cannot seem to understand what you are trying to say, I understand that English is probably not your first language, but I really cannot figure out what the problem is.

Perhaps you know someone more apt in English who could help you describe your situation to us?
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,783
#7: Jul 6 '09

re: DateTimePicker event in C#.net


Quote:
Expand|Select|Wrap|Line Numbers
  1. if (dtpdate.text==convert.tostring(datetime.now()))
  2. {
  3. textbox1.text=0;
  4. dtpdate.text="";
  5. return;
  6. }
Based on the notation... dtpdate is the DateTimePicker control. And you're clearing that by just changing the text. I don't think you can really change the *value* of a DTP by setting the text to null.
Quote:
everytime dtpdate getting clear, so value is changing, it repeating its event.
Of course you are getting an infinite loop...
You are setting the text of the textbox to null
You are setting the text of the DTP to null
Then you do a comparrison to see if they are the same, which they are.
Then do it all over again. and again. and again.
Null == Null
Newbie
 
Join Date: Oct 2008
Posts: 17
#8: Jul 6 '09

re: DateTimePicker event in C#.net


If I understand the problem correctly that infinite loop is the problem. I think the poster wants to check validity as the user enters each value but is encountering that loop. The Leave won't work because the user can close the form before it is run.

You could use a boolean flag to determine if you actually wanted to validate your data and then set it false when your changing the value. See this code:

Expand|Select|Wrap|Line Numbers
  1.         bool validate = true;
  2.  
  3.         private void dtpdate_ValueChanged(object sender, EventArgs e)
  4.         {
  5.             if (validate)
  6.             {
  7.                 validate = false;
  8.  
  9.                 if (dtpdate.text==Convert.ToString(DateTime.Now()))
  10.                 {
  11.                     textbox1.Text=0;
  12.                     dtpdate.Text="";
  13.                 }
  14.  
  15.                 validate = true;
  16.             }
  17.         }
  18.     }
There is probably a better way but this will work. (If I understand the problem correctly)
Reply