calendar

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chris

    calendar

    Hi,

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

    TxtDate.Text = Calendar1.Selec tedDate.ToShort DateString();

    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
  • Mohammad

    #2
    Re: calendar

    You could do:

    Calendar1.Selec tedDate = DateTime.Parse( TxtDate.Text );

    But take care to catch possible exceptions.

    Comment

    • Mohammad

      #3
      Re: calendar

      You could do:

      Calendar1.Selec tedDate = DateTime.Parse( TxtDate.Text );

      But take care to catch possible exceptions.

      Comment

      • Martin

        #4
        Re: calendar

        Chris

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

        This code adds the selected date to the text box:

        private void Calendar1_Value Changed(object sender, System.EventArg s e)
        {
        textBox1.Text = Calendar1.Value .ToShortDateStr ing();
        }

        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.EventArg s e)
        {
        try
        {
        // set the date to be the date in the text box
        char[] sep = {'/'};
        string[] tokens = textBox1.Text.S plit(sep);
        int day = int.Parse(token s[0]);
        int month = int.Parse(token s[1]);
        int year = int.Parse(token s[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:[color=blue]
        > Hi,
        >
        > I get the date from a calendar and put it into a text box.
        >
        > TxtDate.Text = Calendar1.Selec tedDate.ToShort DateString();
        >
        > 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[/color]

        Comment

        • kids_pro

          #5
          Re: calendar

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

          "Martin" <martin.stickle y@btinternet.co m> wrote in message
          news:d27gms$hho $1@sparta.btint ernet.com...[color=blue]
          > Chris
          >
          > When you say 'calendar' I have assumed you meant the
          > System.Windows. Forms.DateTimeP icker control.
          >
          > This code adds the selected date to the text box:
          >
          > private void Calendar1_Value Changed(object sender, System.EventArg s e)
          > {
          > textBox1.Text = Calendar1.Value .ToShortDateStr ing();
          > }
          >
          > 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.EventArg s e)
          > {
          > try
          > {
          > // set the date to be the date in the text box
          > char[] sep = {'/'};
          > string[] tokens = textBox1.Text.S plit(sep);
          > int day = int.Parse(token s[0]);
          > int month = int.Parse(token s[1]);
          > int year = int.Parse(token s[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:[color=green]
          >> Hi,
          >>
          >> I get the date from a calendar and put it into a text box.
          >>
          >> TxtDate.Text = Calendar1.Selec tedDate.ToShort DateString();
          >>
          >> 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[/color][/color]


          Comment

          Working...