Marcus Dutton

Marcus Dutton

  • NA
  • 29
  • 5.3k

How to get Dates into Text Boxes from DateTimePicker

Jan 1 2016 9:44 PM
Happy New Year Everyone!
 
I am working on an application that I'm building.  I have currently a tablelayout control on my form, along with a date time picker set to select month and year only with up/down selector.
 
I have labels in my table that I would like to basically show the dates of each month as they would on a calendar in my labels.  Any idea how I would do this or do you recommend another approach.  I would like to do it this way because I have other cells in the tablelayout  control that will display collections based on the date.
 
 
Is there an easier way to write this code than in this manner?
 
if (scheduledatepicker.Value.DayOfWeek == DayOfWeek.Sunday)
{
clearDates();
lblW1D1.Text = scheduledatepicker.Value.AddDays(0).ToString("dd");
lblW1D2.Text = scheduledatepicker.Value.AddDays(1).ToString("dd");
lblW1D3.Text = scheduledatepicker.Value.AddDays(2).ToString("dd");
lblW1D4.Text = scheduledatepicker.Value.AddDays(3).ToString("dd");
lblW1D5.Text = scheduledatepicker.Value.AddDays(4).ToString("dd");
lblW1D6.Text = scheduledatepicker.Value.AddDays(5).ToString("dd");
lblW1D7.Text = scheduledatepicker.Value.AddDays(6).ToString("dd");
lblW2D1.Text = scheduledatepicker.Value.AddDays(7).ToString("dd");
lblW2D2.Text = scheduledatepicker.Value.AddDays(8).ToString("dd");
lblW2D3.Text = scheduledatepicker.Value.AddDays(9).ToString("dd");
lblW2D4.Text = scheduledatepicker.Value.AddDays(10).ToString("dd");
lblW2D5.Text = scheduledatepicker.Value.AddDays(11).ToString("dd");
lblW2D6.Text = scheduledatepicker.Value.AddDays(12).ToString("dd");
lblW2D7.Text = scheduledatepicker.Value.AddDays(13).ToString("dd");
lblW3D1.Text = scheduledatepicker.Value.AddDays(14).ToString("dd");
lblW3D2.Text = scheduledatepicker.Value.AddDays(15).ToString("dd");
lblW3D3.Text = scheduledatepicker.Value.AddDays(16).ToString("dd");
lblW3D4.Text = scheduledatepicker.Value.AddDays(17).ToString("dd");
lblW3D5.Text = scheduledatepicker.Value.AddDays(18).ToString("dd");
lblW3D6.Text = scheduledatepicker.Value.AddDays(19).ToString("dd");
lblW3D7.Text = scheduledatepicker.Value.AddDays(20).ToString("dd");

Or do I have to write all +/- 42 labels worth of code for each day?
 and I do not want to have the following months dates added to the labels after they hit the end of the month for the selected month in the datetimepicker names scheduledatepicker.
 
 

Answers (9)