vb.net 如何更改/设置 DateTimePicker 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15082664/
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-09-09 16:42:17  来源:igfitidea点击:

How to change/set DateTimePicker value

vb.netdatetimepicker

提问by Aaron

I am trying to set / change the DateTimePicker value (using vb.net) but can't work out how to do it.

我正在尝试设置/更改 DateTimePicker 值(使用 vb.net),但不知道该怎么做。

I have added the control to the page and I have tried using the following code but it doesn't work and I can't work out how to set the value during run-time.

我已将控件添加到页面,并尝试使用以下代码,但它不起作用,我无法弄清楚如何在运行时设置该值。

DateTimePicker1.Value = Now.Day & "-" & Now.Month & "-" & Now.Year

The format of the control is set to Long and it looks like this when first loaded:

控件的格式设置为 Long,首次加载时如下所示:

Tuesday, February 26, 2013

2013 年 2 月 26 日,星期二

But I can't work out how to change it.

但我不知道如何改变它。

The error I get based on my code above is:

我根据上面的代码得到的错误是:

Conversion from string "26-2-2013" to type 'Date' is not valid.

从字符串“26-2-2013”​​到类型“Date”的转换无效。

Anyone got any ideas ?

任何人有任何想法?

回答by Aaron

I ended up getting it working by doing the following:

我最终通过执行以下操作使其工作:

DateTimePicker1.Value = New Date(2013, 2, 26)

I was loading the value wrong.

我加载的值错误。

回答by Daniel Smith

Add the following code on Form Load Event to set the date time picker value to today:

在 Form Load Event 上添加以下代码以将日期时间选择器值设置为今天:

DateTimePicker1.CustomFormat="dd-MM-yyyy"
DateTimePicker1.Value=Now()