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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15672572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:32:05  来源:igfitidea点击:

selected date from calendar into text box

c#asp.net

提问by user2217990

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。

采纳答案by jacob aloysious

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

不能使用字符串分配 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();

回答by AliK

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

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

回答by Mike Schwartz

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

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

string dateTime= Calendar.SelectedDate.ToString();

回答by Jesson

Try with this one its a lambda version

尝试使用它的 lambda 版本

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

回答by Asher Faisal

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

如果你想把它们放在一个变量中,那么试试这样:

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

TBDate.Text = date;

字符串日期 = Convert.ToString(Calendar1.SelectedDate);

TBDate.Text = 日期;