从Datetimepicker中仅选择月份和年份 [英] select only month and year from Datetimepicker

查看:546
本文介绍了从Datetimepicker中仅选择月份和年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从vb.net中的datetimepicker中仅选择月份和年份

推荐答案

日期选择器返回 DateTime 具有属性的对象。
Date pickers return a DateTime object which has Month and Year properties.


使用格式属性。

Use Format property.
dateTimePicker1.Format = DateTimePickerFormat.Custom
dateTimePicker1.CustomFormat = "MMMM yyyy" 'or whatever you want



参考: http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.format.aspx [< a href =http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.format.aspxtarget =_ blanktitle =New Window> ^ ]


解决方案2和3将崩溃OutOfBounds如果Value的日期大于Next / Previous月份的天数且用户使用箭头按钮更改月份,则为例外。例如,当值为10/31/2014并且控件显示2014年10月时。当​​您将月更改为9月时,值将设置为9/31/2014,这是无效日期并导致异常。



解决方法是

Solution 2 and 3 will crash with an OutOfBounds Exception if the Value has a date which is greater than the number of days in the Next/Previous month and the user changes month using the arrow buttons. For Example when the Value is 10/31/2014 and the control displays October, 2014. When you change the Month to September the Value is set to 9/31/2014 which is an invalid date and causes an exception.

A workaround is
Private Sub SalesDatePicker_ValueChanged(sender As Object, e As EventArgs) Handles SalesDatePicker.ValueChanged
    If SalesDatePicker.Value.Day <> 1 Then
        SalesDatePicker.Value = DateSerial(SalesDatePicker.Value.Year, SalesDatePicker.Value.Month, 1)
    End If
End Sub


这篇关于从Datetimepicker中仅选择月份和年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆