如何使用计时器在文本框中显示时间我需要C#代码 [英] how to use timer for time display in textbox i need c# code

查看:115
本文介绍了如何使用计时器在文本框中显示时间我需要C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用计时器在文本框中显示时间,我需要c#代码.
我正在使用Visual Studio 2010

how to use timer for time display in textbox i need c# code .
i am use visual studio 2010

推荐答案

简单:
1)通过设计器或代码将计时器添加到表单中.
2)将间隔"属性设置为100-这将导致1/10秒的更新.
3)处理壁虱事件.
4)在Tick处理程序中,将TextBox.Text属性设置为DateTime.Now.ToString(formatString),其中formatString定义要显示的信息.在这里看看:格式化显示日期时间-格式字符串说明 [ ^ ]).

编写实际的代码是微不足道的,因此我将其留给读者练习! :laugh:
Easy:
1) Add a timer to your form, either via the designer or via code.
2) Set the Interval property to 100 - this will cause 1/10th of a second updates.
3) Handle the Tick Event.
4) In the Tick handler, set the TextBox.Text property to DateTime.Now.ToString(formatString), where teh formatString defines what info you an''t to display. Have a look here: Formatting a DateTime for display - format string description[^] for the format possibilities.

Doing the actual code is trivial, so I will leave it as an exercise for the reader! :laugh:


下一次为您的问题提供更多详细信息.

next time provide more details with your question.

public Form1()
        {
            InitializeComponent();
        }
        private Timer tm = new Timer();
        private void Form1_Load(object sender, EventArgs e)
        {
            tm.Tick += new EventHandler(tm_Tick);
            tm.Interval = 1000;
            tm.Enabled = true;
        }

        void tm_Tick(object sender, EventArgs e)
        {
            textBox1.Text = DateTime.Now.ToLongTimeString();
        }



选择答案,如果有帮助的话



select as answer, if it helps


这篇关于如何使用计时器在文本框中显示时间我需要C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆