c#中的文本框不起作用 [英] textbox in c# not work

查看:83
本文介绍了c#中的文本框不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#程序中写程序在文本框和图片框中写回答。

但是出现问题:数据不是一步一步写在图片框和textbox1上的!所有步骤都突然写在文本框和图片框上。



I have write program in c# program write answer in textbox and Picture box .
but appear problem : data aren't write on picture box and textbox1 step by step ! all step do suddenly date write on textbox and picture box.

public void Display()
        {
            var form = Form.ActiveForm as Form1;
            showcallstack myshowcallstack = new showcallstack();
            //string temp = "";
            
            form.textBox1.Text += "\r\n\t\t\t---------------------------------------------------------------------------------------\r\n";
            
            if (qee.Count != 0)
            {
                for (int i = 0; i < qee.Count; i++)
                {
                    //
                    Thread.Sleep(2000);// Not work !! 
                    //
                    if (qee[i].mood == true)
                    {
                        form.textBox1.Text += "PUSH :: \r\n";
                        form.textBox1.Text += qee[i].fun.name;
                        for (int p = 0; p < qee[i].fun.arquments.Count; p++)
                        {
                            form.textBox1.Text += " : " + qee[i].fun.arquments[p] + ",";
                        }
                        form.textBox1.Text += "\r\n";
                        form.textBox1.Text += "Pointer to line : " + qee[i].line + "\r\n";
                        for (int p = 0; p < qee[i].fun.local_variable.Count; p++)
                        {
                            form.textBox1.Text += " : " + qee[i].fun.Kind_local_variable[p] + " " + qee[i].fun.local_variable[p] + ",";
                        }
                        form.textBox1.Text += "\r\n\r\n";
                        
                    }
                    else
                    {
                        form.textBox1.Text += "POP :: \r\n";
                        form.textBox1.Text += qee[i].fun.name;
                        for (int p = 0; p < qee[i].fun.arquments.Count; p++)
                        {
                            form.textBox1.Text += " : " + qee[i].fun.arquments[p] + ",";
                        }
                        form.textBox1.Text += "\r\n";
                        form.textBox1.Text += "Pointer to line : " + qee[i].line + "\r\n";
                        for (int p = 0; p < qee[i].fun.local_variable.Count; p++)
                        {
                            form.textBox1.Text += " : " + qee[i].fun.Kind_local_variable[p] + " " + qee[i].fun.local_variable[p] + ",";
                        }
                        form.textBox1.Text += "\r\n\r\n";
                    }
                }

                for (int i = 0; i < qee.Count; i++)
                {
                    //
                    Thread.Sleep(2000);// Not work !! 

                    if (qee[i].mood == true)
                    {
                        show_push_callsatack(qee[i].fun, qee[i].line, qee[i].numberofstack);
                    }
                    else
                    {
                        show_pop_callsatack(qee[i].numberofstack);
                    }

                }
            }
            
        }





看看`Thread.Sleep(2000); //不行! 他们不工作!



look at `Thread.Sleep(2000);// Not work !! ` they aren't work!

推荐答案

好的,你这里有一些实际问题。

首先是你不明白事情如何运作:当你执行一个方法时,它从开始到结束运行:它没有停止中途并决定为你更新显示!并且调用Sleep方法会阻止当前线程执行任何操作 - 这并不意味着现在小睡一下,让显示器赶上。为什么?因为显示是从用于执行Sleep方法的同一个线程更新的!



如果你想要一个多行文本框来显示一些文本,请等待然后,再显示一些文字,等待一段时间......等等,然后你需要做两件事:

1)在你的表格中使用一个计时器来控制事情的频率更新。将Interval属性设置为2000以进行两次更新。

2)将循环中的代码移动到定时器中,以便每次Timer.Tick事件发生时执行循环一次 - 在换句话说,摆脱循环,每次都通过Timer.Tick处理程序中的代码执行一次传递。
Ok, you have some real problems here.
The first is that you don't understand how things work: when you execute a method, it runs from the start to the end: it doesn't "stop" halfway through and decide to update the display for you! And calling the Sleep method stops the current thread from doing anything - it doesn't mean "have a nap now, and let the display catch up". Why? Because the display is being updated from the same thread that you are using to execute the Sleep method!

If you want a multiline text box to show some text, wait a while, then show some more text, wait a while... and so forth, then you will need to do two things:
1) Use a Timer in your form in order to control how often things are updated. Set the Interval property to 2000 for two second updates.
2) Move the code within your loop into the timer, so that one time round the loop is executed each time the Timer.Tick event occurs - in other words get rid of the loop, and execute one "pass" through the code in the Timer.Tick handler each time instead.


这篇关于c#中的文本框不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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