单击按钮时,仅更改日期时间选择器中的月份值 [英] Change only month value in datetime picker when a button is clicked

查看:94
本文介绍了单击按钮时,仅更改日期时间选择器中的月份值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个应用程序,其中将有12个按钮,每个按钮被命名为一年中的几个月。当我点击像11月这样的特定按钮时,datetimepicker中的月份值(dd-MM-yyy)MM值应该更改。我该如何实现?

请提供一些代码。我只需要改变一个月。不是日期或年份。在datetimepicker只有一个月。



我尝试过:



Dtp .Date.Value.Month = Convert.ToInt32(Btn_Nov.Text);

Hi I had an application in which there will be 12 buttons each named as months of an year. when I clicked on a specific button like November, the month value (dd-MM-yyy) MM value in datetimepicker should change. how can I achieve that ?
please suggest some code. I need to change only month. not date or year. only month in datetimepicker.

What I have tried:

Dtp.Date.Value.Month=Convert.ToInt32(Btn_Nov.Text);

推荐答案

DateTime value = Dtp.Date.Value;
Dtp.Date.Value = new DateTime(value.Year, theNewMonth, value.Day);


如果按钮有月份NAMES而不是月份NUMBERS,最简单的方法是用枚举来做:



If the buttons have month NAMES as opposed to month NUMBERS, the simplest way is to do it with an enum:

// example, if Btn_Nov.Text is "Nov", the following code would result in 11.
public enum Months { Jan = 1, Feb, Mar, Apr, May...,Nov,Dec};
Dtp.Date.Value.Month = (int)((Months)Enum.Parse(typeof(Months), Btn_Nov.Text, false));





当然,你的枚举应该匹配按钮文本所说的内容(将false传递给 Enum.Parse 使搜索不区分大小写。)



Of course, your enums should match whatever the button text says (passing false to Enum.Parse makes the search case-insensitive).


这篇关于单击按钮时,仅更改日期时间选择器中的月份值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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