Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, may i know how can i set the textbox(eg. txt1) to accept date format(eg. 2014-05). The "-" would be automatically be there every time i run the program and after i have enter "2014", the cursor would jump to behind the "-". I know there is DateTimePicker but my idiot team leader insist me on doing the textbox thing. Any help would be appreciated thks.

Currently i have the following code(which is using DateTimePicker):

C#
string dateFrom = perFromDate.Value.ToString("MMM-yyyy");
string dateTo = perToDate.Value.ToString("MMM-yyyy");

TimeSpan range = DateTime.Parse(dateTo).Subtract(DateTime.Parse(dateFrom));
int newRange = range.Days;

if (newRange > 365)
{
    MessageBox.Show("Date is out of range.", "Validation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
Posted
Updated 15-May-14 23:21pm
v2
Comments
[no name] 16-May-14 5:19am    
can you show your code
Jamie888 16-May-14 5:21am    
yes sure
Prasad Khandekar 16-May-14 5:24am    
Hello,

Why not try using the MaskEdit control (http://msdn.microsoft.com/en-us/library/11405hcf(v=vs.71).aspx)

Regards,
Jamie888 16-May-14 5:28am    
Thk for everyone effort, I have found a solution. Using MaskedTextBox would solve this problem. Just drag and drop it and go to "Mask" under properties to alter the format.
[no name] 16-May-14 5:30am    
You are using windows applicaiton. See the solution is given below

You can use AjaxMaskExtnder for more you can refer :

Date MaskEdit ,

Example of MaskExtender
 
Share this answer
 
Set datetimepicker format from it's Properties
private void Form1_Load(object sender, EventArgs e)
      {
          dateTimePicker1.Format = DateTimePickerFormat.Custom;
          dateTimePicker1.CustomFormat = "yyyy-MM";
      }
 
Share this answer
 
v2

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