shiny
shiny

Reputation: 181

C# Wait for TextBox Text to Populate before proceeding

My program calculates a bunch of stuff, displays it in a TextBox, and carries on. I want to take a screen shot of my program to ensure it makes right calculations and decisions. However, my TextBox text always seems to lag behind. How can I fix it?

textBox26.Text = "HELLO";
            String input = File.ReadAllText(@"C:\XXXXXXXXXXXXXX\LOG.txt");
            int imageid = Convert.ToInt32(input) + 1;
            Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
                bmp.Save(@"C:\XXXXXXXXXXXXXX\" + imageid.ToString("D5") + ".png");  // saves the image
            }
            File.WriteAllText(@"C:\XXXXXXXXXXXXXX\LOG.txt", "" + imageid);

My screenshot displays blank TextBox. Please help

Upvotes: 0

Views: 174

Answers (2)

Caius Jard
Caius Jard

Reputation: 74670

It lags behind because the thread that will redraw the textbox is busy running your application code and your code goes straight from setting the text to taking the screenshot without giving the thread chance to leave your code and redraw the box. The redraw will only happen when your code is done executing. The rough and ready way to achieve what you want would be to call Application.DoEvents() between setting the text and taking the shot.

Upvotes: 2

Daniel A. White
Daniel A. White

Reputation: 190996

I believe after you set the textbox's text property, I would call Application.DoEvents() to flush the messages out.

Upvotes: 2

Related Questions