窗体的简单应用
编写一个闹钟程序,实现如下功能:1、设定时间为某时某分。2、设定闹钟的重复日期为:周一、周二、周三、周四、周五、周六、周日中的某几天。3、到达指定日期的闹钟时间,显示提示信息表示闹钟响了。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
StringBuilder msg = new StringBuilder("闹钟已设定:\n");
foreach (Control ctrl in groupBox2.Controls)
{
if (ctrl.GetType().Name == "CheckBox")
{
if (((CheckBox)ctrl).Checked)
{
msg.Append(((CheckBox)ctrl).Text + " ");
}
}
}
msg.Append("\n时间:\n" + tbHour.Text + ":" + tbMinute.Text);
MessageBox.Show(msg.ToString(), "闹钟", MessageBoxButtons.OK, MessageBoxIcon.Information);
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
int hour = now.Hour;
int minute = now.Minute;
int nhour = Convert.ToInt32(tbHour.Text);
int nminute = Convert.ToInt32(tbMinute.Text);
string dayOfWeek = now.DayOfWeek.ToString();
if (hour == nhour && minute == nminute)
{
timer1.Enabled = false;
MessageBox.Show("闹钟响了:" + dayOfWeek , "闹钟");
}
}
}
利用几个简单的控件即可实现简单的闹钟