用C#设计一个简易时钟
因为我对C#的编程不太熟悉,但老师又要我们自学之后设计一个简易时钟,我大致看了一下C#的书太厚了,看到脑袋都昏了还是不明白。请各位高手帮帮忙!!!设计要求是:a)在界面上...
因为我对C#的编程不太熟悉,但老师又要我们自学之后设计一个简易时钟,我大致看了一下C#的书太厚了,看到脑袋都昏了还是不明白。请各位高手帮帮忙!!!设计要求是:a) 在界面上显示一个电子时钟(可以是数字的)。b) 可以设置电子时钟的日期和时间。
展开
1个回答
2013-12-05
展开全部
我设计了两个界面: 代码如下:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class MainFrm : Form
{
//计时功能,初始化系统时间
public int Hours { get; set; }
public int Minutes { get; set; }
public int Seconds { get; set; }
public string nowdate { get; set; } private void Ticking()
{
Seconds++;
if (Hours > 23)
{
Hours = 0;
Minutes = 0;
Seconds = 0;
}
else if (Minutes > 59)
{
Hours += 1;
Minutes = 0;
Seconds = 0;
}
else if (Seconds > 59)
{
Minutes += 1;
Seconds = 0;
}
} public MainFrm()
{
InitializeComponent();
} private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = "当前日期:" + nowdate + "\n";
label1.Text += string.Format("{0:0#}:{1:0#}:{2:0#}", Hours, Minutes, Seconds);
Ticking();
} private void button1_Click(object sender, EventArgs e)
{
(new SettingFrm(this)).ShowDialog();
} private void MainFrm_Load(object sender, EventArgs e)
{
Hours = DateTime.Now.Hour;
Minutes = DateTime.Now.Minute;
Seconds = DateTime.Now.Second;
nowdate = DateTime.Today.ToLongDateString();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class SettingFrm : Form
{
private MainFrm _mf = null; public SettingFrm(MainFrm mf):this()
{
_mf = mf;
timeBox.Text = mf.Hours + ":" + mf.Minutes + ":" + mf.Seconds;
dateTimePicker1.Text = mf.nowdate;
}
public SettingFrm()
{
InitializeComponent();
} private void SettingFrm_Load(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
_mf.nowdate = dateTimePicker1.Text;
string[] time = timeBox.Text.Split(':');
_mf.Hours = Convert.ToInt32(time[0]);
_mf.Minutes = Convert.ToInt32(time[1]);
_mf.Seconds = Convert.ToInt32(time[2]);
this.Close();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class MainFrm : Form
{
//计时功能,初始化系统时间
public int Hours { get; set; }
public int Minutes { get; set; }
public int Seconds { get; set; }
public string nowdate { get; set; } private void Ticking()
{
Seconds++;
if (Hours > 23)
{
Hours = 0;
Minutes = 0;
Seconds = 0;
}
else if (Minutes > 59)
{
Hours += 1;
Minutes = 0;
Seconds = 0;
}
else if (Seconds > 59)
{
Minutes += 1;
Seconds = 0;
}
} public MainFrm()
{
InitializeComponent();
} private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = "当前日期:" + nowdate + "\n";
label1.Text += string.Format("{0:0#}:{1:0#}:{2:0#}", Hours, Minutes, Seconds);
Ticking();
} private void button1_Click(object sender, EventArgs e)
{
(new SettingFrm(this)).ShowDialog();
} private void MainFrm_Load(object sender, EventArgs e)
{
Hours = DateTime.Now.Hour;
Minutes = DateTime.Now.Minute;
Seconds = DateTime.Now.Second;
nowdate = DateTime.Today.ToLongDateString();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class SettingFrm : Form
{
private MainFrm _mf = null; public SettingFrm(MainFrm mf):this()
{
_mf = mf;
timeBox.Text = mf.Hours + ":" + mf.Minutes + ":" + mf.Seconds;
dateTimePicker1.Text = mf.nowdate;
}
public SettingFrm()
{
InitializeComponent();
} private void SettingFrm_Load(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
_mf.nowdate = dateTimePicker1.Text;
string[] time = timeBox.Text.Split(':');
_mf.Hours = Convert.ToInt32(time[0]);
_mf.Minutes = Convert.ToInt32(time[1]);
_mf.Seconds = Convert.ToInt32(time[2]);
this.Close();
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询