简体   繁体   English

如何在 C# windows 表单应用程序的文本属性中显示当前日期和时间?

[英]How can I display the current date and time in my C# windows form application's text property?

I want to show the current date and time in live in the title of my C# windows form Application.我想在我的 C# windows 表格申请的标题中实时显示当前日期和时间。

My resource says that I need to use datetime.now.tostring();我的资源说我需要使用 datetime.now.tostring(); to do so, but I don't know how.这样做,但我不知道怎么做。 Any ideas?有任何想法吗?

You want to display the current date and time in the title bar of your Form using its Text property.您希望使用窗体的Text属性在窗体的标题栏中显示当前日期和时间。 The simplest way to update the time continuously is to use System.Windows.Forms.Timer which issues its ticks on the UI thread.持续更新时间的最简单方法是使用System.Windows.Forms.Timer ,它会在 UI 线程上发出计时。

Using other timers (eg System.Timers.Timer ) or updating UI controls from background workers generally can be tricky.使用其他计时器(例如System.Timers.Timer )或从后台工作者更新 UI 控件通常可能很棘手。 If your WinForms app might end up being ported to an iOS or Android device, there are better more-portable alternatives.如果您的 WinForms 应用程序可能最终被移植到 iOS 或 Android 设备,则有更好、更便携的替代方案。 A good practice for handling events generated by backgound tasks is to use BeginInvoke to ensure that the control is updated on the UI thread.处理由后台任务生成的事件的一个好做法是使用BeginInvoke来确保控件在 UI 线程上更新。

In this straightforward case however, simply handle the Tick event:然而,在这种简单的情况下,只需处理Tick事件:


Minimal Example最小的例子

截屏

public partial class MainForm : Form
{
    public MainForm() => InitializeComponent();
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        _timer.Tick += onTimerTick;
        _timer.Start();
    }

    private void onTimerTick(object? sender, EventArgs e)
    {
        var now = DateTime.Now;
        Text = $"Main Form - {now.ToShortDateString()} {now.ToLongTimeString()}";
    }

    System.Windows.Forms.Timer _timer= new System.Windows.Forms.Timer { Interval= 1000 };
}

You can use this, it will work on runtime:您可以使用它,它将在运行时运行:

public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            this.Text = "Your title here";
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Windows窗体应用程序C#中的文本框中显示元素的属性为文本? - How can I display as text the properties of an element in a textbox in windows form application c#? 如何在C#中的应用程序中在客户端获取远程服务器的日期和时间? - How can I get the remote server's date and time at the client side in my application in c#? 如何在C#中显示当前时间和日期 - How to display the current time and date in C# C#:如何在Windows应用程序中的Win窗体上自由移动控件? - C#: How can I move my controls on a win form in windows application freely? 我如何检查C#Windows应用程序中的窗体是否打开? - How can i check form is open in c# windows application? 如何在编译时在.net / C#应用程序中找到当前时间和日期? - How do I find the current time and date at compilation time in .net/C# application? 如果标签宽度不适合C#Windows窗体应用程序中的文本宽度,如何使标签中的文本像(选取框)一样移动? - How can i make the text in the label move like in (marquee) if the label width not fit the text width in C# windows form application? 如何在Windows窗体应用程序C#中中止当前任务 - How to Abort Current task in windows form application c# 我如何在C#Windows窗体应用程序中的Webbrowser控件中显示存储的html代码,而不在硬盘上创建html文件? - How can i display stored html codes in webbrowser control in C# windows form application without creating html files on hard drive? 如何在C#Windows应用程序中使用Google Maps获取两个位置之间的旅行时间数据? - How can I get travel time data between two locations using Google Maps in my C# Windows application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM