从日历中选择日期到文本框

时间:2022-08-25 19:21:39

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.

谁能帮我?我正在使用Visual Studio 2010。

5 个解决方案

#1


2  

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

您不能使用String分配TextBox对象。您只能获取或设置其Text属性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.

您还可以使用ToString或Text来获取当前所选的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();

#2


0  

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

你通常只需要调用ToString()。你能展示你的代码吗?

#3


0  

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

当我将日历中的选定日期转换为字符串时,我使用:

string dateTime= Calendar.SelectedDate.ToString();

#4


0  

Try with this one its a lambda version

尝试使用这个lambda版本

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

#5


0  

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

如果您尝试将它们放入变量中,请尝试这样:

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

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

TBDate.Text = date;

TBDate.Text =日期;

#1


2  

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

您不能使用String分配TextBox对象。您只能获取或设置其Text属性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.

您还可以使用ToString或Text来获取当前所选的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();

#2


0  

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

你通常只需要调用ToString()。你能展示你的代码吗?

#3


0  

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

当我将日历中的选定日期转换为字符串时,我使用:

string dateTime= Calendar.SelectedDate.ToString();

#4


0  

Try with this one its a lambda version

尝试使用这个lambda版本

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

#5


0  

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

如果您尝试将它们放入变量中,请尝试这样:

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

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

TBDate.Text = date;

TBDate.Text =日期;