Reputation: 3
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
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
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
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
Reputation: 2212
When I convert a selected date in a calendar to a string I use this:
string dateTime= Calendar.SelectedDate.ToString();
Upvotes: 0