如何写入文本框 [英] How do I write to a text box

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

问题描述

我正在一次读取一行文本文件并尝试解析并提取值,然后将其写入文本框。在一个简单的教程中,我可以写入文本框。



这个工作...

I'm reading a text file one line at a time and trying to parse and extract a value then write it to a text box. In a simple tutorial I can write to the textbox.

this works...

private void button1_Click(object sender, EventArgs e)
 {
     textBox1.Text = "My test";
     txbSysColors.Text = "";
 }

 private void button2_Click(object sender, EventArgs e)
 {
     txbSysColors.Text = "System Colors";
     textBox1.Text = "";
 }





我尝试过:



当我添加条件语句时,即使在引号之间传递文字字符串,我也无法再写文本框。我看到str的内容,包含的数据是正确的。



所有文本框保持空白运行此代码。



What I have tried:

When I add conditional statements I can no longer write the text boxes even passing a literal string between quotes. I see the contents of "str" and the data contained is correct.

all the text boxes remain blank running this code.

private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            StreamReader stream = new StreamReader(openFileDialog1.FileName);

            string str = "";

            while ((str = stream.ReadLine()) != null)
            {
                if (str.Contains("SYS_CLR"))
                {
                    txbSysColors.Text = str.Split('=').Last().ToString();
                    txbSysColors.Text = "here";
                    textBox1.Text = "here";
                    textBox1.Text = str;
                }
            }
        }





是否有一些范围问题我不理解?



Is there some scope issue I'm not understanding?

推荐答案

是的。在button1 Click事件处理程序代码返回之前,文本框将保持空白。



您缺少的是您的应用程序代码在所谓的UI线程上运行。 Windows使用一个消息泵来告诉应用程序正在发生的事情,例如按键和鼠标移动以及按钮点击。它还处理消息,告诉您的应用程序绘制内容,如表单,按钮,文本框等等。



问题是您的代码运行在同一个线程消息泵运行。因此,只要您的代码运行类似于事件处理程序,UI线程就无法处理消息泵,这意味着您的控件和表单不会使用新数据重新绘制自己。在button1_Click方法完成并返回之前,您的文本框不会重新绘制。您的应用程序将返回空闲状态,并且可以处理消息泵。



您如何解决此问题?这是您的代码变得更加复杂的地方。移动读取文件的代码并将其处理为自己的方法。然后你必须在另一个线程上执行该代码以释放UI线程来处理消息泵。



现在你遇到了一个新问题。您不能从UI线程以外的任何其他方式触摸任何控件的方法或属性。因此,现在您需要另一种方法,只需使用您需要使用的数据更新文本框,而不是从处理方法中调用该方法。这将在UI线程上执行文本框更新方法。
Yep. The textboxes will remain blank until the button1 Click event handler code returns.

What you're missing is that your application code runs on what's called the UI thread. There is a message pump that Windows uses to tell your app things that are happening, like key presses and mouse moves and button clicks. It also handles the messages that tell your app to paint things, like forms, buttons, textboxes, and everything else.

The problem is that your code runs on the same thread the message pump runs on. So, so long as your code is running something like an event handler, the UI thread cannot process the message pump, which means your controls and forms don't repaint themselves with new data. Your textbox won't repaint until the button1_Click method finishes and returns. Your app will go back to an idle state and the message pump can be processed.

How do you solve this? This is where your code gets a lot more complicated. Move the code that reads the file and processes it to its own method. Then you have to execute that code on a different thread to free up the UI thread to handle the message pump.

Now you have a new problem. You cannot touch any methods or properties of controls from anything other than the UI thread. So now you need another method that just updates the textbox with the data you need it to use and than Invoke that method from your processing method. That will execute the textbox update method on the UI thread.


我注释掉了while循环并且split函数按预期工作,文本框更新就像你说的那样,一旦条件处理程序退出。



它绝非易事:(
I commented out the "while" loop and the split function worked as expected, text boxes updated just like you said , once the condition handler exited.

Its never easy is it :(


这篇关于如何写入文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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