Search code examples
c#winformsmonthcalendar

Use One Month Calendar to Populate 2 Text Boxes


I was wondering if you could use one month calendar to populate 2 text boxes on a windows form. One for the beginning date and one for the end date. I can figure out how to populate both text boxes with the click on the calendar but now how to populate start date with click 1, clear the calendar, then populate end date with the 2nd calendar click.

Is this possible, and if so how would I achieve this in C# windows form?


Solution

  • You can handle the "Date Selected" event as seen below:

        public Form1()
        {
            InitializeComponent();
            mthCalendarMaster.DateSelected += mthCalendarMaster_DateSelected;
        }
    
        private void mthCalendarMaster_DateSelected(object sender, DateRangeEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textBox1.Text))
                textBox1.Text = e.Start.ToShortDateString();
            else
                textBox2.Text = e.Start.ToShortDateString();
        }
    

    Note: It is not the greatest idea to define your event handlers in the form's constructor.