在Datetimepicker中更改月份 [英] Change Month in Datetimepicker

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

问题描述

大家好



这里我希望根据月份更改日期时间选择器的月份,另外一个ComboBox控件可以使用12个月,所以哪个月用户选择我想在datetimepicker中更改那个月(windows应用程序)



谢谢

Hi All

here i want change month in datetime picker based on month, the total 12 month is available on another ComboBox control, so which month user choose i want to change that month in datetimepicker(windows application)

THANKS

推荐答案

你需要得到从组合中选择月份为整数值,并根据该值设置日期时间选择器值,例如,如果您选择第3个月

you need to get the selected month from your combo to a integer value and set the datetime picker value based on that, for example if you select 3rd month
int monthvalue =3;

dateTimePicker1.Value = new Date(dateTimePicker1.Value.Year, monthvalue, dateTimePicker1.Value.Day);





如果您需要更改datetimepicker日期,当您从组合框中选择月份时,则可以将上面的代码放在combobox selectedIndexChanged事件中。



将月份数据绑定到组合框,将值成员设置为整数并将成员显示为月份名称,然后您可以将选定的组合框值解析为整数值并将其设置为如上所述的日期时间选择器月。



if you need to change the datetimepicker date, when you select month from from combobox, then you can place above code in the combobox selectedIndexChanged event.

when you bind the month data to combobox, set value member as integer and display member as month name, then you can parse the selected value of combobox to a integer value and set it as datetime picker month as above.


DateTime monthSelect(int month)
        {
           return DateTime.Now.AddMonths(-DateTime.Now.Month).AddMonths(month+1);
        }

   private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            dateTimePicker1.Value=monthSelect(comboBox3.SelectedIndex);
        }







您可以使用此方法。




You can use this method.


使用MS帮助。



DateTime.AddMonths方法


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

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