Home All Groups Group Topic Archive Search About
Author
26 Mar 2005 4:13 AM
chris
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

Author
26 Mar 2005 10:46 AM
Mohammad
You could do:

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

But take care to catch possible exceptions.
Author
27 Mar 2005 11:47 PM
Martin
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 -
--------------------------------------


chris wrote:
Show quote
> 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
Author
28 Mar 2005 12:50 AM
kids_pro
Does Microsoft ever think of the way to make datetime working better?
I always have different time each time I work with datetime.

Show quote
"Martin" <martin.stick***@btinternet.com> wrote in message
news:d27gms$hho$1@sparta.btinternet.com...
> 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 -
> --------------------------------------
>
>
> chris wrote:
>> 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

AddThis Social Bookmark Button