calendar

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I get the date from a calendar and put it into a text box.

TxtDate.Text = Calendar1.SelectedDate.ToShortDateString();

But I would like to also do it the other way around. That is, convert the
textbox to a calendar date, so that the calendar (when you open it) has the
same value as the date in the textbox.

Thanks for your help

Chris
 
You could do:

Calendar1.SelectedDate = DateTime.Parse( TxtDate.Text );

But take care to catch possible exceptions.
 
Chris

When you say 'calendar' I have assumed you meant the
System.Windows.Forms.DateTimePicker control.

This code adds the selected date to the text box:

private void Calendar1_ValueChanged(object sender, System.EventArgs e)
{
textBox1.Text = Calendar1.Value.ToShortDateString();
}

but you knew that bit :)

To go the other way you need to put some code in the controls 'Enter' event:

private void Calendar1_Enter(object sender, System.EventArgs e)
{
try
{
// set the date to be the date in the text box
char[] sep = {'/'};
string[] tokens = textBox1.Text.Split(sep);
int day = int.Parse(tokens[0]);
int month = int.Parse(tokens[1]);
int year = int.Parse(tokens[2]);

DateTime date = new DateTime(year, month, day);
Calendar1.Value = date;
}
catch
{
Calendar1.Value = DateTime.Now;
}
}

This is pretty crude and will obviously only work with dates in the
format 'day/month/year' using a '/' as a seperator but it should give
you a good start.

Hope this helps :)

Martin
 
Does Microsoft ever think of the way to make datetime working better?
I always have different time each time I work with datetime.

Martin said:
Chris

When you say 'calendar' I have assumed you meant the
System.Windows.Forms.DateTimePicker control.

This code adds the selected date to the text box:

private void Calendar1_ValueChanged(object sender, System.EventArgs e)
{
textBox1.Text = Calendar1.Value.ToShortDateString();
}

but you knew that bit :)

To go the other way you need to put some code in the controls 'Enter'
event:

private void Calendar1_Enter(object sender, System.EventArgs e)
{
try
{
// set the date to be the date in the text box
char[] sep = {'/'};
string[] tokens = textBox1.Text.Split(sep);
int day = int.Parse(tokens[0]);
int month = int.Parse(tokens[1]);
int year = int.Parse(tokens[2]);

DateTime date = new DateTime(year, month, day);
Calendar1.Value = date;
}
catch
{
Calendar1.Value = DateTime.Now;
}
}

This is pretty crude and will obviously only work with dates in the format
'day/month/year' using a '/' as a seperator but it should give you a good
start.

Hope this helps :)

Martin

--------------------------------------
- Martin Stickley : MCP C#, ASP .NET -
--------------------------------------

Hi,

I get the date from a calendar and put it into a text box.

TxtDate.Text = Calendar1.SelectedDate.ToShortDateString();

But I would like to also do it the other way around. That is, convert the
textbox to a calendar date, so that the calendar (when you open it) has
the same value as the date in the textbox.

Thanks for your help

Chris
 
Back
Top