简体   繁体   中英

How to set the correct validation for DatePicker more than once in C#?

I'm having validation problems with the Date of Birth. I debugged it by not filling in all fields and then I noticed 'dateOfBirthPicker_ValueChanged' will still be invoked due to the this.dateOfBirthPicker = dtN, occurring programmatically. When I do actually use the 'dateofBirthPicker', the 'dateOfBirthTBL' Textblock is still red, bold, and cotain an asterisk. As, in the if-statement, I want to remove it back to default after the user has clicked on, opened and modified the datePicker, but I'm not sure how to do this logically.

Also how can I restrict the max date for the datePicker because every time I try to use one of those date methods, it gives me a missing directive or assembly reference. I don't know which class or package to be able to use it.

dateOfBirthPicker.DisplayDateEnd = DateTime.Now;

C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Windows.Media; // added to support SolidColorBrush, FontWeights, etc...
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Data_Query.Resources;
using System.Windows.Input;

namespace Data_Query
{
public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    DateTime dtN; // var to set current time
    bool clicked = false; // returns true if the 'fingerprintB' Button or DateTimePicker was clicked at least once; otherwise false.

    // user's date of birth by using a date picker
    private void dateOfBirthPicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
    {
        if(clicked) return;
        //MessageBox.Show("You are in the DateTimePicker.ValueChanged event.");
    }

    // validates first, beforing sending the user's information into a database query 
    private void submitButton(object sender, RoutedEventArgs e)
    {
        //------------------------------------FIRST NAME-------------------------------//
        if(string.IsNullOrWhiteSpace(firstNameTB.Text) || firstNameTB.Text == "")
        {
            firstNameTBL.Text = "First Name: *";
            firstNameTBL.Foreground = new SolidColorBrush(Colors.Red);
            firstNameTBL.FontWeight = FontWeights.Bold;
        }
        else if(!(string.IsNullOrWhiteSpace(firstNameTB.Text) || firstNameTB.Text == ""))
        {
            // set back to default layout
            this.firstNameTBL.ClearValue(TextBlock.ForegroundProperty);
            this.firstNameTBL.ClearValue(TextBlock.FontWeightProperty);
            this.firstNameTBL.Text = "First Name:";
        }
        //------------------------------------LAST NAME-------------------------------//
        if(string.IsNullOrWhiteSpace(lastNameTB.Text) || lastNameTB.Text == "")
        {
            lastNameTBL.Text = "Last Name: *";
            lastNameTBL.Foreground = new SolidColorBrush(Colors.Red);
            lastNameTBL.FontWeight = FontWeights.Bold;
        }
        else if(!(string.IsNullOrWhiteSpace(lastNameTB.Text) || lastNameTB.Text == ""))
        {
            // set back to default layout
            this.lastNameTBL.ClearValue(TextBlock.ForegroundProperty);
            this.lastNameTBL.ClearValue(TextBlock.FontWeightProperty);
            this.lastNameTBL.Text = "Last Name:";
        }
        //------------------------------------EMAIL ADDRESS-------------------------------//
        if(string.IsNullOrWhiteSpace(emailAddressTB.Text) || emailAddressTB.Text == "")
        {
            emailAddressTBL.Text = "Email Address: *";
            emailAddressTBL.Foreground = new SolidColorBrush(Colors.Red);
            emailAddressTBL.FontWeight = FontWeights.Bold;
        }
        else if (!(string.IsNullOrWhiteSpace(emailAddressTB.Text) || emailAddressTB.Text == ""))
        {
            // set back to default layout
            this.emailAddressTBL.ClearValue(TextBlock.ForegroundProperty);
            this.emailAddressTBL.ClearValue(TextBlock.FontWeightProperty);
            this.emailAddressTBL.Text = "Email Address:";
        }
        //------------------------------------DATE OF BIRTH-------------------------------//
        //if()
        //dateOfBirthPicker.DisplayDateEnd = DateTime.Now;

        clicked = true; // prevent the validation code from running
        dtN = DateTime.Now; // initialize DateTime.Now to be use in value comparsion with DateTimePicker
        this.dateOfBirthPicker.Value = dtN; // set current time for DateTimePicker to match dtN
        clicked = false; // allow it to run again
        if(dateOfBirthPicker.Value == dtN) // true when user hasn't changed or set the DateTimePicker
        {
            dateOfBirthTBL.Text = "Date of Birth: *";
            dateOfBirthTBL.Foreground = new SolidColorBrush(Colors.Red);
            dateOfBirthTBL.FontWeight = FontWeights.Bold;
        }
        else if(clicked)
        {
            // set back to default layout
            this.dateOfBirthTBL.ClearValue(TextBlock.ForegroundProperty);
            this.dateOfBirthTBL.ClearValue(TextBlock.FontWeightProperty);
            this.dateOfBirthTBL.Text = "Date of Birth:";
        }
        //------------------------------------GENDER-------------------------------//
        if(maleRB.IsChecked == false && femaleRB.IsChecked == false)
        {
            genderTBL.Text = "Gender: *";
            genderTBL.Foreground = new SolidColorBrush(Colors.Red);
            genderTBL.FontWeight = FontWeights.Bold;
        }
        else if(!(maleRB.IsChecked == false && femaleRB.IsChecked == false))
        {
            // set back to default layout
            this.genderTBL.ClearValue(TextBlock.ForegroundProperty);
            this.genderTBL.ClearValue(TextBlock.FontWeightProperty);
            this.genderTBL.Text = "Gender:";
        }
        //------------------------------------FINGERPRINT-------------------------------//
        if(!clicked)
        {
            MessageBox.Show("Fingerprint Scan was not completed! \nPlease scan your fingerprint until the progress bar is full!", "Notice!", MessageBoxButton.OK);
            fingerprintTBL.Text = "Fingerprint: *";
            fingerprintTBL.Foreground = new SolidColorBrush(Colors.Red);
            fingerprintTBL.FontWeight = FontWeights.Bold;
        }
        else if(clicked)
        {
            // set back to default layout
            this.fingerprintTBL.ClearValue(TextBlock.ForegroundProperty);
            this.fingerprintTBL.ClearValue(TextBlock.FontWeightProperty);
            this.fingerprintTBL.Text = "Fingerprint:";
        }

    }

    // clear all user's information inputs
    private void resetButton(object sender, RoutedEventArgs e)
    {
        firstNameTB.Text = string.Empty;
        lastNameTB.Text = string.Empty;
        emailAddressTB.Text = string.Empty;
        dateOfBirthPicker.Value = DateTime.Now;
        maleRB.IsChecked = false;
        femaleRB.IsChecked = false;
        clicked = false;
    }

    // supply a method to allow you to use the SIP Enter key to dismiss the SIP
    private void TextBox_KeyUp(object sender, KeyEventArgs e)
    {
        if(e.Key == Key.Enter)
        {
            this.Focus();
        }
    }
}

}

You could use a Boolean flag. For example:

var manualDateChange = false;

void datepicker_valuechange(object sender, DateTimeValueChangedEventArgs e)
{
    if(manualDateChange) return;

    //Do your validation here
}

void someFunctionThatChangesTheDate()
{
    manualDateChange = true; //prevent the validation code from running
    datepicker.Value = DateTime.Now;
    manualDateChange = false; //allow it to run again
}

Note: if you are concerned that changing manualDateChange will allow the user to change it without validation, remember that all event handling code is executed on the main thread and that while the code is running, the user cannot interact with any user interface elements. So as long as you set the flag, set the value, and reset the flag, its safe.

EDIT: If all you want to do is set the max date, you want to set DatePicker.MaxYear since that's the only real option you have in Windows Phone 8. http://msdn.microsoft.com/en-US/library/windows/apps/windows.ui.xaml.controls.datepicker.maxyear

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM