[英]C#, how do I use a dateTimePicker's value on if statements?
The work I'm doing consists on finding out one's astral sign, so I put a dateTimePicker to select the date of birth, but now I'm not sure how I will get the data from that dateTimePicker to check which sign is it... 我正在做的工作是找出一个人的星号,所以我放了一个dateTimePicker来选择出生日期,但是现在我不确定如何从该dateTimePicker中获取数据来检查它是哪个星座。 。
Like for example, from january first to february 14th you are this sign, from this to that you're... you know what I mean. 例如,从1月1日到2月14日,您就是这个标志,从这个标志到您就是...您知道我的意思。
You are not concerned about the year. 您不必担心年份。 You just need the month and day values:
您只需要月份和日期值:
DateTimePicker dtp = new DateTimePicker();
int month = dtp.Value.Month;
int day = dtp.Value.Day;
Then, you can do if-else
statements around the various astral signs: 然后,您可以围绕各种星号进行
if-else
语句:
if (month >= 3 && day >= 20 && month < 4 && day <21 )
{
return AstralSign.Aries;
}
else if (/* some other sign */)
{
// ...
}
You can create an enum
for the signs as well: 您还可以为这些标志创建一个
enum
:
enum AstralSign
{
...
Aries
...
}
The DateTimePicker
has a property Value
. DateTimePicker
具有属性Value
。 This contains the selected date by the user. 它包含用户选择的日期。
If you like to get informed when the user made his selection, simply subscribe to the ValueChanged
property. 如果您希望在用户进行选择时得到通知,只需订阅
ValueChanged
属性。
Afterwards simply compare if the the given date falls within the range of any astral sign. 然后,只需比较给定的日期是否在任何星号的范围内。
To get the astral sign i would built up a nested dictionary Dictionary<int, Dictionary<int, AstralSign>>
which could then be access by 为了得到星号,我将建立一个嵌套的字典
Dictionary<int, Dictionary<int, AstralSign>>
,然后可以通过
selectedSign = astralSigns[pickedDate.Month][pickedDate.Day];
There is no built-in functionality in DateTimePicker
for achieving this. DateTimePicker
没有内置功能可实现此目的。 You will have to either go for if-else
approach or a switch
statement to find out which sign a particular date belongs to.. 您将不得不采用
if-else
方法或使用switch
语句来找出特定日期属于哪个符号。
If your DateTimePicker was called dateTimePicker, you could do it like this: 如果您的DateTimePicker称为dateTimePicker,则可以这样操作:
DateTime pickedDate = new DateTime(dateTimePicker.Value.Year, dateTimePicker.Value.Month, dateTimePicker.Value.Day);
And then you could use pickedDate as you want. 然后,您可以根据需要使用pickedDate。
There is nothing in built in for Zodiac signs , so i would recommend you building your own list for the Zodiac signs. 黄道十二宫没有内置任何内容,因此我建议您为黄道十二宫建立自己的列表。
Comparing a date falls in which zodia part you can go along with DayOfYear
part of the Datetime value
selected in the DatePicker
( dateTimePicker1.Value
) 比较一个日期,您可以将
DayOfYear
部分与在DatePicker
( dateTimePicker1.Value
)中选择的Datetime value
DayOfYear
部分一起使用
So 0 to 46 could be your first zodiac and compare this with the DayOfYear
part and you will have your Zodiac sign (else year would be a problem while comparing DateTime
objects) 所以0到46可能是您的第一个十二生肖,并将其与
DayOfYear
部分进行比较,您将拥有十二生肖(否则,在比较DateTime
对象时,年将是一个问题)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.