【实验名称】
实验 5. 窗体界面日期是星期几面向对象程序设计
【实验要求】
实验 5. 使用窗体中最常用的文本框与按钮对象开发一个日期是星期几查询程序。
【实验目的】
实验 5. 掌握简单控件对象属性的设置与事件处理。
【实验内容】
实验 5. 窗体界面日期是星期几面向对象程序设计的图形界面如图所示:
(1)在 VS 开发环境中新建“Windows 窗体应用(.NET Framework)”,从菜单“项目”→
“添加 Windows 窗体”TestDate,使用工具箱设置窗体界面并设置控件属性。
(2)从菜单“项目”→“添加现有项”,添加实验4中定义的Date类的代码文件“Date.cs”。
(3)编写“星期几查询”按钮的单击事件代码,完成日期是星期几的计算并在 label 控
件中显示计算结果。
【代码实现】
body.cs
using ConsoleApp1;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class TestDate : Form
{
public TestDate()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int inyear, inmonth, inday;
try
{
inyear = Convert.ToInt32(textBox1.Text);
inmonth = Convert.ToInt32(textBox2.Text);
inday = Convert.ToInt32(textBox3.Text);
Data userinputdata = new Data(inyear, inmonth, inday);
if (userinputdata.elegeday())
{
string sesultstr = userinputdata.DisplayDate() + "是" + userinputdata.getweek();
label4.Text = sesultstr;
}
else
MessageBox.Show("您输入的年月日信息有误,请稍后再试。", "提示");
}
catch
{
MessageBox.Show("输入的信息有误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
body.Designer.cs
namespace WindowsFormsApp1
{
partial class TestDate
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if