none
Date Time Picker Value Changed Event RRS feed

  • Question

  • I have several date time pickers on a form that I would like to access the same value changed event and change the custom format as in the code below. If that is possible how could I do that? Thank you.

            private void dtpmi_date1_ValueChanged(object sender, EventArgs e)
            {
                dtpmi_date1.CustomFormat = "MM-dd-yyyy";
            }
    



    DrewT1755

    Monday, September 10, 2018 3:09 PM

Answers

  • In the designer.cs file or in the constructor you can subscribe all those datetime pickers with same event and alter those like:

    dtpmi_date1.ValueChanged += dtpmi_date1_ValueChanged;
    dtpmi_date2.ValueChanged += dtpmi_date1_ValueChanged;
     
    

    and then in your event method you can write the logic following way to change format :

    private void dtpmi_date1_ValueChanged(object sender, EventArgs e)
    {
        var picker = sender as DateTimePicker;
        picker.CustomFormat = "MM-dd-yyyy";
    }


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by DrewT1755 Monday, September 10, 2018 6:47 PM
    Monday, September 10, 2018 3:52 PM

All replies

  • In the designer.cs file or in the constructor you can subscribe all those datetime pickers with same event and alter those like:

    dtpmi_date1.ValueChanged += dtpmi_date1_ValueChanged;
    dtpmi_date2.ValueChanged += dtpmi_date1_ValueChanged;
     
    

    and then in your event method you can write the logic following way to change format :

    private void dtpmi_date1_ValueChanged(object sender, EventArgs e)
    {
        var picker = sender as DateTimePicker;
        picker.CustomFormat = "MM-dd-yyyy";
    }


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by DrewT1755 Monday, September 10, 2018 6:47 PM
    Monday, September 10, 2018 3:52 PM
  • Thank you.

    DrewT1755

    Monday, September 10, 2018 6:47 PM