任何人都可以帮助我为什么无法在字符串中获取当前时间 [英] Any One Can Help Why I do not get the current time in my string

查看:53
本文介绍了任何人都可以帮助我为什么无法在字符串中获取当前时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个公共类,我将两个变量的当前时间存储为字符串"tx",另一个用于定时器类的"myT"存储,如下所示:

I have a public class that I store my two variables one for current time as string "tx" and the other for timer class "myT" as follow:

public static class MainVariables
    {

        private static string  _tx = "";
        public static string tx
        {
            get { return MainVariables ._tx; }
            set { MainVariables ._tx = value; }
        }
        private static System.Windows.Forms.Timer _myT =
                new System .Windows .Forms .Timer();
        public static System .Windows .Forms .Timer myT
        {
            get { return MainVariables ._myT; }
            set { MainVariables ._myT = value; }
        }


    }



初始化Windows组件后,我有一个方法可以初始化我的组件,称为"myInitialize",如下所示:



after initializing windows component i have a method that initialize my components called "myInitialize" as follow:

public MainWindow()
        {
            InitializeComponent();
            myInitialize();
        }



在我的initialize方法中,我有以下代码:



in my initialize method I have the following code:

private void myInitialize()
        {
            ShowMeTheTime();
}


这是ShowMeTheTime方法:


and here is the ShowMeTheTime method:

public void ShowMeTheTime()
        {
            MainVariables .myT .Enabled = true;
            MainVariables .myT .Interval = 500;
           
            MainVariables .myT .Tick += new EventHandler( ShoWTIME );
            MainVariables .myT .Start();

        }


        public void ShoWTIME( object obj , EventArgs e )
        {
            MainVariables .tx = string .Empty;
            MainVariables .tx = DateTime .Now .ToLongTimeString();
        }



所以这是问题.为什么在"ShoWTIME"方法中使用变量"MainVariables .tx"
计时器滴答时没有任何值?
但是,如果我用textbox1.Text放置"MainVariables .tx",则textbox1的text属性会获取值并每秒显示当前时间?

您的SIncerely



So here is the question. Why my variable "MainVariables .tx " in "ShoWTIME" method
does not get any value when the timer tick?
However, if I raplace the "MainVariables .tx" with a textbox1.Text the text property of textbox1 get value and show me the current time every second?

Yours SIncerely

推荐答案

正在获得价值.我对它进行了测试.

检查为

It is getting value. i tested it on my end.

check it as

public void ShoWTIME(object obj, EventArgs e)
        {
            MainVariables.tx = string.Empty;
            MainVariables.tx = DateTime.Now.ToLongTimeString();
            Console.WriteLine(MainVariables.tx);
        }



并观察VS IDE中的输出窗口.您会看到变量中的时间到了.问题可能出在其他地方.



and observer the output window in VS IDE. you will see the time is coming in the variable. the problem could be somewhere else.


这篇关于任何人都可以帮助我为什么无法在字符串中获取当前时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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