C#简单日历 实验报告.docx
《C#简单日历 实验报告.docx》由会员分享,可在线阅读,更多相关《C#简单日历 实验报告.docx(17页珍藏版)》请在冰豆网上搜索。
C#简单日历实验报告
实验4简单日历
【实验目的】
⏹进一步实践windows窗口程序开发的流程;
⏹掌握并熟练使用后台代码添加控件并定义布局和格式以及交互的技能。
【实验环境】
VisualStudio2005(或更高版本)
【实验内容】
设计一个基于后台代码添加控件的方式来实现日历的显示和交互功能的Winform程序,鼓励扩展其他功能。
基本功能如下图:
【实验记录】
打开日历:
选择几个日期
选择一个年份
选择一个月份
点击下一月按钮“>”
点击上一月按钮“<”
点击今天按钮:
【核心代码】
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceCalendar
{
publicpartialclassForm1:
Form
{
privateComboBoxyear;
privateComboBoxmonth;
privateButtonlast;
privateButtonnext;
privateButtontoday;
privateDataGridViewcalendar;
privateDataGridViewTextBoxColumn[]Column;
privateLabeldate;
privateint[]mday=newint[]{31,28,31,30,31,30,31,31,30,31,30,31};
privateDateTimedt;
privatestring[]week=newstring[]{"日","一","二","三","四","五","六"};
publicForm1()
{
year=newComboBox();
month=newComboBox();
last=newButton();
next=newButton();
today=newButton();
calendar=newDataGridView();
Column=newDataGridViewTextBoxColumn[7];
date=newLabel();
date.AutoSize=true;
date.Location=newSystem.Drawing.Point(146,10);
date.Name="label1";
date.Size=newSystem.Drawing.Size(45,20);
date.TabIndex=1;
date.Text="";
last.Location=newPoint(225,50);
last.Name="last";
last.Size=newSize(20,20);
last.TabIndex=1;
last.Text="<";
last.UseVisualStyleBackColor=true;
last.Click+=newSystem.EventHandler(this.last_Click);
today.Location=newPoint(345,50);
today.Name="today";
today.Size=newSize(40,20);
today.TabIndex=1;
today.Text="今天";
today.UseVisualStyleBackColor=true;
today.Click+=newSystem.EventHandler(this.today_Click);
next.Location=newPoint(297,50);
next.Name="next";
next.Size=newSize(20,20);
next.TabIndex=1;
next.Text=">";
next.UseVisualStyleBackColor=true;
next.Click+=newSystem.EventHandler(this.next_Click);
year.FlatStyle=FlatStyle.Popup;
year.FormattingEnabled=true;
for(inti=1901;i<1901+150;i++)
year.Items.Add(i+"年");
year.SelectedIndex=DateTime.Now.Year-1901;
year.Location=newPoint(146,50);
year.Name="year";
year.Size=newSize(70,20);
year.TabIndex=0;
year.SelectedValueChanged+=newSystem.EventHandler(this.year_Changed);
month.FlatStyle=FlatStyle.Popup;
month.FormattingEnabled=true;
for(inti=1;i<13;i++)
month.Items.Add(i+"月");
month.SelectedIndex=DateTime.Now.Month-1;
month.Location=newPoint(246,50);
month.Name="year";
month.Size=newSize(50,20);
month.TabIndex=0;
month.SelectedValueChanged+=newSystem.EventHandler(this.month_Changed);
DataGridViewCellStyledataGridViewCellStyle1=newSystem.Windows.Forms.DataGridViewCellStyle();
dataGridViewCellStyle1.Alignment=System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
DataGridViewCellStyledataGridViewCellStyle2=newSystem.Windows.Forms.DataGridViewCellStyle();
dataGridViewCellStyle2.Alignment=System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
dataGridViewCellStyle2.ForeColor=Color.Red;
this.calendar.ColumnHeadersDefaultCellStyle=dataGridViewCellStyle1;
//DataGridViewColumn[]dc=newDataGridViewColumn[]{};
for(inti=0;i{
Column[i]=newSystem.Windows.Forms.DataGridViewTextBoxColumn();
Column[i].DefaultCellStyle=dataGridViewCellStyle1;
Column[i].FillWeight=65F;
Column[i].HeaderText=week[i];
Column[i].Name="Column"+i;
Column[i].Width=66;
Column[i].SortMode=DataGridViewColumnSortMode.NotSortable;
Column[i].Resizable=DataGridViewTriState.False;
if(i==0||i==6)
Column[i].DefaultCellStyle=dataGridViewCellStyle2;
calendar.Columns.Add(Column[i]);
}
calendar.ColumnHeadersHeightSizeMode=System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
calendar.Location=newSystem.Drawing.Point(8,80);
calendar.Name="calendar";
calendar.RowTemplate.Height=40;
calendar.RowHeadersVisible=false;
calendar.AllowUserToResizeRows=false;
calendar.MultiSelect=false;
calendar.Size=newSystem.Drawing.Size(464,259);
calendar.ScrollBars=ScrollBars.None;
calendar.TabIndex=0;
calendar.SelectionChanged+=newSystem.EventHandler(this.calendar_SelectionChanged);
for(inti=0;i<6;i++)
calendar.Rows.Add();
dt=DateTime.Now.Date;
calendarchange(dt);
Controls.Add(date);
Controls.Add(month);
Controls.Add(year);
Controls.Add(last);
Controls.Add(next);
Controls.Add(today);
Controls.Add(calendar);
InitializeComponent();
}
privatevoidForm1_Load(objectsender,EventArgse)
{
}
privatevoidlast_Click(objectsender,EventArgse)
{
if(month.SelectedIndex!
=0)
month.SelectedIndex--;
else
{
month.SelectedIndex=11;
year.SelectedIndex--;
}
}
privatevoidnext_Click(objectsender,EventArgse)
{
if(month.SelectedIndex!
=11)
month.SelectedIndex++;
else
{
month.SelectedIndex=0;
year.SelectedIndex++;
}
}
privatevoidtoday_Click(objectsender,EventArgse)
{
dt=DateTime.Now.Date;
year.SelectedIndex=dt.Year-1901;
month.SelectedIndex=dt.Month-1;
calendarchange(dt);
}
privatevoidyear_Changed(objectsender,EventArgse)
{
dt=newDateTime(year.SelectedIndex+1901,dt.Month,dt.Day);
calendarchange(dt);
}
privatevoidmonth_Changed(objectsender,EventArgse)
{
dt=newDateTime(dt.Year,month.SelectedIndex+1,dt.Day);
calendarchange(dt);
}
privatevoidcalendar_SelectionChanged(objectsender,EventArgse)
{
intmonth=dt.Month;
intyear=dt.Year;
if(calendar.SelectedCells[0].RowIndex==0&&Convert.ToInt32(calendar.SelectedCells[0].Value)>7)
month--;
if(calendar.SelectedCells[0].RowIndex>2&&Convert.ToInt32(calendar.SelectedCells[0].Value)<20)
month++;
if(month<=0)
{
month=12;
year--;
}
if(month>12)
{
month=1;
year++;
}
date.Font=newFont("",15);
date.Text=year+"年"+month+"月"+calendar.SelectedCells[0].Value
+"日星期"+week[calendar.SelectedCells[0].ColumnIndex];
}
privatevoidcalendarchange(DateTimedt)
{
DataGridViewCellStylestyle1=newDataGridViewCellStyle();
DataGridViewCellStylestyle2=newDataGridViewCellStyle();
DataGridViewCellStylestyle3=newDataGridViewCellStyle();
style1.BackColor=Color.Aqua;
style1.Font=newFont("",20);
style2.ForeColor=Color.Gray;
style3.Font=newFont("",20);
DateTimedt1=newDateTime(dt.Year,dt.Month,1);
intmw=Convert.ToInt32(dt1.DayOfWeek);
intday=0;
if(dt1.Year%4==0&&dt1.Year%100!
=0||dt1.Year%400==0)
mday[1]=29;
for(inth=0;h<6;h++)
{
for(intl=0;l<7;l++)
{
calendar.Rows[h].Cells[l].Value="";
calendar.Rows[h].Cells[l].Style=null;
}
}
for(inth=0;h<6;h++)
{
for(intl=0;l<7;l++)
{
if(l{
if(dt1.Month-2<0)
calendar.Rows[h].Cells[l].Value=mday[11]-mw+l+1;
else
calendar.Rows[h].Cells[l].Value=mday[dt1.Month-2]-mw+l+1;
calendar.Rows[h].Cells[l].Style=style2;
}
else
{
day++;
if(day>mday[dt1.Month-1])
{
calendar.Rows[h].Cells[l].Value=day-mday[dt1.Month-1];
calendar.Rows[h].Cells[l].Style=style2;
}
else
{
calendar.Rows[h].Cells[l].Value=day;
calendar.Rows[h].Cells[l].Style=style3;
}
}
if(day==DateTime.Now.Day&&dt1.Month==DateTime.Now.Month&&dt1.Year==DateTime.Now.Year)
{
calendar.Rows[h].Cells[l].Style=style1;
calendar.Rows[h].Cells[l].Selected=true;
}
}
}
if(!
dt.Date.Equals(DateTime.Now.Date))
calendar.Rows[0].Cells[mw].Selected=true;
}
}
}
注意:
以上是我们实验报告的主要内容
代码部分每位同学请保存在一个文本文档或一个文件夹内(以后的实验文件结构会比本次试验复杂时)
以后每次试验的每位同学都需要提交一个文件夹,该文件夹要求以“学号+姓名+实验n”命名如:
,顺序格式都不能乱
文件夹中需包含如下内容:
文件夹:
学号+姓名+实验n+实验源程序
实验报告:
学号+姓名+实验n+实验报告.doc
实验代码:
学号+姓名+实验n+实验核心代码.txt
如图所示:
顺序格式都不能乱!
!