Home > Net >  When month picker value is changed my day picker value will change in number picker using xamarin C#
When month picker value is changed my day picker value will change in number picker using xamarin C#

Time:05-18

This is my code, I would like when month value is changed my day picker value will change. Day value is depends on month. Like, if month value is 1,3,5,7,8,10,12 then day value will 31 otherwise day value 30. If month is feb then day value will 28 and will depends on leap years.

I'm using XAMARIN with C#.

Thank you!

public class DayMonthYearPickerDialog : Android.Support.V4.App.DialogFragment
{
    public event EventHandler<DateTime> OnDateTimeChanged;
    public event EventHandler<DateTime> OnClosed;

    public DateTime? Date { get; set; }
    public void Hide() => base.Dialog?.Hide();

    public override Dialog OnCreateDialog(Bundle savedInstanceState)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(Activity);
        // Get the layout inflater
        LayoutInflater inflater = Activity.LayoutInflater;
        var selectedDate = GetSelectedDate();

        Calendar cal = Calendar.Instance;

        View dialog = inflater.Inflate(Resource.Layout.date_picker_dialog, null);
        NumberPicker monthPicker = (NumberPicker)dialog.FindViewById(Resource.Id.picker_month);
        NumberPicker yearPicker = (NumberPicker)dialog.FindViewById(Resource.Id.picker_year);
        NumberPicker dayPicker = (NumberPicker)dialog.FindViewById(Resource.Id.picker_day);

        monthPicker.MinValue = 1;
        monthPicker.MaxValue = 12;
        monthPicker.Value = cal.Get(CalendarField.Month)   1;


        dayPicker.MinValue = 1;
        dayPicker.MaxValue = 31;
        dayPicker.Value = cal.Get(CalendarField.DayOfMonth);

        int year = cal.Get(CalendarField.Year);
        yearPicker.MinValue = 1900;
        yearPicker.MaxValue = year;
        yearPicker.Value = year;

        builder.SetView(dialog)
            .SetPositiveButton("Ok", (sender, e) =>
            {
                selectedDate = new DateTime(yearPicker.Value, monthPicker.Value, dayPicker.Value);

                OnDateTimeChanged?.Invoke(dialog, selectedDate);
            })
            .SetNegativeButton("Cancel", (sender, e) =>
            {
                Dialog.Cancel();
                OnClosed?.Invoke(dialog, selectedDate);
            }); return builder.Create();

CodePudding user response:

You can add a ValueChanged event to the monthPicker. Such as:

 var month = new List<int>() {1,3,5,7,8,10,12 };
           
            monthPicker.ValueChanged  = (s, e) =>
            {
                if(month.Contains(monthPicker.Value))
                {
                    dayPicker.MaxValue = 31;
                }else if(monthPicker.Value == 2 && yearPicker.Value %4 == 0)
                {
                    dayPicker.MaxValue = 28;
                }else if(monthPicker.Value == 2 && yearPicker.Value % 4 != 0)
                {
                    dayPicker.MaxValue = 29;
                }
                else
                {
                    dayPicker.MaxValue = 30;
                }
            };
  • Related