我是编程新手,并不确定我在做什么。我试图在c#中的WindowsForm中复制日历。我开始使用TableLayoutPanel(6x7)。我在表格布局面板的每个单元格中添加了一个常规面板。在常规面板中,我有一个标签和一个列表框。我希望int day成为标签。我不想硬编码。当我搬到下个月时,我希望标签能够适当改变。我怎么能做到这一点?
以下是布局图:
所以我在一个小组中这样做,以下是我的一个小组成员想出来的。除了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 :(得分:0)
您可以使用TableLayoutPanel.GetControlFromPosition。一旦你有控件将它转换为你所知道的(你说“常规面板”)并相应地使用它。现在的问题是那些Panel项目没有公开它们的标签+列表框内容,但是Controls属性正在以某种方式暴露它们。我怀疑Controls[0]
总会给出标签,Controls[1]
会给你列表框,但不确定。你必须投他们。这取决于它们在每个单元格中创建的顺序。
您应该自己设置用户控件,就像Mr47的评论一样。您的用户控件将是一个带有标签和列表框的面板,然后您必须使用此新用户控件替换所有单元格中的所有面板。然后,您应该创建将公开标签和列表框的其他属性(Label和ListBox)。这样编程会更容易,而且代码可以更好地抵御错误。
另外,请注意,如果一个月的第一天是星期六,那么一个月的五行是不够的。