DateTimePicker event in C#.net | Member | | Join Date: Apr 2008
Posts: 65
| | |
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.
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,783
| | | 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.
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,783
| | | re: DateTimePicker event in C#.net Quote:
Can anyone suggest me, to display error message as"Select Valid Date"
- 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. - private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
-
{
-
-
}
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
| | | re: DateTimePicker event in C#.net Quote:
Originally Posted by Limno 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
| | | 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
| | | 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?
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,783
| | | re: DateTimePicker event in C#.net Quote: - if (dtpdate.text==convert.tostring(datetime.now()))
-
{
-
textbox1.text=0;
-
dtpdate.text="";
-
return;
-
}
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
| | | 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: - bool validate = true;
-
-
private void dtpdate_ValueChanged(object sender, EventArgs e)
-
{
-
if (validate)
-
{
-
validate = false;
-
-
if (dtpdate.text==Convert.ToString(DateTime.Now()))
-
{
-
textbox1.Text=0;
-
dtpdate.Text="";
-
}
-
-
validate = true;
-
}
-
}
-
}
There is probably a better way but this will work. (If I understand the problem correctly)
|  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|