C#标签的文本属性不会更改,应用程序无法复制错误 [英] C# labels' text properties don't change and app gives unable to copy error

查看:127
本文介绍了C#标签的文本属性不会更改,应用程序无法复制错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在不使用计时器的情况下制作简单的数字时钟。控制台版本运行良好,但表单版本没有。应用程序启动时,标签的文本属性仍会显示其默认的.Text值(label1,label2等)。



该应用程序还提供无法复制obj\Debug \ blabla.exetobin \Debug\blabla.exe错误,每当我尝试进行一些更改以使其按预期工作时。



这个是我在Form1类下使用的方法:



I am trying to make a simple digital clock without using timer. Console version runs fine but Form version doesn't. labels' Text properties still show their default .Text values (label1,label2 etc) when app starts.

The app also gives "Could not copy "obj\Debug\blabla.exe" to "bin\Debug\blabla.exe" error whenever I try to make some changes to make it work as expected.

This is the method I use under the Form1 class:

public void Time()
        {
            int[] saat = new int[6];
            saat[0] = DateTime.Now.Hour;
            saat[1] = DateTime.Now.Minute;
            saat[2] = DateTime.Now.Second;
            saat[3] = DateTime.Now.Day;
            saat[4] = DateTime.Now.Month;
            saat[5] = DateTime.Now.Year;
        baslangic:
            label1.Text = saat[0].ToString();
            label2.Text = saat[1].ToString();
            label3.Text = saat[2].ToString();
            label4.Text = saat[3].ToString();
            label5.Text = saat[4].ToString();
            label6.Text = saat[5].ToString();

            for (;;)
            {

                if (DateTime.Now.Hour != saat[0])
                {
                    saat[0] = DateTime.Now.Hour;
                    label1.Text = "";
                    goto baslangic;

                }

                else if (DateTime.Now.Minute != saat[1])
                {
                    saat[1] = DateTime.Now.Minute;
                    label2.Text = "";
                    goto baslangic;

                }
                else if (DateTime.Now.Second != saat[2])
                {
                    saat[2] = DateTime.Now.Second;
                    label3.Text = "";
                    goto baslangic;

                }
                else if (DateTime.Now.Day != saat[3])
                {
                    saat[3] = DateTime.Now.Day;
                    label4.Text = "";
                    goto baslangic;

                }
                else if (DateTime.Now.Month != saat[4])
                {
                    saat[4] = DateTime.Now.Month;
                    label5.Text = "";
                    goto baslangic;

                }
                else if (DateTime.Now.Year != saat[5])
                {
                    saat[5] = DateTime.Now.Year;
                    label6.Text = "";
                    goto baslangic;

                }


            }





和Main()方法:





and Main() method:

static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            Form1 nesne = new Form1();
            nesne.Time();
        }







没有转到的时间()方法:






Time() method without goto:

public void Time()
        {
            int[] saat = new int[6];
            saat[0] = DateTime.Now.Hour;
            saat[1] = DateTime.Now.Minute;
            saat[2] = DateTime.Now.Second;
            saat[3] = DateTime.Now.Day;
            saat[4] = DateTime.Now.Month;
            saat[5] = DateTime.Now.Year;
            while (true)
            {
                label1.Text = saat[0].ToString();
                label2.Text = saat[1].ToString();
                label3.Text = saat[2].ToString();
                label4.Text = saat[3].ToString();
                label5.Text = saat[4].ToString();
                label6.Text = saat[5].ToString();


                for (;;)
                {

                    if (DateTime.Now.Hour != saat[0])
                    {
                        saat[0] = DateTime.Now.Hour;
                        label1.Text = "";
                        break;

                    }

                    else if (DateTime.Now.Minute != saat[1])
                    {
                        saat[1] = DateTime.Now.Minute;
                        label2.Text = "";
                        break;

                    }
                    else if (DateTime.Now.Second != saat[2])
                    {
                        saat[2] = DateTime.Now.Second;
                        label3.Text = "";
                        break;

                    }
                    else if (DateTime.Now.Day != saat[3])
                    {
                        saat[3] = DateTime.Now.Day;
                        label4.Text = "";
                        break;

                    }
                    else if (DateTime.Now.Month != saat[4])
                    {
                        saat[4] = DateTime.Now.Month;
                        label5.Text = "";
                        break;

                    }
                    else if (DateTime.Now.Year != saat[5])
                    {
                        saat[5] = DateTime.Now.Year;
                        label6.Text = "";
                        break;

                    }


                }





我是什么尝试过:



公开标签并将主要代码放在Main()下面,

为Form1创建一个load事件将代码放在那里。



What I have tried:

Making labels public and putting main codes under Main(),
Creating a load event for Form1 and putting codes there.

推荐答案

无法复制错误通常是因为exe文件正在使用中:在尝试编译之前检查是否已将其正确关闭在未来。如果应用程序正在运行,则无法用新版本替换exe文件。



坦率地说,代码是它正在使用的原因:它是一个循环而不是nevr退出是因为你使用 goto 在每个if条件下将其发送回来。

停止使用 goto 。忘记它甚至存在几年 - 在此之后你可能有足够的经验来理解何时应该使用它以及何时不应该:以及后者几乎总是。在多年的时间里,我一直用C#编写代码,我以前不需要使用 goto !它们使您的代码难以阅读,难以维护且难以编写。改为使用正确的循环结构。



为什么你的标签永远不会改变?因为你的循环永远不会退出,这意味着系统不会绕过绘制应用程序显示。

如果你想连续显示一个时间,你需要设置一个Timer(使用一个500的间隔使它每半秒更新一次)并使用Tick事件每次更新一次标签。不要在Tick事件中放置循环!
The "cannot copy" error is normally because the exe file is in use: check that you have shut it down properly before you try to compile in future. If the application is running, you can't replace the exe file with a new version.

And frankly that code is why it is in use: it's a loop than nevr exits because you are using goto to send it back round in every if condition.
Stop using goto. Forget it even exists for a few years - after which you will probably have enough experience to understand when you should use it and when you shouldn't: and the latter is "nearly always". In all the many years I have been coding with C#, I have not once needed to use a goto! They make your code hard to read, hard to maintain, and hard to write. Use "proper" loop constructs instead.

And why do your labels never change? Because your loop never exits, which means that the system doesn't "get round" to painting the application display.
If you want to continually display a time, you need to set up a Timer (use an Interval of 500 so it updates every half second) and use the Tick event to update the label once each time. Do not put a loop in the Tick event!


这篇关于C#标签的文本属性不会更改,应用程序无法复制错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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