我是編程新手,不確定自己在做什麼。我想在c#中的WindowsForm中複製日曆。我開始使用TableLayoutPanel(6x7)。我在表格佈局面板的每個單元格中添加了一個常規面板。在常規面板中,我有一個標籤和一個列表框。我希望整天都在標籤中。我不想硬編碼它。我希望標籤在下個月移動時適當改變。我怎樣才能做到這一點?如何填充多個標籤?
這裏是佈局的畫面:
所以我在一組這樣做的,下面是我的組員之一想出了。除了2013年12月和2014年6月之外,它會很好地填充標籤。任何想法可能會導致這種情況?這是做事的好方法嗎?
` private void dtpMonthTab_ValueChanged(object sender, EventArgs e)
{
this.setCal();
}
private void setCal()
{
DateTime now = dtpMonthTab.Value;
int month = now.Month;
int year = now.Year;
DateTime startmonth = new DateTime(year, month, 1);
DayOfWeek dow = startmonth.DayOfWeek;
int daynumber = (int)dow;
//MessageBox.Show(" testing: " + dow + daynumber);
switch (daynumber)
{
case 0:
//MessageBox.Show("The number is zero!");
startmonth = startmonth.AddDays(-6);
label1.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label2.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label3.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label4.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label5.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label6.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label7.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
break;
case 1:
//MessageBox.Show("The number is one!");
startmonth = startmonth.AddDays(-1);
label1.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label2.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label3.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label4.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label5.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label6.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label7.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
break;
case 2:
//MessageBox.Show("The number is 2!");
startmonth = startmonth.AddDays(-2);
label1.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label2.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label3.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label4.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label5.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label6.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label7.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
break;
case 3:
//MessageBox.Show("The number is 3!");
startmonth = startmonth.AddDays(-3);
label1.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label2.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label3.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label4.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label5.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label6.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label7.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
break;
case 4:
//MessageBox.Show("The number is 4!");
startmonth = startmonth.AddDays(-4);
label1.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label2.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label3.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label4.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label5.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label6.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label7.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
break;
case 5:
//MessageBox.Show("The number is 5!");
startmonth = startmonth.AddDays(-5);
label1.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label2.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label3.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label4.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label5.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label6.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label7.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
break;
case 6:
//MessageBox.Show("The number is 6!");
startmonth = startmonth.AddDays(-6);
label1.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label2.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label3.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label4.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label5.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label6.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label7.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
break;
default:
MessageBox.Show("I'm sorry, I don't understand that!");
break;
}
label8.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label9.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label10.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label11.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label12.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label13.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label14.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label15.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label16.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label17.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label18.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label19.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label20.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label21.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label22.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label23.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label24.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label25.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label26.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label27.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label28.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label29.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label30.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label31.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label32.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label33.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label34.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label35.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label36.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label37.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label38.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label39.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label40.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label41.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
label42.Text = String.Format("{0:dd}", startmonth);
startmonth = startmonth.AddDays(1);
} `
您應該以編程方式創建標籤並將它們添加到父級控件。在一個循環中,您只需確定位置並將其添加到適當的數據結構(例如數組)中以便於使用。 – Matthias
我該怎麼做?父級控制檯面佈局面板還是常規面板? – Shades9323
如果沒有其他佈局要求,則根本不需要使用表格佈局。您只需使用兩個* for *循環(* x *和* y *),其中兩者一起表示父面板中的座標。另外,您根據* x *和* y *設置* label.Top *和* label.Left *。 – Matthias