c# winform显示当前时间
时间: 2023-08-24 21:05:34 浏览: 223
要在C# Winform中显示当前时间,你可以使用Timer控件和Label控件。首先,在窗体加载时,你可以使用以下代码将当前日期和星期转换为中文,并将其保存在一个变量中\[2\]:
```csharp
string weekstr = "";
private void Form_Load(object sender, EventArgs e)
{
// 将定时器启动
timer1.Start();
// 将得到的星期转换成中文
switch (DateTime.Now.DayOfWeek.ToString())
{
case "Monday":
weekstr = "星期一";
break;
case "Tuesday":
weekstr = "星期二";
break;
case "Wednesday":
weekstr = "星期三";
break;
case "Thursday":
weekstr = "星期四";
break;
case "Friday":
weekstr = "星期五";
break;
case "Saturday":
weekstr = "星期六";
break;
case "Sunday":
weekstr = "星期日";
break;
}
}
```
然后,你可以使用Timer控件的Tick事件来更新Label控件的文本,以显示当前时间和星期\[2\]:
```csharp
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = "当前时间:" + DateTime.Now.ToString() + " " + weekstr;
}
```
确保在窗体中拖放一个Timer控件,并将其Enable属性设置为true,Interval属性设置为1000(1秒)\[2\]。这样,每秒钟Timer控件的Tick事件就会触发一次,更新Label控件的文本,从而显示当前时间和星期。
#### 引用[.reference_title]
- *1* [C#winform【在状态栏显示实时时间】--实战练习一](https://blog.csdn.net/BoomBiuBiu/article/details/122616095)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [C#在Winform中显示当前日期、时间、星期](https://blog.csdn.net/whb78952/article/details/124712447)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文