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?
C#
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");Code
Code
Or do I have to write all +/- 42 labels worth of code for each day?
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Jan 2, 2016, 2:06 PM EST
for(int i=1;i<=3;i++)
{
for(j=1;j<=7;j++)
{
lbl=(Label)Page.FindControl("Label" + i.ToString() + j.ToString());
lbl.Text=scheduledatepicker.Value.AddDays(i+j-2);
}
}
Marcus Dutton29 reputationPosted Jan 2, 2016, 2:19 PM EST
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Jan 2, 2016, 2:52 PM EST
int add=0;
for(int i=1;i<=3;i++)
{
for(j=1;j<=7;j++)
{
lbl=(Label)Page.FindControl("LabelW" + i.ToString() +"D"+ j.ToString());
lbl.Text=scheduledatepicker.Value.AddDays(add);
add++;
}
}
lbl=(Label)Page.FindControl("LabelW" + i.ToString() + "D"+j.ToString()); if value of i=1 and j=1 then lbl represent the "LabelW1D1" so using value of add=0; lbl.Text=scheduledatepicker.Value.AddDays(add) we assign the text to LabelW1D1so on
when i=1 and j=2
now add=1;
when i=1 and j=3
Please reply me if i got your question wrong or this code is not as you expect...
Marcus Dutton29 reputationPosted Jan 2, 2016, 3:00 PM EST
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Jan 2, 2016, 3:13 PM EST
Marcus Dutton29 reputationPosted Jan 2, 2016, 3:29 PM EST
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Jan 2, 2016, 3:41 PM EST
Marcus Dutton29 reputationPosted Jan 3, 2016, 10:13 PM EST
Marcus Dutton29 reputationPosted Jan 7, 2016, 11:04 PM EST
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts