简体   繁体   English

C#,如何在if语句上使用dateTimePicker的值?

[英]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部分与在DatePickerdateTimePicker1.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.

相关问题 如何通过单击C#中的datagridview单元格将值插入datetimepicker? - How do i insert value to datetimepicker by clicking datagridview cells in C#? c#datetimepicker:我如何获得格式的日期? 1/1/2001 - c# datetimepicker : how do i get the date in the format? 1/1/2001 如何获取使用顶级语句的 C# 9 程序的反射类型信息? - How do I get the Reflection TypeInfo of a C# 9 program that use Top-level statements? Oracle / C#:如何将绑定变量与select语句一起使用以返回多个记录? - Oracle/c#: How do i use bind variables with select statements to return multiple records? 如何使用XPath在C#中获取属性的值? - How do I use XPath to get the value of an attribute in c#? 如果需要在INSERT调用之间进行SELECT调用,如何在C#中将准备好的语句与SQLite一起使用? - How do I use prepared statements with SQLite in C# if I need to do a SELECT call between INSERT calls? 我如何获得这些if语句以在C#中很好地播放 - How do I get these if statements to play nicely in c# 如何将if语句存储为字符串? C# - How do I store if statements as strings? C# 如何正确使用 c# 中的更新语句 - How can i use update statements in c# correctly 如何在 C# 中插入“if”语句? [等候接听] - How do I thread in “if ” statements in C#? [on hold]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM