Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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...
C#
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 = "";
 }


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.
C#
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?
Posted
Updated 3-Apr-19 12:58pm
Comments
Dylvh 3-Apr-19 5:06am    
One thing that you're doing is, you're overwriting your results. You have txbSysColors.Text = str.Split('=').Last().ToString(); and then beneath that line you have txbSysColors.Text = "here"; which is overwriting what you've set to it in the line above it. You're doing a similar thing with textBox1. Why not maybe make use of a stringbuilder?
OldPCGuy 3-Apr-19 18:52pm    
I know.

When the split function was not yielding a result I just kept trying different things.
Dylvh 4-Apr-19 2:31am    
Oops.. I should have known that it's for troubleshooting.

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.
 
Share this answer
 
Comments
GenJerDan 3-Apr-19 2:15am    
And, unless I'm not thinking clearly, that code will wind up only displaying the last instance that meets the conditional. Well...they might all appear, but you won't see them becasue they'll fly by really quickly. ;)
Pawel Wzietek 3-Apr-19 2:25am    
" so long as your code is running something like an event handler, the UI thread cannot process the message pump "

It is still possible to invoke the message pump from inside the handler with "DoEvents()". Of course in this case the handler itself should be made reentrant.
Dave Kreskowiak 3-Apr-19 8:35am    
That's a bad habit that I would rather not teach anyone.

If you're using DoEvents, you're setting yourself up for failure.
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 :(
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900