2013-10-20 89 views
0

我是編程新手,不確定自己在做什麼。我想在c#中的WindowsForm中複製日曆。我開始使用TableLayoutPanel(6x7)。我在表格佈局面板的每個單元格中添加了一個常規面板。在常規面板中,我有一個標籤和一個列表框。我希望整天都在標籤中。我不想硬編碼它。我希望標籤在下個月移動時適當改變。我怎樣才能做到這一點?如何填充多個標籤?

這裏是佈局的畫面:

Example

所以我在一組這樣做的,下面是我的組員之一想出了。除了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); 
    } ` 
+0

您應該以編程方式創建標籤並將它們添加到父級控件。在一個循環中,您只需確定位置並將其添加到適當的數據結構(例如數組)中以便於使用。 – Matthias

+0

我該怎麼做?父級控制檯面佈局面板還是常規面板? – Shades9323

+0

如果沒有其他佈局要求,則根本不需要使用表格佈局。您只需使用兩個* for *循環(* x *和* y *),其中兩者一起表示父面板中的座標。另外,您根據* x *和* y *設置* label.Top *和* label.Left *。 – Matthias

回答

0

您可以使用TableLayoutPanel.GetControlFromPosition。一旦你把控制權轉移到你知道的地方(你說的「常規面板」)並相應地使用它。現在的問題是這些面板項目不公開其標籤+列表框內容,但Controls屬性以某種方式公開它們。我懷疑Controls[0]總會給出標籤,Controls[1]會給你列表框,但不確定。你將不得不施放他們。這取決於它們在每個單元格中創建的順序。

您應該製作自己的用戶控件,就像在Mr47的評論中一樣。您的用戶控件將是一個包含標籤和列表框的面板,然後您將不得不使用此新的用戶控件替換所有單元格中的所有面板。然後,您應該創建額外的屬性(標籤和列表框),以暴露標籤和列表框。這樣編程會更容易,並且代碼將更能抵抗錯誤。

此外,請注意,五個行不夠一個月,例如,如果這個月的第一天是星期六。

+0

供參考每個容器控件都有自己的Controls集合。在設置步驟中,您可以將對控件的引用存儲在按日期或月份的日期索引的(或兩個,一個用於文本,一個用於列表)集合中......然後,當您更新控件時,請使用集合引用。 – rheitzman

+0

@rheitzman如果我們讓Visual Studio的編輯器創建代碼,那麼編輯器將爲每個控件設置Controls屬性。如果OP去創建並將控件放置在他自己的代碼中,那麼我的答案是非常有用的。 – Dialecticus

+0

我的意思是把這個評論添加到OP - 對不起。 OPer可以在代碼中添加控件,併爲它們指定與要顯示日期相關的名稱,然後通過名稱索引它們: .Controls(「Oct_22_2013」​​) – rheitzman