user2217990
user2217990

Reputation: 3

selected date from calendar into text box

I am trying to put the date selected from a Calendar into a text box.

I keep getting the error message that a date can't be converted to a string.

I am very new to coding and can't figure out how to parse it properly so that it would work.

Can anyone help me? I am using Visual Studio 2010.

Upvotes: 0

Views: 41855

Answers (5)

Asher Faisal
Asher Faisal

Reputation: 23

If you are trying to put them in a variable then try it like this:

string date = Convert.ToString(Calendar1.SelectedDate);

TBDate.Text = date;

Upvotes: 0

jacob aloysious
jacob aloysious

Reputation: 2597

You cannot assign a TextBox object with a String. You can only get or set its Text property TxtTrvFrm.Text

From your example

Wrong:

TxtTrvFrm.ToString() = cdrDepart.SelectedDate

Correct:

TxtTrvFrm.Text = cdrDepart.SelectedDate.ToString();

You could also use ToString or Text to get the current selected value of datePicker.

        //Output: 3/28/2013 12:00:00 AM
        TxtTrvFrm.Text = this.datePicker1.ToString();

        //Output: 3/28/2013
        TxtTrvFrm.Text = this.datePicker1.Text;

        //Output: 3/28/2013 12:00:00 AM
        TxtTrvFrm.Text = this.datePicker1.SelectedDate.ToString();

Upvotes: 3

Jesson
Jesson

Reputation: 291

Try with this one its a lambda version

cdrDepart.SelectedDatesChanged += (a, b) =>
{
   TxtTrvFrm.text = cdrDepart.SelectedDate.Value.ToString("yy'.'MM'.'dd");
};

Upvotes: 0

Mike Schwartz
Mike Schwartz

Reputation: 2212

When I convert a selected date in a calendar to a string I use this:

string dateTime= Calendar.SelectedDate.ToString();

Upvotes: 0

AliK
AliK

Reputation: 1067

You would normally just call ToString(). Can you show your code?

Upvotes: 0

Related Questions