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 s开发者_如何学Pythonign is it...
Like for example, from january first to february 14th you are this sign, from this to that you're... you know what I mean.
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 (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 AstralSign
{
...
Aries
...
}
There is no built-in functionality in DateTimePicker
for achieving this. 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 your DateTimePicker was called dateTimePicker, you could do it like this:
DateTime pickedDate = new DateTime(dateTimePicker.Value.Year, dateTimePicker.Value.Month, dateTimePicker.Value.Day);
And then you could use pickedDate as you want.
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
)
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)
The DateTimePicker
has a property 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.
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
selectedSign = astralSigns[pickedDate.Month][pickedDate.Day];
精彩评论