C# 简单数字时钟

在本教程中,我们将使用 C# 创建一个简单的数字时钟。C# 是一种通用的面向对象编程语言。C# 使用垃圾收集器自动管理不可访问的对象内存,从而消除了开发人员的顾虑和内存泄漏。它旨在提高 Web 应用程序的开发效率。它为所有新开发人员提供了友好的环境。那么让我们开始编码吧......

示例源码:https://download.csdn.net/download/hefeng_aspnet/90448004

入门

首先,您必须下载并安装 Visual Studio。Visual Studios 是一个开源开发工具,您可以随意创建任何您想要的应用程序。这是 Visual Studio 的链接https://www.visualstudio.com/

应用程序设计

我们现在将创建应用程序的设计,首先找到名为form1.Designer.cs的设计器文件,这是创建新 Windows 窗体时的默认名称。将窗体重命名为Main.cs,然后在设计器文件中写入这些代码。

namespace Simple_Digital_Clock

{

partial class Main

{

/// <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 (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

this.lbl_time = new System.Windows.Forms.Label();

this.lbl_sec = new System.Windows.Forms.Label();

this.lbl_day = new System.Windows.Forms.Label();

this.timer1 = new System.Windows.Forms.Timer(this.components);

this.lbl_date = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// lbl_time

//

this.lbl_time.AutoSize = true;

this.lbl_time.Font = new System.Drawing.Font("DS-Digital", 72F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

this.lbl_time.ForeColor = System.Drawing.Color.Green;

this.lbl_time.Location = new System.Drawing.Point(88, 76);

this.lbl_time.Name = "lbl_time";

this.lbl_time.Size = new System.Drawing.Size(376, 142);

this.lbl_time.TabIndex = 0;

this.lbl_time.Text = "00:00";

//

// lbl_sec

//

this.lbl_sec.AutoSize = true;

this.lbl_sec.Font = new System.Drawing.Font("DS-Digital", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

this.lbl_sec.ForeColor = System.Drawing.Color.Green;

this.lbl_sec.Location = new System.Drawing.Point(423, 161);

this.lbl_sec.Name = "lbl_sec";

this.lbl_sec.Size = new System.Drawing.Size(57, 40);

this.lbl_sec.TabIndex = 1;

this.lbl_sec.Text = "00";

//

// lbl_day

//

this.lbl_day.AutoSize = true;

this.lbl_day.Font = new System.Drawing.Font("DS-Digital", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

this.lbl_day.ForeColor = System.Drawing.Color.Green;

this.lbl_day.Location = new System.Drawing.Point(343, 201);

this.lbl_day.Name = "lbl_day";

this.lbl_day.Size = new System.Drawing.Size(77, 40);

this.lbl_day.TabIndex = 2;

this.lbl_day.Text = "Day";

//

// timer1

//

this.timer1.Enabled = true;

this.timer1.Tick += new System.EventHandler(this.Time_Start);

//

// lbl_date

//

this.lbl_date.AutoSize = true;

this.lbl_date.Font = new System.Drawing.Font("DS-Digital", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

this.lbl_date.ForeColor = System.Drawing.Color.Green;

this.lbl_date.Location = new System.Drawing.Point(105, 200);

this.lbl_date.Name = "lbl_date";

this.lbl_date.Size = new System.Drawing.Size(171, 40);

this.lbl_date.TabIndex = 3;

this.lbl_date.Text = "MM-DD-YY";

//

// Main

//

this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.BackColor = System.Drawing.Color.Black;

this.ClientSize = new System.Drawing.Size(557, 342);

this.Controls.Add(this.lbl_date);

this.Controls.Add(this.lbl_day);

this.Controls.Add(this.lbl_sec);

this.Controls.Add(this.lbl_time);

this.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

this.Name = "Main";

this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

this.Text = "Form1";

this.ResumeLayout(false);

this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label lbl_time;

private System.Windows.Forms.Label lbl_sec;

private System.Windows.Forms.Label lbl_day;

private System.Windows.Forms.Timer timer1;

private System.Windows.Forms.Label lbl_date;

}

}

创建脚本

我们现在将创建脚本以使事情正常运转。首先要在表单中添加一个计时器工具,然后转到计时器属性并将 enabled 设置为true。之后转到名为 Main.cs 的 csharp 脚本,然后右键单击并选择查看代码,这将强制您转到文本编辑器。然后在表单的类中写入这些代码块。

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace Simple_Digital_Clock

{

public partial class Main : Form

{

public Main()

{

InitializeComponent();

}

private void Time_Start(object sender, EventArgs e)

{

lbl_time.Text = DateTime.Now.ToString("hh:mm");

lbl_sec.Text = DateTime.Now.ToString("ss");

lbl_day.Text = DateTime.Now.ToString("dddd");

lbl_date.Text = DateTime.Now.ToString("MMM dd yyyy");

}

}

}

尝试运行该应用程序并查看它是否有效。我们成功地使用 C# 创建了一个简单的数字时钟。我希望本教程能帮助您了解如何使用 C# 开发应用程序。

示例源码:https://download.csdn.net/download/hefeng_aspnet/90448004

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

相关推荐
钢铁男儿3 小时前
C#:创建变量和类的实例
c#
了一li4 小时前
C#中用 OxyPlot 在 WinForms 实现波形图可视化(附源码教程)
开发语言·c#
大模型铲屎官4 小时前
Unity C# 与 Shader 交互入门:脚本动态控制材质与视觉效果 (含 MaterialPropertyBlock 详解)(Day 38)
c语言·unity·c#·交互·游戏开发·材质·shader
chegan5 小时前
用c#从头写一个AI agent,实现企业内部自然语言数据统计分析(三)--一个综合的例子
ai·c#·agent
全栈小57 小时前
【C#】.net core6.0无法访问到控制器方法,直接404。由于自己的不仔细,出现个低级错误,这让DeepSeek看出来了,是什么错误呢,来瞧瞧
开发语言·c#·.netcore
浅陌sss14 小时前
C#中实现XML解析器
xml·c#
全栈师16 小时前
C#中分组循环的做法
开发语言·c#
FAREWELL0007516 小时前
C#进阶学习(十六)C#中的迭代器
开发语言·学习·c#·迭代器模式·迭代器
DXM052117 小时前
牟乃夏《ArcGIS Engine地理信息系统开发教程》学习笔记3-地图基本操作与实战案例
开发语言·笔记·学习·arcgis·c#·ae·arcgis engine
csdn_aspnet20 小时前
C# 如何验证磁盘路径,如:D:\\m\aa.txt
c#