سؤال

I've been trying to figuring out how to make both values match and to no avail I get non-matching times every time. I came very close when using this code:

this.dateOfBirthPicker.Value = DateTime.Now;

but it was off a second from matching each others' values. I looked around and noticed Date popping up frequently, but it said I'm missing an assembly reference, is it using System.DateTime? because I added that and still didn't work. I don't know what else to try and am sneaking help. DateTimePicker is a pain when I tried to change the time or remove the time and just stick with the date-only format.

If the user hasn't changed or set the DateOfBirthPicker which is set to DateTime.Now(Default), then the user will be asked to do so and that's all I'm trying to do.

C# Code:

DateTime dtN = DateTime.Now; // create and initialize DateTime.Now
        this.dateOfBirthPicker.Value = DateTime.Now; // set current time for DateTimePicker to match dtN
        DateTime date = dateOfBirthPicker.Value.Date; // ERROR: missing directive or assembly reference
        if(dateOfBirthPicker.Value == dtN)
        {
            dateOfBirthTBL.Text = "Date of Birth: *";
            dateOfBirthTBL.Foreground = new SolidColorBrush(Colors.Red);
            dateOfBirthTBL.FontWeight = FontWeights.Bold;
        }
هل كانت مفيدة؟

المحلول

By the sounds of your question, you are tying to implement a validator, if the user hasn't changed the value of dateOfBirthPicker, then highlight it in red?

Instead of setting:

this.dateOfBirthPicker.Value = DateTime.Now;

When you load the form set a global variable to DateTime.Now, set your datePicker value to this variable and use it to compare in your function above:

_dateNow = DateTime.Now;
this.dateOfBirthPicker.Value = _dateNow;

Then do the comparison like this:

if(dateOfBirthPicker.Value == _dateNow)
{
    dateOfBirthTBL.Text = "Date of Birth: *";
    dateOfBirthTBL.Foreground = new SolidColorBrush(Colors.Red);
    dateOfBirthTBL.FontWeight = FontWeights.Bold;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top